Solved

XML transformation using XSL

Posted on 2007-11-19
7
1,168 Views
Last Modified: 2013-11-18
Hi folks:

I have the following xml and xsl but it produces an incorrect transformation because of the inclusion of attributes like xmlns:xsi etc in the root element.

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="organization.xsl"?>
<Organization xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://some_name_space">
  <Data>
    <Field1>1001</Field1>
  </Data>
</Organization>


<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml"/>
 
  <xsl:template match="/">
    <xsl:element name="Organization">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="Data">
   
      <xsl:for-each select="/Organization/Data">
        <xsl:element name='{name()}'>
          <xsl:for-each select="*" >
            <xsl:element name='{name()}'>
              <xsl:value-of select="."/>
            </xsl:element>
          </xsl:for-each>
        </xsl:element>
      </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

It produces the following result

<?xml version="1.0" encoding="utf-16"?><Organization>1001</Organization>

The logic that I am using to do the transformation is as follows:

  string xml = "<xml goes here>";
 System.Xml.Xsl.XslCompiledTransform xslTransform = new XslCompiledTransform();
      xslTransform.Load(@"C:\Organization.xsl");
      XmlDocument doc = new XmlDocument();
      doc.LoadXml(xml);
      StringWriter writer = new StringWriter();
      xslTransform.Transform(doc, null, writer);
      string transformedXML = writer.ToString();

Now on using the same logic but a different XML

<?xml version=\"1.0\" encoding=\"utf-16\"?><Organization><Data><Field1>1001</Field1></Data></Organization>

that does has a clean root element I get the desired result which is as follows:

<?xml version="1.0" encoding="utf-16"?><Organization><Data><Field1>1001</Field1></Data></Organization>

why the inclusion of extra attributes in root element is causing a problem. Looks like it is not matching the desired template in XSL
0
Comment
Question by:rxraza
  • 4
  • 3
7 Comments
 
LVL 4

Accepted Solution

by:
rallsaldo earned 500 total points
ID: 20319316
Hi rxraza,

I think it is because you have specified a namespace in your xml (xmlns="http://some_name_space") but not used it in the xsl. If you take it out of your xml I think this should work as you want it to or if you change the declaration to xmlns:xsl="http://some_name_space" that should also work (i.e. adding in the abbreviation :xsl).

I hope that helps,
R
0
 

Author Comment

by:rxraza
ID: 20320916
Thanks for the reply. Is there any other way out of it. I would not want to change the incoming XML because it is fed from a different system.
0
 
LVL 4

Expert Comment

by:rallsaldo
ID: 20322062
Hi,

Try specifying a default namespace in your xsl file and then change the references to the nodes in your code:
- add xmlns:rxraza="http://some_name_space"> to <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:rxraza="http://some_name_space">

- change references from <xsl:element name="Organization"> to <xsl:element name="rxraza:Organization"> etc, for all

Hope that helps,
R
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 4

Expert Comment

by:rallsaldo
ID: 20322080
Sorry I may not have made that very clear:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:rxraza="http://some_name_space">
  <xsl:output method="xml"/>
  
  <xsl:template match="/">
    <xsl:element name="rxraza:Organization">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
 
  <xsl:template match="rxraza:Data">
    
      <xsl:for-each select="/rxraza:Organization/rxraza:Data">
        <xsl:element name='{name()}'>
          <xsl:for-each select="*" >
            <xsl:element name='{name()}'>
              <xsl:value-of select="."/>
            </xsl:element>
          </xsl:for-each>
        </xsl:element>
      </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Open in new window

0
 

Author Comment

by:rxraza
ID: 20362320
I can't get your second approach to work. Have you tried that at your end?
0
 

Author Comment

by:rxraza
ID: 20362544
I tried the following xml and XSL

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="organization_01.xsl"?>
<rxraza:Organization xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:rxraza="http://some_name_space">
  <rxraza:Data>
    <rxraza:Field1>1001</rxraza:Field1>
  </rxraza:Data>
</rxraza:Organization>

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:rxraza="http://some_name_space">
  <xsl:output method="xml"/>
 
  <xsl:template match="/">
    <xsl:element name="rxraza:Organization">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
 
  <xsl:template match="rxraza:Data">
   
      <xsl:for-each select="/rxraza:Organization/rxraza:Data">
        <xsl:element name='{name()}'>
          <xsl:for-each select="*" >
            <xsl:element name='{name()}'>
              <xsl:value-of select="."/>
            </xsl:element>
          </xsl:for-each>
        </xsl:element>
      </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

and the output that I got is as follows:

<?xml version="1.0" encoding="utf-16"?><Organization>1001</Organization>
0
 
LVL 4

Expert Comment

by:rallsaldo
ID: 20365008
Hi,

Yes if I do it using an XML editor, Oxygen http://www.oxygenxml.com I get the following:

<?xml version="1.0" encoding="utf-8"?><rxraza:Organization xmlns:rxraza="http://some_name_space">
    <rxraza:Data><rxraza:Field1>1001</rxraza:Field1></rxraza:Data>
</rxraza:Organization>

How are you running the translation? Is the xml you included correct as its different from your first paste and I think you said that you couldn't change the xml. If I use your original xml:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="organization.xsl"?>
<Organization xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://some_name_space">
  <Data>
    <Field1>1001</Field1>
  </Data>
</Organization>

and then the xsl:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:rxraza="http://some_name_space">
  <xsl:output method="xml"/>
 
  <xsl:template match="/">
    <xsl:element name="rxraza:Organization">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
 
  <xsl:template match="rxraza:Data">
   
      <xsl:for-each select="/rxraza:Organization/rxraza:Data">
        <xsl:element name='{name()}'>
          <xsl:for-each select="*" >
            <xsl:element name='{name()}'>
              <xsl:value-of select="."/>
            </xsl:element>
          </xsl:for-each>
        </xsl:element>
      </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

I get:
<?xml version="1.0" encoding="utf-8"?><rxraza:Organization xmlns:rxraza="http://some_name_space">
    <Data><Field1>1001</Field1></Data>
</rxraza:Organization>

Let me know...
Cheers,
Ian


0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question