从XSL按位置新建标签

huangapple go评论105阅读模式
英文:

New tag by position from xsl

问题

<xsl:template match="column">
    <xsl:element name="column_{position() + count(../preceding-sibling::row/column)}">
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>
英文:

I have a slightly different question, comparing to this previous one:

 <?xml version="1.0" encoding="UTF-8"?>
 <importoTotaleCommissioni>
    <row>
        <column>5</column>
        <column>6</column>
    </row>
    <row>
        <column>5</column>
        <column>6</column>
        <column>7</column>
        <column>8</column>
    </row>
</importoTotaleCommissioni>

I have to create an output, by xsl, with tags named positionally, for example

<importoTotaleCommissioni>
    <row>
        <column_1>5</column_1>
        <column_2>6</column_2>
    </row>
    <row>
        <column_1>5</column_1>
        <column_2>6</column_2>
        <column_3>7</column_3>
        <column_4>8</column_4>
    </row>
</importoTotaleCommissioni>

The columns enumeration must restart at every row. I have tried using

<xsl:template match="column">
    <xsl:element name="column_{position()}">
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

... but it gives me wrong enumaration. How can I correct this?

答案1

得分: -1

可以使用此模板代替:

<xsl:template match="column">
  <xsl:element name="column_{count(preceding-sibling::column)+1}">
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>
英文:

You could use this template instead :

&lt;xsl:template match=&quot;column&quot;&gt;
  &lt;xsl:element name=&quot;column_{count(preceding-sibling::column)+1}&quot;&gt;
    &lt;xsl:apply-templates/&gt;
  &lt;/xsl:element&gt;
&lt;/xsl:template&gt;

huangapple
  • 本文由 发表于 2023年2月6日 21:49:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75362156.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定