Mor*_*rty 5 xml xslt xpath xslt-1.0
我想获得当前节点的所有祖先:

XML:
<root>
<item title="a">
<item title="b">
<item title="c"></item> <!--CURRENT-->
<item title="d"></item>
</item>
<item title="x">
<item title="y"></item>
<item title="z"></item>
</item>
</item>
</root>
Run Code Online (Sandbox Code Playgroud)
结果:
<item title="a">...</item>
<item title="b">...</item>
Run Code Online (Sandbox Code Playgroud)
编辑:轴祖先的答案很好.我的问题出在其他地方,在XSLT中
XSLT:
<xsl:variable name="curr" select="//item[@title = 'c']"></xsl:variable>
<xsl:variable name="test" select="$curr/ancestor::item"></xsl:variable>
<xsl:for-each select="$test/item">
<xsl:value-of select="@title"></xsl:value-of>
</xsl:for-each>
Run Code Online (Sandbox Code Playgroud)
返回:
bcdx
Run Code Online (Sandbox Code Playgroud)
编辑2:对于dimitre和所有有类似问题的人
我问题的所有答案都很好.
只是XSLT(up)给我一个奇怪的结果,而@Mads Hansen纠正了我.
最终工作示例:
XML:
<?xml version="1.0" encoding="utf-8"?>
<root>
<item title="a">
<item title="b">
<item title="c"></item>
<item title="d"></item>
</item>
<item title="x">
<item title="y"></item>
<item title="z"></item>
</item>
</item>
</root>
Run Code Online (Sandbox Code Playgroud)
XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="curr" select="//item[@title = 'c']"></xsl:variable>
<xsl:variable name="test" select="$curr/ancestor::item"></xsl:variable>
<xsl:for-each select="$test">
<xsl:value-of select="@title"></xsl:value-of>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
返回:
ab
Run Code Online (Sandbox Code Playgroud)
向亚当致敬,获得快速的第一答案.
您列出的预期结果与您的单词不符.根元素也是祖先节点,文档也是祖先节点.
ancestor::node()
Run Code Online (Sandbox Code Playgroud)
...将按此顺序返回一个序列:
item[@title='b']item[@title='a']root元件(又名文档元素)/要获得列出的具体结果,您需要:
ancestor::item/.
Run Code Online (Sandbox Code Playgroud)
/的效果.是将订购更改回转发文件订单.祖先的本机顺序::是反向文档顺序.
这个样式表(带OP的输入)......
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:for-each select="//item[@title='c']">
<xsl:value-of select="ancestor::item[1]/@title" />
<xsl:value-of select="ancestor::item[2]/@title" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
...将输出'ba',说明祖先::确实是反转轴的点.然而这个样式表......
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:for-each select="//item[@title='c']">
<xsl:value-of select="(ancestor::item/@title)[1]" />
<xsl:value-of select="(ancestor::item/@title)[2]" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
......结果相反'ab'.这是有启发性的,因为它表明在XSLT 1.0中(在XSLT 2.0中不是这样),括号删除了反向性质,它变成了文档有序节点集.
OP已经询问了类似......的变换.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:for-each select="//item[@title='c']">
<xsl:for-each select="ancestor::item">
<xsl:value-of select="@title" />
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
这个返回'ab'(在XSLT 2.0中它将返回'ba').为什么?因为在XSLT 1.0中,xsl:for-each指令忽略了轴的反向性并按文档顺序处理(除非xsl:sort指令另有说明).
| 归档时间: |
|
| 查看次数: |
23636 次 |
| 最近记录: |