简体   繁体   中英

XSLT adding a tr at nth position while transforming the XML node with different child nodes

I am new to XSLT, and I am trying to transform the below XML. Note that the child nodes will be different

<parent>
 <child1>A</child1>
 <child2>B</child2>
 <child3>C</child3>
 <child4>D</child4>
 <child5>E</child5>
 <child6>F</child6>
 <child7>G</child7>
</parent>

I want transform the above as follows,when n is 3

<tr><td>child1:A</td><td>child2:B</td><td>child3:C</td></tr>
<tr><td>child4:D</td><td>child5:E</td><td>child6:F</td></tr>
<tr><td>child7:G</td></tr>

I tried as follows

<xsl:template match="parent/*[position() mod 4 = 1]">
    <tr>
    <xsl:apply-templates mode="proc"
        select=".|following-sibling::parent/*[not(position() > 3)]"
    />
    </tr>
</xsl:template>
<xsl:template match="parent/*[not(position() mod 4 = 1)]"/>
 <xsl:template match="parent/*" mode="proc">
    <td class="label"><xsl:value-of select ="name(.)"/>:<xsl:value-of select ="."/></td>
 </xsl:template>

The problem is with this line...

<xsl:apply-templates mode="proc"
                     select=".|following-sibling::parent/*[not(position() > 3)]" />

You are in a template matching a child of the parent element ( parent/*) . This means the current node does not have a parent node as a sibling. Change the expression to this

<xsl:apply-templates mode="proc" 
                     select=".|following-sibling::*[not(position() > 3)]" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM