XSLT: Loop selecting two elements at a time

后端 未结 1 1222
北海茫月
北海茫月 2021-01-18 05:45

I have a bunch of xml documents where the author chose to represent a set of cartesian points like this:


  0&l         


        
相关标签:
1条回答
  • 2021-01-18 06:26

    Sure there is a generic way:

    <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>
    

    Output for me:

    <set>
      <point x="0" y="0" />
      <point x="1" y="1" />
    </set>
    
    0 讨论(0)
提交回复
热议问题