我正在尝试将以下前缀添加到我的xsd:schema
xmlns:nr0="http://NamespaceTest.com/balisesXrm"
而不更改我的 XSD 文档。 我试过这个:
<xsl:template match="xsd:schema">
<xsl:element name="nr0:{local-name()}" namespace="http://NamespaceTest.com/balisesXrm">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="node() | @*"/>
</xsl:element>
</xsl:template>
但它会产生两个问题: 1 - 我的架构变得无效,因为名称更改为:<nr0:schema xmlns:nr0="http://NamespaceTest.com/balisesXrm" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SCCOAMCD="urn:SCCOA-schemaInfo" xmlns="urn:SBEGestionZonesAeriennesSYSCA-schema">
2 - 我在 XML 架构中创建的所有元素都已被删除。
如何保留我的元素并仅将前缀添加到我的根?
对于您的第一个问题,当您真正想要创建命名空间声明时,您的代码当前正在创建一个元素。
您可以做的是,只需使用所需的命名空间声明创建一个新的xsd:schema
元素,并复制所有现有元素。
<xsl:template match="xsd:schema">
<xsd:schema xmlns:nr0="http://NamespaceTest.com/balisesXrm">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="@*|node()"/>
</xsd:schema>
</xsl:template>
或者,如果您可以使用 XSLT 2.0,则可以使用xsl:namespace
并执行此操作...
<xsl:template match="xsd:schema">
<xsl:copy>
<xsl:namespace name="nr0" select="'http://NamespaceTest.com/balisesXrm'" />
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
(在这种情况下,xsl:copy
复制现有的命名空间(
对于第二个问题,您需要将标识模板添加到样式表中(如果尚未
添加(<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
试试这个 XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
version="2.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="xsd:schema">
<xsd:schema xmlns:nr0="http://NamespaceTest.com/balisesXrm">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="@*|node()"/>
</xsd:schema>
</xsl:template>
<!-- Alternative code for XSLT 2.0 -->
<xsl:template match="xsd:schema">
<xsl:copy>
<xsl:namespace name="nr0" select="'http://NamespaceTest.com/balisesXrm'" />
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>