gpt4 book ai didi

xml - xslt 和 xpath : match directly preceding comments

转载 作者:数据小太阳 更新时间:2023-10-29 02:23:03 25 4
gpt4 key购买 nike

我正在尝试对一批 XML 文档应用 XSLT 转换。转换的要点是重新排序几个元素。我希望保留任何直接在元素之前的注释:

<!-- not this comment -->
<element />

<!-- this comment -->
<!-- and this one -->
<element />

我最接近的解决方案是使用表达式:

<xsl:template match="element">
<xsl:copy-of select="preceding-sibling::comment()"/>
</xsl:template>

它吸引了太多评论:

<!-- not this comment -->
<!-- this comment -->
<!-- and this one -->

我明白为什么前面提到的 XPath 不能正常工作,但我对如何继续没有任何好的想法。我正在寻找的是选择所有前面的评论,其后续兄弟是另一个评论或正在处理的当前元素:

preceding-sibling::comment()[following-sibling::reference_to_current_element() or following-sibling::comment()]

最佳答案

<xsl:template match="element">
<xsl:copy-of select="preceding-sibling::comment()[
generate-id(following-sibling::*[1]) = generate-id(current())
]"/>
</xsl:template>

更高效:

<xsl:key 
name = "kPrecedingComment"
match = "comment()"
use = "generate-id(following-sibling::*[1])"
/>

<!-- ... -->

<xsl:template match="element">
<xsl:copy-of select="key('kPrecedingComment', generate-id())" />
</xsl:template>

关于xml - xslt 和 xpath : match directly preceding comments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2991141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com