gpt4 book ai didi

xslt - 选择具有默认命名空间的节点

转载 作者:行者123 更新时间:2023-12-03 17:20:18 25 4
gpt4 key购买 nike

我有一个 XML 文档,其中使用了许多不同的命名空间和一个要验证的模式。架构要求所有元素都“合格”,我认为这意味着它们需要具有完整的 QName,而没有空 namespace 。

但是,这个巨大的 XML 文档中的某些元素仅使用默认 namespace 就被忽略了,在本文档中,默认 namespace 是空白的。自然地,它们无法通过模式验证。

我正在尝试编写一个 XSLT,它将选择没有命名空间的节点,并为它们分配一个与其他节点具有相同前缀的特定节点。例如:

<x:doc xmlns:x="http://thisns.com/">
<x:node @x:property="true">
this part passes validation
</x:node>
<node property="false">
this part does not pass validation
</node>
</x:doc>

我试过添加 xmlns="http://thisns.com/"到文档的根节点,但这与模式验证器不一致。关于如何使这项工作有任何想法?

谢谢!

最佳答案

<!-- Identity transform by default -->
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<!-- Override identity transform for elements with blank namespace -->
<xsl:template match="*[namespace-uri() = '']">
<xsl:element name="{local-name()}" namespace="http://thisns.com/">
<xsl:apply-templates select="node() | @*"/>
</xsl:element>
</xsl:template>
<!-- Override identity transform for attributes with blank namespace -->
<xsl:template match="@*[namespace-uri() = '']">
<xsl:attribute name="{local-name()}" namespace="http://thisns.com/"><xsl:value-of select="."/></xsl:attribute>
</xsl:template>

这将产生类似于以下内容的结果:
<x:doc xmlns:x="http://thisns.com/">
<x:node x:property="true">
this part passes validation
</x:node>
<node xp_0:property="false" xmlns="http://thisns.com/" xmlns:xp_0="http://thisns.com/">
this part does not pass validation
</node>
</x:doc>

请注意,第二个 仍然没有命名空间前缀,但由于 xmlns= 属性,它现在被视为同一命名空间的一部分。

关于xslt - 选择具有默认命名空间的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1008930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com