Moonwalker
Moonwalker

Reputation: 3812

XSLT namespace and XML doc without one - how does it work

I have an XML document that defines a default namespace i.e.

<rootnode xmlns="..."><book>...</book>

None of the tags in this document carry any prefix.

Now I am looking at a XSLT document that is supposedly transforming the above document and it does define a namespace and includes patterns for nodes using this namespace prefix "foo:book".

<xsl:stylesheet xmlns:foo="..."> 

How does it work..i.e can this XSLT really match the tags using namespace prefix to tags in XML doc that do not have that prefix ?

The only thing I find intriguing is the URL for namespace in both XML and XSLT is exactly the same but not sure if that is relevant.

Can anyone explain this?

Upvotes: 0

Views: 57

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117043

How does it work..i.e can this XSLT really match the tags using namespace prefix to tags in XML doc that do not have that prefix ?

Yes. The prefix itself is meaningless; what matters is the namespace URI that it's bound to (in the stylesheet, where it's being used).

The only thing I find intriguing is the URL for namespace in both XML and XSLT is exactly the same but not sure if that is relevant.

That's exactly how it works. For example, the instruction:

<xsl:copy-of select="foo:chapter"/>

selects:

<chapter xmlns="http://www.example.com">

provided the stylesheet declares:

xmlns:foo="http://www.example.com"

somewhere on the ancestor-or-self axis relative to the instruction.

Upvotes: 1

Related Questions