gpt4 book ai didi

xslt - XSLT-如何包装具有特定属性的相邻元素

转载 作者:行者123 更新时间:2023-12-01 06:41:58 24 4
gpt4 key购买 nike

我是xsl的新手。我发现了类似的东西,但不能完全调整为我的用途。

输入:

<section>
<heading>some heading text</heading>
<amendment chapter="1">
<foo/>
</amendment>
<amendment chapter="2">
<bar/>
</amendment>
<amendment chapter="3">
<baz/>
</amendment>
<heading>some heading text</heading>
<amendment chapter="1">
<baz/>
</amendment>
</section>

包装属性为“chapter ='1'或Chapter ='2'”的元素。
输出:
<section>
<heading>some heading text</heading>
<newwrapper>
<amendment chapter="1">
<foo/>
</amendment>
<amendment chapter="2">
<bar/>
</amendment>
</newwrapper>
<amendment chapter="3">
<baz/>
</amendment>
<heading>some heading text</heading>
<newwrapper>
<amendment chapter="1">
<baz/>
</amendment>
</newwrapper>
</section>

最佳答案

I.此XSLT 2.0转换:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="/*">
<section>
<xsl:for-each-group select="*"
group-adjacent="self::amendment and @chapter =(1,2)">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<newwrapper>
<xsl:sequence select="current-group()"/>
</newwrapper>
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</section>
</xsl:template>
</xsl:stylesheet>

应用于提供的XML文档时 :
<section>
<heading>some heading text</heading>
<amendment chapter="1">
<foo/>
</amendment>
<amendment chapter="2">
<bar/>
</amendment>
<amendment chapter="3">
<baz/>
</amendment>
<heading>some heading text</heading>
<amendment chapter="1">
<baz/>
</amendment>
</section>

产生所需的正确结果:
<section>
<heading>some heading text</heading>
<newwrapper>
<amendment chapter="1">
<foo/>
</amendment>
<amendment chapter="2">
<bar/>
</amendment>
</newwrapper>
<amendment chapter="3">
<baz/>
</amendment>
<heading>some heading text</heading>
<newwrapper>
<amendment chapter="1">
<baz/>
</amendment>
</newwrapper>
</section>

解释:

正确使用 xsl:for-each-group属性的 group-adjacent

II。 XSLT 1.0解决方案:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="kFollowing" match="amendment[not(@chapter >2)]" use=
"generate-id(preceding-sibling::*
[not(self::amendment and @chapter &lt;= 2)][1])"/>

<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>


<xsl:template match=
"*[not(self::amendment and @chapter &lt;= 2)
and
following-sibling::*[1][self::amendment and not(@chapter >2)]
]">
<xsl:call-template name="identity"/>
<newwrapper>
<xsl:apply-templates mode="inGroup"
select="key('kFollowing', generate-id())"/>
</newwrapper>
</xsl:template>

<xsl:template match="*" mode="inGroup">
<xsl:call-template name="identity"/>
</xsl:template>
<xsl:template match="amendment[not(@chapter >2)]"/>
</xsl:stylesheet>

当此转换应用于相同的XML文档(如上)时,将产生相同,正确的结果。

解释:

正确使用:
  • 使用和覆盖 identity rule
  • Keys 来定义一组相邻的amendment元素,这些元素的chapter属性不大于2,这是该组前一个兄弟元素的 generate-id() 的函数。
  • 关于xslt - XSLT-如何包装具有特定属性的相邻元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11596564/

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