XML/XSLT stylesheet/generate id — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 08:26, 26 мая 2010
Create id with generate-id()
File: Data.xml
<resorts>
<resort>
<name>A</name>
<details>B</details>
<hotel>
<name>C</name>
<stars>5</stars>
<address> . . . </address>
</hotel>
<hotel>
<name>L</name>
<stars>2</stars>
<address> . . . </address>
</hotel>
</resort>
<resort>
<name>D</name>
<details>E</details>
<hotel>
<name>W</name>
<stars>5</stars>
<address> . . . </address>
</hotel>
<hotel>
<name>P</name>
<stars>2</stars>
<address> . . . </address>
</hotel>
</resort>
</resorts>
File: Transform.xslt
<html xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xsl:version="1.0">
<body>
<h1>Hotels</h1>
<xsl:for-each select="//hotel">
<xsl:sort select="number(stars)" order="descending"
data-type="number" />
<h2>
<xsl:value-of select="name" />
</h2>
<p>
Address:
<xsl:value-of select="address" />
</p>
<p>
Stars:
<xsl:value-of select="stars" />
</p>
<p>
Resort:
<a href="#{generate-id(parent::resort)}">
<xsl:value-of select="parent::resort/name" />
</a>
</p>
</xsl:for-each>
<h1>Resorts</h1>
<xsl:for-each select="//resort">
<h2>
<a name="{generate-id()}">
<xsl:value-of select="name" />
</a>
</h2>
<p>
<xsl:value-of select="details" />
</p>
</xsl:for-each>
</body>
</html>
Output:
<html>
<body>
<h1>Hotels</h1>
<h2>C</h2>
<p>
Address:
. . .
</p>
<p>
Stars:
5
</p>
<p>
Resort:
<a href="#d3e3">A</a></p>
<h2>W</h2>
<p>
Address:
. . .
</p>
<p>
Stars:
5
</p>
<p>
Resort:
<a href="#d3e36">D</a></p>
<h2>L</h2>
<p>
Address:
. . .
</p>
<p>
Stars:
2
</p>
<p>
Resort:
<a href="#d3e3">A</a></p>
<h2>P</h2>
<p>
Address:
. . .
</p>
<p>
Stars:
2
</p>
<p>
Resort:
<a href="#d3e36">D</a></p>
<h1>Resorts</h1>
<h2><a name="d3e3">A</a></h2>
<p>B</p>
<h2><a name="d3e36">D</a></h2>
<p>E</p>
</body>
</html>
generate-id() function
File: Data.xml
<verse color="red">
<amount>5</amount>
</verse>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
<xsl:template match="verse">
<line status="done" hue="{@color}" number="{amount}"
sourceElement="src{generate-id()}" />
</xsl:template>
</xsl:stylesheet>
Output:
<line status="done" hue="red" number="5" sourceElement="srcd2e1"/>
Get value out of generate-id() with value-of
File: Data.xml
<verse color="red">
<amount>5</amount>
</verse>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
<xsl:template match="verse">
<xsl:element name="line">
<xsl:attribute name="status">done</xsl:attribute>
<xsl:attribute name="hue">
<xsl:value-of select="@color" />
</xsl:attribute>
<xsl:attribute name="number">
<xsl:value-of select="amount" />
</xsl:attribute>
<xsl:attribute name="sourceElement">
<xsl:text>src</xsl:text>
<xsl:value-of select="generate-id()" />
</xsl:attribute>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Output:
<line status="done" hue="red" number="5" sourceElement="srcd2e1"/>