XML/XSLT stylesheet/xlink — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 11:26, 26 мая 2010
Содержание
Construct xlink
<source lang="xml">
File: Data.xml <recipe xmlns:xlink="http://www.w3.org/1999/xlink">
<author xlink:href="http:/www.jcookie.ru" xlink:type="simple"> name </author> <ingredients> <ingredient xlink:href="http:/www.wbex.ru" xlink:type="simple"> flour </ingredient> <ingredient xlink:href="http:/www.wbex.ru" xlink:type="simple"> sugar </ingredient> </ingredients> <steps />
</recipe>
File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xlink="http://www.w3.org/1999/xlink" exclude-result-prefixes="xlink" version="1.0"> <xsl:output method="html" /> <xsl:template match="*[@xlink:type = "simple" and @xlink:href]"> <a href="{@xlink:href}"> <xsl:apply-templates /> </a> </xsl:template> <xsl:template match="recipe"> <html> <body> <xsl:apply-templates /> </body> </html> </xsl:template>
</xsl:stylesheet> Output: <html>
<body> <a href="http:/www.jcookie.ru"> name </a> <a href="http:/www.wbex.ru"> flour </a> <a href="http:/www.wbex.ru"> sugar </a> </body>
</html>
</source>
Create simple xlink
<source lang="xml">
File: Data.xml <html>
<body>
The poem"s <a href="jmilton.html">author</a> was English.
</body>
</html> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xlink="http://www.w3.org/1999/xlink" exclude-result-prefixes="xlink" version="1.0"> <xsl:output method="xml" omit-xml-declaration="yes" /> <xsl:template match="a"> <author xlink:type="simple" xlink:href="{@href}"> <xsl:apply-templates /> </author> </xsl:template> <xsl:template match="p"> <para> <xsl:apply-templates /> </para> </xsl:template>
</xsl:stylesheet> Output:
<para> The poem"s <author xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="jmilton.html">author</author> was English. </para> </source>
create xlink in style sheet
<source lang="xml">
File: Data.xml <html>
<body>
Here is the <a href="a.html">author</a>
</body>
</html> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xlink="http://www.w3.org/1999/xlink" version="1.0"> <xsl:output method="xml" omit-xml-declaration="yes" /> <xsl:template match="a"> <author xlink:type="simple" xlink:href="{@href}"> <xsl:apply-templates /> </author> </xsl:template> <xsl:template match="p"> <para> <xsl:apply-templates /> </para> </xsl:template>
</xsl:stylesheet>
Output:
<para xmlns:xlink="http://www.w3.org/1999/xlink"> Here is the <author xlink:type="simple" xlink:href="a.html">author</author> </para> </source>
Use generate-id() as link
<source lang="xml">
File: Data.xml <story>
<chapter> <title>Chapter 1</title> <para>para 1</para> <para>item 1</para> </chapter> <chapter> <title>Chapter 2</title> <para>item 2</para> <para>For while they sit contriving, shall the rest</para> </chapter> <chapter> <title>Chapter 3</title> <para>para 2</para> <para>para A</para> </chapter>
</story>
File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"> <xsl:output method="html" /> <xsl:template match="story"> <html> <body>
Table of Contents
<xsl:apply-templates select="chapter/title" mode="toc" /> <xsl:apply-templates /> </body> </html> </xsl:template> <xsl:template match="chapter/title">
<a name="{generate-id()}" /> <xsl:apply-templates />
</xsl:template> <xsl:template match="chapter/title" mode="toc">
<a href="#{generate-id()}"> <xsl:apply-templates /> </a>
</xsl:template> <xsl:template match="para">
<xsl:apply-templates />
</xsl:template> <xsl:template match="story/title">
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet> Output: <html>
<body>
Table of Contents
<a href="#d2e5">Chapter 1</a>
<a href="#d2e17">Chapter 2</a>
<a href="#d2e29">Chapter 3</a>
<a name="d2e5"></a>Chapter 1
para 1
item 1
<a name="d2e17"></a>Chapter 2
item 2
For while they sit contriving, shall the rest
<a name="d2e29"></a>Chapter 3
para 2
para A
</body>
</html>
</source>