xml:lang 的 XPath?测试属性自轴失败
XPath for xml:lang? Testing attribute self axis fails
上下文是一个 XSLT 身份转换
<xsl:template match="abstract[@xml:lang]">
<xsl:copy>
<xsl:apply-templates select="@*[not(self::xml:lang)]|node()"/>
</xsl:copy>
</xsl:template>
所以,我希望删除属性 xml:lang
。
您应该已经看到如下所示的警告:
Warning! The self axis will never select any element nodes when
starting at an attribute node
您可以改为测试属性的 name()
:
<xsl:template match="abstract[@xml:lang]">
<xsl:copy>
<xsl:apply-templates select="@*[name() != 'xml:lang']|node()"/>
</xsl:copy>
</xsl:template>
这将根据要求有效地从 abstract
中删除 xml:lang
属性。
自轴上永远不会有属性(除非您使用的是 libxslt 处理器...)。
请参阅下面评论中的讨论
你为什么不简单地做:
<xsl:template match="abstract/@xml:lang"/>
抑制不需要的属性特别是?
这里的问题是 self::[QName]
只能引用 元素 ,而不是属性 1.
我认为 michael.hor257k 的建议最适合这种特殊情况,在这种特殊情况下检查 kjhughes 回答中的名称应该没问题,因为它是关于 xml
命名空间的,它是明确的.
但一般来说,如果您想排除单个属性而不依赖 name()
,您可以这样做:
<xsl:template match="abstract[@xml:lang]">
<xsl:copy>
<xsl:apply-templates select="@*[(. | ../@xml:lang)[2]]|node()"/>
</xsl:copy>
</xsl:template>
http://xsltransform.net/eiZQaFp/2
这只有在存在 xml:lang
属性的情况下才能正常工作,但此处 match
属性中的模式可确保它确实存在。
- 下面是 XPath 规范中的相关文本,解释了为什么会这样:
Every axis has a principal node type. If an axis can contain elements, then the principal node type is element; otherwise, it is the type of the nodes that the axis can contain. Thus,
- For the attribute axis, the principal node type is attribute.
- For the namespace axis, the principal node type is namespace.
- For other axes, the principal node type is element.
A node test that is a QName is true if and only if the type of the node (see [5 Data Model]) is the principal node type and has an expanded-name equal to the expanded-name specified by the QName. For example, child::para selects the para element children of the context node; if the context node has no para children, it will select an empty set of nodes. attribute::href selects the href attribute of the context node; if the context node has no href attribute, it will select an empty set of nodes.
self::
的主要节点类型是element,所以如果后面跟着一个QName(比如xml:lang
),那么self::xml:lang
只能引用一个元素,不能一个属性。
上下文是一个 XSLT 身份转换
<xsl:template match="abstract[@xml:lang]">
<xsl:copy>
<xsl:apply-templates select="@*[not(self::xml:lang)]|node()"/>
</xsl:copy>
</xsl:template>
所以,我希望删除属性 xml:lang
。
您应该已经看到如下所示的警告:
Warning! The self axis will never select any element nodes when starting at an attribute node
您可以改为测试属性的 name()
:
<xsl:template match="abstract[@xml:lang]">
<xsl:copy>
<xsl:apply-templates select="@*[name() != 'xml:lang']|node()"/>
</xsl:copy>
</xsl:template>
这将根据要求有效地从 abstract
中删除 xml:lang
属性。
自轴上永远不会有属性(除非您使用的是 libxslt 处理器...)。
请参阅下面评论中的讨论
你为什么不简单地做:
<xsl:template match="abstract/@xml:lang"/>
抑制不需要的属性特别是?
这里的问题是 self::[QName]
只能引用 元素 ,而不是属性 1.
我认为 michael.hor257k 的建议最适合这种特殊情况,在这种特殊情况下检查 kjhughes 回答中的名称应该没问题,因为它是关于 xml
命名空间的,它是明确的.
但一般来说,如果您想排除单个属性而不依赖 name()
,您可以这样做:
<xsl:template match="abstract[@xml:lang]">
<xsl:copy>
<xsl:apply-templates select="@*[(. | ../@xml:lang)[2]]|node()"/>
</xsl:copy>
</xsl:template>
http://xsltransform.net/eiZQaFp/2
这只有在存在 xml:lang
属性的情况下才能正常工作,但此处 match
属性中的模式可确保它确实存在。
- 下面是 XPath 规范中的相关文本,解释了为什么会这样:
Every axis has a principal node type. If an axis can contain elements, then the principal node type is element; otherwise, it is the type of the nodes that the axis can contain. Thus,
- For the attribute axis, the principal node type is attribute.
- For the namespace axis, the principal node type is namespace.
- For other axes, the principal node type is element.
A node test that is a QName is true if and only if the type of the node (see [5 Data Model]) is the principal node type and has an expanded-name equal to the expanded-name specified by the QName. For example, child::para selects the para element children of the context node; if the context node has no para children, it will select an empty set of nodes. attribute::href selects the href attribute of the context node; if the context node has no href attribute, it will select an empty set of nodes.
self::
的主要节点类型是element,所以如果后面跟着一个QName(比如xml:lang
),那么self::xml:lang
只能引用一个元素,不能一个属性。