我正在尝试修改其他人编写的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如何不同?
感谢
答案 0 :(得分: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()
是当前节点列表中当前节点的位置。这通常与其同名兄弟节点的节点集中当前节点的位置不同。
答案 1 :(得分:1)
用于设置position()
的“独立”$pos
函数非常依赖于上下文,当您设置position()
变量时,其计算方式与方括号内的$page
不同。< / p>
考虑一个例子(在这里测试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]
[+ ]