Solved

XML transformation using XSL

Posted on 2007-11-19
7
1,167 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Icons and Colors for Terms 3 24
LINQ - C# to VB convertion 12 48
Syntax error 9 45
C# parent child form 5 19
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
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. 
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

863 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

22 Experts available now in Live!

Get 1:1 Help Now