XSLT:一次循环选择两个元素
我有一堆xml文档,作者选择在这些文档中表示一组笛卡尔点,如下所示:XSLT:一次循环选择两个元素,xslt,Xslt,我有一堆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. 这将等于点(0,0)和(1,1) 我想
<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.
这将等于点(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>
...
...
一定有更好的办法吗?
总而言之,我想创建像
这样的元素,其中x和y是偶数/奇数索引列元素。确保有一种通用方法:
<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>