Solved

transforming XML with namspaces to XML using XSL

Posted on 2007-11-27
12
425 Views
Last Modified: 2013-11-18
Hi folks:

Somehow I can't figure out why XML is not correctly transformed when it contains namepspace(s). When it does not contain namesspace(s) the transformation works as expected.

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="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>

where as I should have gotten the following

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

Thanks in advance
0
Comment
Question by:rxraza
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
12 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20363685
that is strange.
Your XSLT does exactly what it should do on my machine
The tags are there, but maybe in the wrong namespace
(by using name() you pass the namespace as well

I have simplified your XSLT a bit

<?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"
     exclude-result-prefixes="rxraza">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

note that it is important when you work with namespaces
that the url (not necesarily the prefix) is equal on both sides of he equation
0
 

Author Comment

by:rxraza
ID: 20366115
How did you test it. Did you open the XML in the IE browser?
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20366168
No, opening in a browser is no good.

I tested this using an Integrated Development Environment, that I use for XSLT development
In this case I used Oxygen (www.oxygenxml.com)
That is the only way to decently debug an XSLT

If you view the result in a browser, you will not see the tags that are not html
(because the browser is set to ignore the tags outside the html namespace)

You could transform the XML using XSLT outside a browser, on the command line
(eg. using msxsl, xalan or saxon)
and then view the xml in the treeview in the browser,
but then you look at it as XML, not as HTML

I hope this helps

Geert
0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

Author Comment

by:rxraza
ID: 20366296
Yes you are right. I tested mine with C# code. Anyways, you gave me enough clues to figure out the problem. The xsl file being pointed was different. :-)
0
 

Author Comment

by:rxraza
ID: 20366716
Gertone:

Thanks for assisting me so far. I got a more complex xml structure now and I applied the same logic but am not getting the desired results

The result that I am getting is

<Organization>1001</Organization>

instead of getting

<Organization><OrganizationChild></OrganizationChild></Organization>

Following is the XML and XSL

<?xml version="1.0" encoding="iso-8859-1" ?>
<SOAP-ENV:Envelope xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <SOAP-ENV:Header>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <ns1:NodeA xmlns:ns1="namespace1" xmlns:ns2="http://namespace2">
      <ns2:Organization>
        <ns2:Data>
          <ns2:Field>1001</ns2:Field>
        </ns2:Data>
      </ns2:Organization>
    </ns1:NodeA>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:SOAP-ENV="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:ns1="http://namespace1"
  xmlns:ns2="http://namespace2"
>
  <xsl:output method="xml" encoding="UTF-8" />

  <xsl:template match="/">
    <xsl:element name="Organization">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="SOAP-ENV:Body">
    <xsl:element name="OrganizationChild">
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

Am I missing something here? Thanks in advance
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20367809
you simply had the wrong namespace URI for SOAP-ENV
I also made some changes to your stylesheet to simplyfy it

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ns1="http://namespace1"
    xmlns:ns2="http://namespace2"
    >
    <xsl:strip-space elements="*"/>
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
   
    <xsl:template match="SOAP-ENV:Body">
        <xsl:element name="Organization">
            <xsl:element name="OrganizationChild">
                <xsl:apply-templates/>
            </xsl:element>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

cheers

Geert
0
 

Author Comment

by:rxraza
ID: 20378576
Gertone:

You have been awesome in pointing errors. I got another trouble. For the following changed XSL I am not getting the desired result

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:ns1="http://namespace1"
  xmlns:ns2="http://namespace2"
>
  <xsl:output method="xml" encoding="UTF-8"/>

  <xsl:template match="/">
    <xsl:element name="Organization">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="SOAP-ENV:Body">
    <xsl:for-each select="/SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:NodeA/ns2:Organization/ns2:Data">
      <xsl:element name="transformedData"></xsl:element>
      </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

What I am getting is following

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

as opposed to the desired following

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

Am I missing something again ? I would appreciate any help in this matter.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20378599
change this template

<xsl:template match="SOAP-ENV:Body">
    <xsl:for-each select="ns1:NodeA/ns2:Organization/ns2:Data">
      <xsl:element name="transformedData"></xsl:element>
      </xsl:for-each>
  </xsl:template>

in the template matching SOAP-ENV:Body, you should use the context
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 20378623
on top of that, your namespaces strings are not equal
    xmlns:ns1="namespace1"
and
    xmlns:ns1="http://namespace1"

they need to be identical strings in both XML and XSLT
0
 

Author Comment

by:rxraza
ID: 20399834
Thanks Gertone for all the feedbacks. One final question. If a schema contains references to other schemas do we have to include all schemas in the XSL stylesheet in its stylesheet  element even though those elements are not even used ?
0
 

Author Closing Comment

by:rxraza
ID: 31411337
you truly are a GENIUS
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20401360
welcome

you only need to reference the namespaces that are used in the stylesheet
The stylesheet is XML, so you have to make sure that teh XSLT is wellformed
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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
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:
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…
Suggested Courses

615 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