XSLT增量变量

cc9*_*6ai 9 xslt

我有以下XML

<data>
 <records>
   <record name="A record">
     <info>A1</info>
     <info>A2</info>
   </record>
   <record name="B record"/>
   <record name="C record">
     <info>C1</info>
   </record>
  </records>
</data>
Run Code Online (Sandbox Code Playgroud)

我怎样才能转换成以下输出,问题是如何在记录和记录/信息之间进行计数?

<div id="1">
    <p>A record</p>
    <span id="1">A1</span>
    <span id="2">A2</span> 
</div> 
<div id="2">
    <p>C record</p>   
    <span id="3">C1</span> 
</div>
Run Code Online (Sandbox Code Playgroud)

小智 7

解决方案1.细粒度遍历.这个样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="records">
        <xsl:apply-templates select="*[1]"/>
    </xsl:template>
    <xsl:template match="record"/>
    <xsl:template match="record[node()]">
        <xsl:param name="pRecordNum" select="1"/>
        <xsl:param name="pInfoNum" select="1"/>
        <div id="{$pRecordNum}">
            <xsl:apply-templates select="@*|*[1]">
                <xsl:with-param name="pInfoNum" select="$pInfoNum"/>
            </xsl:apply-templates>
        </div>
        <xsl:apply-templates select="following-sibling::record[node()][1]">
            <xsl:with-param name="pRecordNum" select="$pRecordNum +1"/>
            <xsl:with-param name="pInfoNum" select="$pInfoNum + count(info)"/>
        </xsl:apply-templates>
    </xsl:template>
    <xsl:template match="info">
        <xsl:param name="pInfoNum"/>
        <span id="{$pInfoNum}">
            <xsl:value-of select="."/>
        </span>
        <xsl:apply-templates select="following-sibling::info[1]">
            <xsl:with-param name="pInfoNum" select="$pInfoNum +1"/>
        </xsl:apply-templates>
    </xsl:template>
    <xsl:template match="@name">
        <p>
            <xsl:value-of select="."/>
        </p>
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

输出:

<div id="1">
    <p>A record</p>
    <span id="1">A1</span>
    <span id="2">A2</span>
</div>
<div id="2">
    <p>C record</p>
    <span id="3">C1</span>
</div>
Run Code Online (Sandbox Code Playgroud)

解决方案2:preceding斧头.这个样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="record"/>
    <xsl:template match="record[node()]">
        <div id="{count(preceding-sibling::record[node()])+1}">
            <xsl:apply-templates select="@*|*"/>
        </div>
    </xsl:template>
    <xsl:template match="info">
        <span id="{count(preceding::info)+1}">
            <xsl:value-of select="."/>
        </span>
    </xsl:template>
    <xsl:template match="@name">
        <p>
            <xsl:value-of select="."/>
        </p>
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

解决方案3:用fn:position()preceding斧头.这个样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="records">
        <xsl:apply-templates select="record[node()]"/>
    </xsl:template>
    <xsl:template match="record">
        <div id="{position()}">
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates/>
        </div>
    </xsl:template>
    <xsl:template match="info">
        <span id="{count(preceding::info)+1}">
            <xsl:value-of select="."/>
        </span>
    </xsl:template>
    <xsl:template match="@name">
        <p>
            <xsl:value-of select="."/>
        </p>
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

注意:您需要一个明确的拉式.

编辑:错过了span/@ id的任何级别编号.