XSLT 增量变量 [英] XSLT increment variable

查看:30
本文介绍了XSLT 增量变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 XML

<块引用>

<记录><记录名称="一条记录"><info>A1</info><info>A2</info></记录><记录名称=B记录"/><记录名称=C记录"><info>C1</info></记录></记录></数据>

如何转换为以下输出,问题是如何在记录和记录/信息之间进行计数?

<p>A记录</p><span id="1">A1</span><span id="2">A2</span>

<div id="2"><p>C记录</p><span id="3">C1</span>

解决方案

解决方案 1.细粒度的遍历.此样式表:

<xsl:template match="records"><xsl:apply-templates select="*[1]"/></xsl:模板><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>

<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:模板><xsl:template match="信息"><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:模板><xsl:template match="@name"><p><xsl:value-of select="."/></p></xsl:模板></xsl:stylesheet>

输出:

<p>A记录</p><span id="1">A1</span><span id="2">A2</span>

<div id="2"><p>C记录</p><span id="3">C1</span>

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

<xsl:template match="record"/><xsl:template match="record[node()]"><div id="{count(preceding-sibling::record[node()])+1}"><xsl:apply-templates select="@*|*"/>

</xsl:模板><xsl:template match="信息"><span id="{count(preceding::info)+1}"><xsl:value-of select="."/></span></xsl:模板><xsl:template match="@name"><p><xsl:value-of select="."/></p></xsl:模板></xsl:stylesheet>

解决方案 3:使用 fn:position()preceding 轴.此样式表:

<xsl:template match="records"><xsl:apply-templates select="record[node()]"/></xsl:模板><xsl:模板匹配=记录"><div id="{position()}"><xsl:apply-templates select="@*"/><xsl:apply-templates/>

</xsl:模板><xsl:template match="信息"><span id="{count(preceding::info)+1}"><xsl:value-of select="."/></span></xsl:模板><xsl:template match="@name"><p><xsl:value-of select="."/></p></xsl:模板></xsl:stylesheet>

注意:您需要一个显式的拉取样式.

编辑:缺少 span/@id 的任何级别编号.

I have the following 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>

how can I transform into following output, the problem is how can I count between on record, and record/info?

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

解决方案

Solution 1. Fine grained traversal. This stylesheet:

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

Output:

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

Solution 2: preceding axe. This stylesheet:

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

Solution 3: With fn:position() and preceding axe. This stylesheet:

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

Note: You need a explict pull style.

Edit: Missed any level numbering for span/@id.

这篇关于XSLT 增量变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆