Solved

XML transformation using XSL

Posted on 2007-11-19
7
1,171 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
How our DevOps Teams Maximize Uptime

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

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

829 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