使用XSLT复制具有名称空间的元素

使用XSLT复制具有名称空间的元素,xslt,Xslt,我想复制元素中的名称空间。namespace属性及其值可能不同,并且可以出现在任何元素中。但我想复制名称空间的原样。另外,我不应该包含任何属性作为复制名称空间的附加属性。我正在使用Saxon 9(he)XSLT处理器进行转换 在下面的XML文件中,我得到了缺少“xmlns:ct ext”属性的元素。我尝试了copy namespaces=“yes”,但没有得到正确的输出。我正在为各种DTD编写一个通用XSLT 示例XML: <?xml version="1.0" encoding="UTF

我想复制元素中的名称空间。namespace属性及其值可能不同,并且可以出现在任何元素中。但我想复制名称空间的原样。另外,我不应该包含任何属性作为复制名称空间的附加属性。我正在使用Saxon 9(he)XSLT处理器进行转换 在下面的XML文件中,我得到了缺少
“xmlns:ct ext”
属性的元素
。我尝试了
copy namespaces=“yes”
,但没有得到正确的输出。我正在为各种DTD编写一个通用XSLT

示例XML:

<?xml version="1.0" encoding="UTF-8"?>
<ct:doc identifier="GPPCIA702661235" xsi:schemaLocation="http://test.com/test test.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ct="http://test.com/test" xmlns="http://www.w3.org/1998/Math/MathML" xmlns:ct-ext="http://test.com/test-ext">
<ct:doc-meta identifier="EHIXRW383636159">
<ct:para><ct:inline-math identifier="RCSNDD453018159"><math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>&#x0024;</mi><mn>1.65</mn></mrow></math></ct:inline-math></ct:para>
<ct-ext:case identifier="CDVOXU875594216" xmlns:ct-ext="http://test.com/test-ext">
<ct:simple-meta identifier="HNKRFT326435269">
<ct:title identifier="CGSVLX990515344">This is title</ct:title>
</ct:simple-meta>
</ct-ext:case>
</ct:doc-meta>
</ct:doc>
<?xml version="1.0" encoding="UTF-8"?>
<ct:doc identifier="GPPCIA702661235" xsi:schemaLocation="http://test.com/test test.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ct="http://test.com/test" xmlns="http://www.w3.org/1998/Math/MathML" xmlns:ct-ext="http://test.com/test-ext">
<ct:doc-meta identifier="EHIXRW383636159">
<ct:para><ct:inline-math identifier="RCSNDD453018159"><math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>&#x0024;</mi><mn>1.65</mn></mrow></math></ct:inline-math></ct:para>
<ct-ext:case identifier="CDVOXU875594216" xmlns:ct-ext="http://test.com/test-ext">
<ct:simple-meta identifier="HNKRFT326435269">
<ct:title identifier="CGSVLX990515344">This is title</ct:title>
</ct:simple-meta>
</ct-ext:case>
</ct:doc-meta>
</ct:doc>

$;1.65
这是标题
所需输出:

<?xml version="1.0" encoding="UTF-8"?>
<ct:doc identifier="GPPCIA702661235" xsi:schemaLocation="http://test.com/test test.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ct="http://test.com/test" xmlns="http://www.w3.org/1998/Math/MathML" xmlns:ct-ext="http://test.com/test-ext">
<ct:doc-meta identifier="EHIXRW383636159">
<ct:para><ct:inline-math identifier="RCSNDD453018159"><math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>&#x0024;</mi><mn>1.65</mn></mrow></math></ct:inline-math></ct:para>
<ct-ext:case identifier="CDVOXU875594216" xmlns:ct-ext="http://test.com/test-ext">
<ct:simple-meta identifier="HNKRFT326435269">
<ct:title identifier="CGSVLX990515344">This is title</ct:title>
</ct:simple-meta>
</ct-ext:case>
</ct:doc-meta>
</ct:doc>
<?xml version="1.0" encoding="UTF-8"?>
<ct:doc identifier="GPPCIA702661235" xsi:schemaLocation="http://test.com/test test.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ct="http://test.com/test" xmlns="http://www.w3.org/1998/Math/MathML" xmlns:ct-ext="http://test.com/test-ext">
<ct:doc-meta identifier="EHIXRW383636159">
<ct:para><ct:inline-math identifier="RCSNDD453018159"><math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mi>&#x0024;</mi><mn>1.65</mn></mrow></math></ct:inline-math></ct:para>
<ct-ext:case identifier="CDVOXU875594216" xmlns:ct-ext="http://test.com/test-ext">
<ct:simple-meta identifier="HNKRFT326435269">
<ct:title identifier="CGSVLX990515344">This is title</ct:title>
</ct:simple-meta>
</ct-ext:case>
</ct:doc-meta>
</ct:doc>

$;1.65
这是标题
XSLT尝试:

<?xml version='1.0'?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ct="http://test.com/test" xmlns="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:ct-ext="http://test.com/test-ext">
<xsl:output method="xml" encoding="UTF-8" indent="no"/>
<xsl:template match="@*|node()">
<xsl:copy copy-namespaces="yes">
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

是否使用
复制名称空间
无关紧要(不管怎样,默认值是“是”),如果输入XML在内部元素节点上有重复的名称空间声明,则XSLT处理器操作的数据模型与不存在内部名称空间声明时生成的数据模型没有区别。 所以


...

<root xmlns:ct-ext="http://test.com/test-ext">
  <ct-ext:case>...</ct-ext:case>
</root>

...

因此,在序列化复制的结果树时,最初重复的命名空间声明将丢失。我认为XSLT没有办法阻止这种情况发生。

详细说明Martin所说的话:您正在对XML工具(如XSLT)提出要求,而这些工具并不是为满足这些要求而设计的

如果我们使用更精确的术语,这将有所帮助。您要求复制的不是名称空间,而是

XML工具的设计目的是能够在您指定的名称空间中生成指定的XML元素(和其他节点)。这是XML信息模型的一部分。 只要输出XML在正确的名称空间中具有正确的元素和属性,就不需要XML工具来指定要使用的名称空间前缀或将名称空间声明放置在何处


因此,您指定的需求对于任何下游XML使用者都不是必需的。如果您解释一下为什么希望名称空间声明以某种方式出现,我们可以帮助您找到实现这些目的的方法,这些方法与XML名称空间的设计工作方式兼容。

谢谢您的回答。