Given an xml document where a versioning scheme has been implemented in the namespaces, what xslt can I apply to transform all the uris, mapping the document to the new version?
I don't know what namespaces a document will use, or how the references will be organised within the document.
eg
<a:add
xmlns:a="urn:_2018_1.a"
xmlns:b="urn:_2018_1.b"
xmlns:c="urn:_2018_1.c"
....
>
...
to
<a:add
xmlns:a="urn:_2019_1.a"
xmlns:b="urn:_2019_1.b"
xmlns:c="urn:_2019_1.c"
....
>
...
In XSLT 2 and 3 you can create namespace nodes using xsl:namespace
so
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="3.0">
<xsl:param name="old-ns-year" select="'2018'"/>
<xsl:param name="new-ns-year" select="'2019'"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="*">
<xsl:element name="{name()}" namespace="{replace(namespace-uri(), $old-ns-year, $new-ns-year)}">
<xsl:apply-templates select="namespace::*, @*, node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="namespace::*">
<xsl:namespace name="{name()}" select="replace(., $old-ns-year, $new-ns-year)"/>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{name()}" namespace="{replace(namespace-uri(), $old-ns-year, $new-ns-year)}" select="."/>
</xsl:template>
</xsl:stylesheet>
should give you an idea, at https://xsltfiddle.liberty-development.net/pPzifpe it transforms
<a:add
xmlns:a="urn:_2018_1.a"
xmlns:b="urn:_2018_1.b"
xmlns:c="urn:_2018_1.c"
>
</a:add>
into
<a:add xmlns:a="urn:_2019_1.a" xmlns:b="urn:_2019_1.b" xmlns:c="urn:_2019_1.c">
</a:add>
using Saxon 9.8.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.