Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Namespace Nodes... not a really an attribute? [Thread Next] Re: Namespace Nodes... not a really an attribute?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/ | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
