I had to transform a xml to a html with xlst. This is fairly simple if you don't consider the namespace declarations.
Because I had some trouble to find this solution for handling the namespaces and I'm currious if this can be done in a easier way I create this post.
Problem: Given the following xml the namespaces should be printed when they are declared. The root element has two namespace declarations but calling
namespace::node()
All the Namespaces are listed.
<foo:root xmlns:foo="http://www.foo.com" xmlns:bar="http://www.bar.com">
<xx:child xmlns:xx="http://www.xx.com" />
<bar:otherChild>
<defaultNamespace />
</bar:otherChild>
</foo:root>
Solution:
<!--
Select the current namespace without the xml namespace and without the namespaces of all ancestors (parents)
-->
<xsl:for-each select="namespace::node()[name()!='xml'][not(.=ancestor::*[position()>1]/namespace::*)]">
<xsl:variable name="name" select="name(current())" /> <!-- would be foo -->
<xsl:variable name="namespace" select="current()" /> <!-- http://www.foo.com -->
</xsl:for-each>
No comments:
Post a Comment