简体   繁体   English

xsl名称空间uri转换

[英]xsl namespace uri transformation

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? 给定一个在名称空间中已实现版本控制方案的xml文档,我可以应用什么xslt来转换所有uri,将文档映射到新版本?

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 在XSLT 2和3中,您可以使用xsl:namespace创建名称空间节点,因此

<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 应该会在https://xsltfiddle.liberty-development.net/pPzifpe上给您一个想法

<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. 使用Saxon 9.8。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM