Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT with XML source that has a default namespace set to xmlns

I have an XML document with a default namespace indicated at the root. Something like this:

<MyRoot xmlns="http://www.mysite.com">    <MyChild1>        <MyData>1234</MyData>     </MyChild1>  </MyRoot> 

The XSLT to parse the XML does not work as expected because of the default namespace, i.e. when I remove the namespace, everything works as expected.

Here is my XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"             xmlns:xs="http://www.w3.org/2001/XMLSchema">  <xsl:template match="/" >   <soap:Envelope xsl:version="1.0" 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>      <NewRoot xmlns="http://wherever.com">        <NewChild>          <ChildID>ABCD</ChildID>          <ChildData>             <xsl:value-of select="/MyRoot/MyChild1/MyData"/>          </ChildData>        </NewChild>      </NewRoot>    </soap:Body>   </soap:Envelope>  </xsl:template> </xsl:stylesheet> 

What needs to be done with XSLT document so that translation works properly? What exactly needs to be done in XSLT document?

like image 560
Larry Avatar asked Aug 27 '09 22:08

Larry


People also ask

How do I create a namespace in XSLT?

You'll usually find a namespace declaration in a document element's start-tag, and the XSLT processor passes this declaration along to the start-tag of the result document's document element. It also passes all references to that namespace along to the result tree, making the result document a working XLink document.

What is namespace in XSLT?

The namespace http://www.w3.org/1999/XSL/Transform is referred to as "the XSLT namespace". The prefix xsl is conventionally used to refer to this namespace (and is so used both within this document and within the XSLT specification), but it has no special status: any prefix may be used.

Why do we use Xmlns in XML?

An XML namespace is a collection of names that can be used as element or attribute names in an XML document. The namespace qualifies element names uniquely on the Web in order to avoid conflicts between elements with the same name.

What is xmlns in XML schema?

As defined by the W3C Namespaces in XML Recommendation , an XML namespace is a collection of XML elements and attributes identified by an Internationalized Resource Identifier (IRI); this collection is often referred to as an XML "vocabulary."


2 Answers

You need to declare the namespace in your XSLT, and use it in XPath expressions. E.g.:

<xsl:stylesheet ... xmlns:my="http://www.mysite.com">     <xsl:template match="/my:MyRoot"> ... </xsl:template>  </xsl:stylesheet> 

Note that you must provide some prefix if you want to refer to elements from that namespace in XPath. While you can just do xmlns="..." without the prefix, and it will work for literal result elements, it won't work for XPath - in XPath, an unprefixed name is always considered to be in namespace with blank URI, regardless of any xmlns="..." in scope.

like image 116
Pavel Minaev Avatar answered Oct 19 '22 09:10

Pavel Minaev


If you use XSLT 2.0, specify xpath-default-namespace="http://www.example.com" in the stylesheet section.

like image 43
vinh Avatar answered Oct 19 '22 07:10

vinh