10

XSLT for the namespace edit

 3 years ago
source link: https://www.codesd.com/item/xslt-for-the-namespace-edit.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

XSLT for the namespace edit

advertisements

I have an XSLT transform which given some xml, I would like it to change the xml's namespace URI.

Input XML:

<given xmlns="http://www.sample.co.uk/version/6">
  <child>content here</child>
</given>

XSLT:

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

<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/node()[1]" >
    <xsl:element name="{local-name()}" namespace="{concat(substring-before(namespace-uri(), '/6'),'/7')}" >
        <xsl:apply-templates />
    </xsl:element>
</xsl:template>

<xsl:template match="*">
    <xsl:element name="{local-name()}">
        <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

Output XML:

<given xmlns="http://www.sample.co.uk/version/7">
  <child xmlns="">content here</child>
</given>

The XSLT transform works as expected but, as you can see, it has included an empty namespace in the child node <child xmlns="">. I would like the output child node to just be <child>. How can I achieve this?

Thanks in advance, PM


If the root element has xmlns="http://www.sample.co.uk/version/6" then that namespace applies to all child and descendant elements so basically you need to make sure your XSLT changes the namespace of all elements doing e.g.

<xsl:template match="*">
  <xsl:element name="{local-name()}" namespace="{concat(substring-before(namespace-uri(), '/6'),'/7')}" >
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK