这段XSLT代码是否荒谬?

这段XSLT代码是否荒谬?,xslt,Xslt,我正在尝试修改其他人编写的xsl表。但现在我只是想了解。。。 读到这篇文章我真的很惊讶: <xsl:template match="par"> [...] <xsl:variable name="pos"><xsl:value-of select="position()"/></xsl:variable> <xsl:variable name="page"><xsl:value-of select=".

我正在尝试修改其他人编写的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有什么不同吗?

谢谢

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

考虑一个示例(此处测试):

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

是,
$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>
<t>
 <par page="1"/>
 <par page="2"/>
 <par page="3"/>
 <par page="4"/>
 <par page="5"/>
 <par page="6"/>
 <par page="7"/>
</t>
说明

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

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

这可能是一个很好的问题,因为在
[…]
中不可能回答这个问题
@page
是当前节点的
page
属性
$page
$pos
-th
par
节点的
page
属性。为什么你认为它们应该相等?@hroptatyr和$pos是当前位置的最大值?我不知道
[…]
位中发生了什么。您可以更改当前节点。上面的
$pos
是当前节点的位置,是的。但该节点不必是
par
节点。这个问题似乎离题了,因为它是关于XSL编码样式的。感谢您的示例,我现在理解了“上下文相关”的含义。但在您的示例中,[x]始终等于[-x]正确。我只是检查变量是否设置正确。这个输出实际上并不需要。重要的是
position()。在“独立”版本中,它统计所有XML元素(在我的示例中),在方括号中
。/cd[position()=$pos]
它只统计
cd
元素。+1用于解释
position()
根据使用位置更改其函数的方式。@ABach,欢迎您。
<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>
position()