Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Namespace Nodes... not a really an attribute?

From: Martin Honnen <mahotrash@-----.-->
To: NULL
Date: 5/12/2007 2:19:00 PM

ComCastNews wrote:
> Basically, I want to take an XML which 
> may include namespaces, and reformat it into a proprietary XML format:
> 
> Here's an example:
> 
> SOURCE XML:
> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
> xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
> xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
>   <soap:Body>
>     <HelloWorld xmlns="http://lartermi.org/" />
>   </soap:Body>
> </soap:Envelope>
> 
> DESIRED OUTPUT XML:
> <nameValues>
>         <nameValue name="url" dataType="attribute" value="" />
>         <nameValue dataType="element" name="soap:Envelope" value="">
>           <nameValue dataType="attribute" name="xmlns:xsi" 
> value="http://www.w3.org/2001/XMLSchema-instance"/>
>           <nameValue dataType="attribute" name="xmlns:xsd" 
> value="http://www.w3.org/2001/XMLSchema"/>
>           <nameValue dataType="attribute" name="xmlns:soap" 
> value="http://schemas.xmlsoap.org/soap/envelope"/>
>           <nameValue dataType="element" name="soap:Body" value="">
>             <nameValue dataType="element" name="HelloWorld" value="" />
>           </nameValue>
>         </nameValue>
>       </nameValues>

Yes, in the XSLT/XPath data model xmlns declarations are not attributes.
Rather, each element node has associated namespace nodes 
(<http://www.w3.org/TR/xpath#namespace-nodes>) which you find on the 
namespace axis. There you will not find the declared namespaces but 
rather the namespaces in scope.

This stylesheet

<xsl:stylesheet
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   version="1.0">

   <xsl:strip-space elements="*"/>

   <xsl:output method="xml" indent="yes"/>

   <xsl:template match="/">
     <nameValues>
       <xsl:apply-templates select="*"/>
     </nameValues>
   </xsl:template>

   <xsl:template match="*">
     <nameValue dataType="element" name="{name()}" value="">
       <xsl:apply-templates select="@*"/>
       <xsl:for-each select="namespace::*[not(. = ../../namespace::*)]">
         <nameValue dataType="attribute" value="{.}">
           <xsl:attribute name="name">
             <xsl:choose>
               <xsl:when test="local-name()">
                 <xsl:value-of select="concat('xmlns:', local-name())"/>
               </xsl:when>
               <xsl:otherwise><xsl:text>xmlns</xsl:text></xsl:otherwise>
             </xsl:choose>
           </xsl:attribute>
         </nameValue>
       </xsl:for-each>
       <xsl:apply-templates select="*"/>
     </nameValue>
   </xsl:template>

</xsl:stylesheet>

when applied to your XML sample, yields the following result:

<nameValues>
    <nameValue dataType="element" name="soap:Envelope" value="">
       <nameValue dataType="attribute" 
value="http://www.w3.org/XML/1998/namespace" name="xmlns:xml"/>
       <nameValue dataType="attribute" 
value="http://www.w3.org/2001/XMLSchema-instance" name="xmlns:xsi"/>
       <nameValue dataType="attribute" 
value="http://www.w3.org/2001/XMLSchema" name="xmlns:xsd"/>
       <nameValue dataType="attribute" 
value="http://schemas.xmlsoap.org/soap/envelope/" name="xmlns:soap"/>
       <nameValue dataType="element" name="soap:Body" value="">
          <nameValue dataType="element" name="HelloWorld" value="">
             <nameValue dataType="attribute" 
value="http://lartermi.org/" name="xmlns"/>
          </nameValue>
       </nameValue>
    </nameValue>
</nameValues>

As you can see, the xml namespace is always in scope althoug it is not 
declared. You can filter that out if needed.


-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent