3

我正在尝试修改其他人编写的 xsl 表。但现在我只是想理解......我真的很惊讶读到这个:

<xsl:template match="par">

    [...]

    <xsl:variable name="pos"><xsl:value-of select="position()"/></xsl:variable>
    <xsl:variable name="page"><xsl:value-of select="../par[position()=$pos]/@page"/></xsl:variable>

    [...]

    <xsl:if test="$page!=''">
    <xsl:if test="$page!=@page">
        <div style="text-align:right;page-break-before:always">
        <font color="#000000" style="font-style:normal;text-decoration:none;font-weight:normal">
                - <xsl:value-of select="$page"/> -
            <br/>
         </font>
        </div>
    </xsl:if>
    </xsl:if>

    [...]

</xsl:template>

我是愚蠢的还是这段代码愚蠢的?

如果是我,有人可以解释一下在这种情况下 $page 和 @page 会有什么不同吗?

谢谢

4

2 回答 2

4

$page是的,和的字符串值@page可以不同:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/*">
      <xsl:apply-templates select="*[@page mod 2 = 0]"/>
    </xsl:template>

    <xsl:template match="par">      
        <xsl:variable name="pos">
            <xsl:value-of select="position()"/>
        </xsl:variable>

        <xsl:variable name="page">
            <xsl:value-of select="../par[position()=$pos]/@page"/>
        </xsl:variable>

        <xsl:if test="$page!=''">
            <xsl:if test="$page!=@page">
                <div style="text-align:right;page-break-before:always">
                    <font color="#000000" style="font-style:normal;text-decoration:none;font-weight:normal">                 - 
                        <xsl:value-of select="$page"/> -             
                        <br/>
                    </font>
                </div>
            </xsl:if>
        </xsl:if>  
    </xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时:

<t>
 <par page="1"/>
 <par page="2"/>
 <par page="3"/>
 <par page="4"/>
 <par page="5"/>
 <par page="6"/>
 <par page="7"/>
</t>

产生的结果都是由于上面代码中$page@page的不等式:

<div style="text-align:right;page-break-before:always">
   <font color="#000000" style="font-style:normal;text-decoration:none;font-weight:normal">                 - 
                        1 -             
                        <br/>
   </font>
</div>
<div style="text-align:right;page-break-before:always">
   <font color="#000000" style="font-style:normal;text-decoration:none;font-weight:normal">                 - 
                        2 -             
                        <br/>
   </font>
</div>
<div style="text-align:right;page-break-before:always">
   <font color="#000000" style="font-style:normal;text-decoration:none;font-weight:normal">                 - 
                        3 -             
                        <br/>
   </font>
</div>

说明

在模板或 中xsl:for-each,表达式:

position()

当前节点当前节点列表中的位置。这通常与当前节点在其同名兄弟节点集中的位置不同。

于 2012-08-14T12:02:01.327 回答
1

position()用于设置的“独立”函数非常依赖于上下文,并且在设置变量时与方括号内$pos的计算方式不同。position()$page

考虑一个例子(在这里测试http://xslttest.appspot.com/):

源 XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Greatest Hits</title>
        <artist>Dolly Parton</artist>
        <country>USA</country>
        <company>RCA</company>
        <price>9.90</price>
        <year>1982</year>
    </cd>
    <cd>
        <title>Still got the blues</title>
        <artist>Gary Moore</artist>
        <country>UK</country>
        <company>Virgin records</company>
        <price>10.20</price>
        <year>1990</year>
    </cd>
    <cd>
        <title>Eros</title>
        <artist>Eros Ramazzotti</artist>
        <country>EU</country>
        <company>BMG</company>
        <price>9.90</price>
        <year>1997</year>
    </cd>
    <cd>
        <title>One night only</title>
        <artist>Bee Gees</artist>
        <country>UK</country>
        <company>Polydor</company>
        <price>10.90</price>
        <year>1998</year>
    </cd>
    <cd>
        <title>Sylvias Mother</title>
        <artist>Dr.Hook</artist>
        <country>UK</country>
        <company>CBS</company>
        <price>8.10</price>
        <year>1973</year>
    </cd>
    <cd>
        <title>Maggie May</title>
        <artist>Rod Stewart</artist>
        <country>UK</country>
        <company>Pickwick</company>
        <price>8.50</price>
        <year>1990</year>
    </cd>
    <cd>
        <title>Romanza</title>
        <artist>Andrea Bocelli</artist>
        <country>EU</country>
        <company>Polydor</company>
        <price>10.80</price>
        <year>1996</year>
    </cd>
    <cd>
        <title>When a man loves a woman</title>
        <artist>Percy Sledge</artist>
        <country>USA</country>
        <company>Atlantic</company>
        <price>8.70</price>
        <year>1987</year>
    </cd>
    <cd>
        <title>Black angel</title>
        <artist>Savage Rose</artist>
        <country>EU</country>
        <company>Mega</company>
        <price>10.90</price>
        <year>1995</year>
    </cd>
    <cd>
        <title>1999 Grammy Nominees</title>
        <artist>Many</artist>
        <country>USA</country>
        <company>Grammy</company>
        <price>10.20</price>
        <year>1999</year>
    </cd>
    <cd>
        <title>For the good times</title>
        <artist>Kenny Rogers</artist>
        <country>UK</country>
        <company>Mucik Master</company>
        <price>8.70</price>
        <year>1995</year>
    </cd>
    <cd>
        <title>Big Willie style</title>
        <artist>Will Smith</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>9.90</price>
        <year>1997</year>
    </cd>
    <cd>
        <title>Tupelo Honey</title>
        <artist>Van Morrison</artist>
        <country>UK</country>
        <company>Polydor</company>
        <price>8.20</price>
        <year>1971</year>
    </cd>
    <cd>
        <title>Soulsville</title>
        <artist>Jorn Hoel</artist>
        <country>Norway</country>
        <company>WEA</company>
        <price>7.90</price>
        <year>1996</year>
    </cd>
    <cd>
        <title>The very best of</title>
        <artist>Cat Stevens</artist>
        <country>UK</country>
        <company>Island</company>
        <price>8.90</price>
        <year>1990</year>
    </cd>
    <cd>
        <title>Stop</title>
        <artist>Sam Brown</artist>
        <country>UK</country>
        <company>A and M</company>
        <price>8.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Bridge of Spies</title>
        <artist>T`Pau</artist>
        <country>UK</country>
        <company>Siren</company>
        <price>7.90</price>
        <year>1987</year>
    </cd>
    <cd>
        <title>Private Dancer</title>
        <artist>Tina Turner</artist>
        <country>UK</country>
        <company>Capitol</company>
        <price>8.90</price>
        <year>1983</year>
    </cd>
    <cd>
        <title>Midt om natten</title>
        <artist>Kim Larsen</artist>
        <country>EU</country>
        <company>Medley</company>
        <price>7.80</price>
        <year>1983</year>
    </cd>
    <cd>
        <title>Pavarotti Gala Concert</title>
        <artist>Luciano Pavarotti</artist>
        <country>UK</country>
        <company>DECCA</company>
        <price>9.90</price>
        <year>1991</year>
    </cd>
    <cd>
        <title>The dock of the bay</title>
        <artist>Otis Redding</artist>
        <country>USA</country>
        <company>Atlantic</company>
        <price>7.90</price>
        <year>1987</year>
    </cd>
    <cd>
        <title>Picture book</title>
        <artist>Simply Red</artist>
        <country>EU</country>
        <company>Elektra</company>
        <price>7.20</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Red</title>
        <artist>The Communards</artist>
        <country>UK</country>
        <company>London</company>
        <price>7.80</price>
        <year>1987</year>
    </cd>
    <cd>
        <title>Unchain my heart</title>
        <artist>Joe Cocker</artist>
        <country>USA</country>
        <company>EMI</company>
        <price>8.20</price>
        <year>1987</year>
    </cd>
</catalog>

测试 XSL:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="cd">
     [<xsl:value-of select="position()"/>]<xsl:value-of select="title"/>
    <xsl:variable name="pos"><xsl:value-of select="position()"/></xsl:variable>
     [- <xsl:value-of select="$pos"/>]
     [+ <xsl:value-of select="../cd[position()=$pos]/position()"/>]<xsl:value-of select="../cd[position()=$pos]/title"/>
</xsl:template>
</xsl:stylesheet>

结果:

     [2]Empire Burlesque
     [- 2]
     [+ 1]Hide your heart

     [4]Hide your heart
     [- 4]
     [+ 1]Still got the blues

     [6]Greatest Hits
     [- 6]
     [+ 1]One night only

     [8]Still got the blues
     [- 8]
     [+ 1]Maggie May

     [10]Eros
     [- 10]
     [+ 1]When a man loves a woman

     [12]One night only
     [- 12]
     [+ 1]1999 Grammy Nominees

     [14]Sylvias Mother
     [- 14]
     [+ 1]Big Willie style

     [16]Maggie May
     [- 16]
     [+ 1]Soulsville

     [18]Romanza
     [- 18]
     [+ 1]Stop

     [20]When a man loves a woman
     [- 20]
     [+ 1]Private Dancer

     [22]Black angel
     [- 22]
     [+ 1]Pavarotti Gala Concert

     [24]1999 Grammy Nominees
     [- 24]
     [+ 1]Picture book

     [26]For the good times
     [- 26]
     [+ 1]Unchain my heart

     [28]Big Willie style
     [- 28]
     [+ ]

     [30]Tupelo Honey
     [- 30]
     [+ ]

     [32]Soulsville
     [- 32]
     [+ ]

     [34]The very best of
     [- 34]
     [+ ]

     [36]Stop
     [- 36]
     [+ ]

     [38]Bridge of Spies
     [- 38]
     [+ ]

     [40]Private Dancer
     [- 40]
     [+ ]

     [42]Midt om natten
     [- 42]
     [+ ]

     [44]Pavarotti Gala Concert
     [- 44]
     [+ ]

     [46]The dock of the bay
     [- 46]
     [+ ]

     [48]Picture book
     [- 48]
     [+ ]

     [50]Red
     [- 50]
     [+ ]

     [52]Unchain my heart
     [- 52]
     [+ ]
于 2012-08-14T07:35:08.510 回答