0

此代码应该打印数据中所有元素和属性的完整路径。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0" expand-text="yes">

   <xsl:output method="text"/>
   <xsl:template match="text()"/>

   <xsl:template match="*">
       {ancestor-or-self::*/concat(node-name(),'/')}<xsl:apply-templates select="@*|*"/>
   </xsl:template>
   <xsl:template match="@*">{'@' || node-name() || ','}</xsl:template>

 </xsl:stylesheet>

所以有了这个输入

<a:b xmlns:a="ans" xmlns:c="cns">
    <c:x/>
</a:b>

我预计

{ans}b
{ans}b/ {cns}x

精确的间距无关紧要。

我得到的输出好像我使用了 name() 函数而不是 node-name 即

a:b
a:b / c:x

通过连接 local-name 和 namespace-uri 可能有一种解决方法,但我想知道为什么发布的内容没有按照我希望的那样做。

4

1 回答 1

3

也许该path功能有帮助:<xsl:value-of select="descendant::*!path()" separator="&#10;"/>.

或者构造你想要的格式namespace-uri-from-QNamelocal-name-fromQName

<xsl:value-of select="descendant::*!string-join(ancestor-or-self::*!node-name()!('{' || namespace-uri-from-QName(.) || '}' || local-name-from-QName(.)), '/')" separator="&#10;"/>

https://xsltfiddle.liberty-development.net/naZXVFj

至于为什么node-name()返回 anxs:QName但它的字符串值给出了您看到的格式,我认为https://www.w3.org/TR/xpath-functions-31/#casting-to-string指定了将 anxs:QName转换为字符串:

如果 ST 是 xs:QName 或 xs:NOTATION:

如果限定名称有前缀,则 TV 是 SV 的前缀、单个冒号 (:) 和 SV 的本地名称的串联。

否则 TV 是本地名称。

于 2020-05-26T20:23:45.797 回答