小编典典
此XSLT转换:
'
"count(preceding-sibling::*[name()=name(current())])"/>
当应用于提供的XML文档时:
one
two
three
four
five
产生完全想要的正确结果:
/root/elemA='one'
/root/elemA[2]='two'
/root/elemA[2][@attribute1='first']
/root/elemA[2][@attribute2='second']
/root/elemB='three'
/root/elemA[3]='four'
/root/elemC/elemB='five'
当通过@ c0mrade应用于新提供的文档时:
89734
再次产生正确的结果:
/root/elemX='89734'
/root/elemX[@serial='kefw90234kf2esda9231']
说明:
仅对没有子元素或具有属性的元素进行匹配和处理。
对于任何此类元素,如果它没有子元素,则其所有祖先或自身元素都以命名为的特定模式进行处理’path‘。然后”=’theValue‘“输出零件,然后输出NL字符。
然后处理匹配元素的所有属性。
最后,将模板应用于所有子元素。
在’path‘模式下处理元素很简单:输出一个/字符和元素名称。然后,如果前面有同名的同级兄弟,则输出“ [numPrecSiblings + 1]”部分。
属性的处理很简单:首先ancestor-or-self::以'path'模式处理其父元素的所有元素,然后输出[attrName = attrValue]部分,然后输出NL字符。
注意事项:
命名空间中的名称以其初始可读形式显示,没有任何问题。
为提高可读性,[1]从不显示索引。
以下是我的初步答案(可以忽略)
这是一个纯XSLT 1.0解决方案:
以下是一个示例xml文档和一个样式表,该样式表带有一个节点集参数,并为每个成员节点生成一个有效的XPath表达式。
stylesheet (buildPath.xsl):
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
>
/
select="count(preceding-sibling::*[name(current()) =
name()])"/>
select="count(following-sibling::*[name(current()) =
name()])"/>
select="count(preceding-sibling::text())"/>
select="count(following-sibling::text())"/>
select="count(preceding-sibling::processing-instruction())"/>
select="count(following-sibling::processing-instruction())"/>
select="count(preceding-sibling::comment())"/>
select="count(following-sibling::comment())"/>
'
xml source (buildPath.xml):
textA
xxxxxxxx
yyyyyyy
结果:
/root/nodeA[2]/nodeB[2]/namespace::*[local-name() = 'myNamespace']
/root/nodeA[2]/nodeB[2]/nodeC/namespace::*[local-name() =
'myNamespace']
2020-03-03