xslt,如何根据另一个相同级别标记的内容选择标记
我的源文件如下所示:xslt,如何根据另一个相同级别标记的内容选择标记,xslt,Xslt,我的源文件如下所示: <stuff> <s> <contents> <code>503886</code> <code>602806</code> </contents> ... </s> <p> <code>344196</code> <export>true</exp
<stuff>
<s>
<contents>
<code>503886</code>
<code>602806</code>
</contents>
...
</s>
<p>
<code>344196</code>
<export>true</export>
...
</p>
<!-- more 's' and 'p' tags -->
...
</stuff>
我需要迭代“s”并选择那些-在“contents”标记中有一个属于export=true的“p”的“code”
过去几个小时我一直在努力解决这个问题。
请分享一些想法。此样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="kSByCode" match="s" use="contents/code"/>
<xsl:template match="text()"/>
<xsl:template match="p[export='true']">
<xsl:copy-of select="key('kSByCode',code)"/>
</xsl:template>
</xsl:stylesheet>
输出:
<s>
<contents>
<code>503886</code>
<code>602806</code>
</contents>
</s>
注意:如果存在交叉引用,请使用键
编辑:错过了对s
部分的迭代。谢谢你,迪米特
编辑2:重新阅读这个答案,我发现它可能会让人困惑。因此,对于选择节点的表达式,请使用:
key('kSByCode',/stuff/p[export='true']/code)
此样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="kSByCode" match="s" use="contents/code"/>
<xsl:template match="text()"/>
<xsl:template match="p[export='true']">
<xsl:copy-of select="key('kSByCode',code)"/>
</xsl:template>
</xsl:stylesheet>
输出:
<s>
<contents>
<code>503886</code>
<code>602806</code>
</contents>
</s>
注意:如果存在交叉引用,请使用键
编辑:错过了对s
部分的迭代。谢谢你,迪米特
编辑2:重新阅读这个答案,我发现它可能会让人困惑。因此,对于选择节点的表达式,请使用:
key('kSByCode',/stuff/p[export='true']/code)
我需要迭代“s”并选择
那些-哪些在“内容”标签内
具有属于“p”的“代码”
这是export=true
使用:
<xsl:apply-templates select=
"/*/s
[code
=
/*/p
[export='true']
/code]"/>
我需要迭代“s”并选择
那些-哪些在“内容”标签内
具有属于“p”的“代码”
这是export=true
使用:
<xsl:apply-templates select=
"/*/s
[code
=
/*/p
[export='true']
/code]"/>
好问题(+1)。有关一行XPath解决方案,请参见我的答案。:)好问题(+1)。有关一行XPath解决方案,请参见我的答案。:)+1我错过了迭代s
部分。我编辑了我的答案。+1我错过了迭代s
部分。我已经编辑了我的答案。