Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

XML transformation using XSL

Posted on 2007-11-19
7
Medium Priority
?
1,178 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 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

916 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