transforming XML with namspaces to XML using XSL

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
rxrazaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Geert BormansConnect With a Mentor Information ArchitectCommented:
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
 
Geert BormansInformation ArchitectCommented:
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
 
rxrazaAuthor Commented:
How did you test it. Did you open the XML in the IE browser?
0
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.

 
Geert BormansInformation ArchitectCommented:
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
 
rxrazaAuthor Commented:
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
 
rxrazaAuthor Commented:
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
 
Geert BormansInformation ArchitectCommented:
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
 
rxrazaAuthor Commented:
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
 
Geert BormansInformation ArchitectCommented:
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
 
rxrazaAuthor Commented:
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
 
rxrazaAuthor Commented:
you truly are a GENIUS
0
 
Geert BormansInformation ArchitectCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.