Solved

XML transformation using XSL

Posted on 2007-11-19
7
1,164 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now