gpt4 book ai didi

XSLT:一次循环选择两个元素

转载 作者:行者123 更新时间:2023-12-04 11:32:34 25 4
gpt4 key购买 nike

我有一堆 xml 文档,作者选择在其中表示一组像这样的笛卡尔点:

<row index="0">
<col index="0">0</col>
<col index="1">0</col>
<col index="2">1</col>
<col index="3">1</col>
</row>

这将等于点 (0,0) 和 (1,1)。

我想将其重写为
<set>
<point x="0" y="0"/>
<point x="1" y="1"/>
</set>

但是,我无法弄清楚如何在 XSLT 中创建它,除了对每种可能的情况进行硬编码 - 例如对于 4 点集:
<set>
<point>
<xsl:attribute name="x"><xsl:value-of select="col[@index = 0]"/></xsl:attribute>
<xsl:attribute name="y"><xsl:value-of select="col[@index = 1]"/></xsl:attribute>
</point>
<point>
<xsl:attribute name="x"><xsl:value-of select="col[@index = 1]"/></xsl:attribute>
<xsl:attribute name="y"><xsl:value-of select="col[@index = 2]"/></xsl:attribute>
</point>
...

必须有更好的方法来做到这一点?
总而言之,我想创建像 <point x="..." y="..."/> 这样的元素,其中 x 和 y 是偶数/奇数索引 col元素。

最佳答案

当然有一个通用的方法:

<xsl:stylesheet 
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>

<xsl:template match="row">
<set>
<xsl:apply-templates select="
col[position() mod 2 = 1 and following-sibling::col]
" />
</set>
</xsl:template>

<xsl:template match="col">
<point x="{text()}" y="{following-sibling::col[1]/text()}" />
</xsl:template>

</xsl:stylesheet>

我的输出:
<set>
<point x="0" y="0" />
<point x="1" y="1" />
</set>

关于XSLT:一次循环选择两个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1131866/

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