XML/XSLT stylesheet/xlink
Содержание
Construct xlink
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>
Create simple xlink
File: Data.xml
<html>
<body>
<p>
The poem"s
<a href="jmilton.html">author</a>
was English.
</p>
</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>
create xlink in style sheet
File: Data.xml
<html>
<body>
<p>
Here is the
<a href="a.html">author</a>
</p>
</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>
Use generate-id() as link
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>
<h1>Table of Contents</h1>
<xsl:apply-templates select="chapter/title" mode="toc" />
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="chapter/title">
<h2>
<a name="{generate-id()}" />
<xsl:apply-templates />
</h2>
</xsl:template>
<xsl:template match="chapter/title" mode="toc">
<p>
<a href="#{generate-id()}">
<xsl:apply-templates />
</a>
</p>
</xsl:template>
<xsl:template match="para">
<p>
<xsl:apply-templates />
</p>
</xsl:template>
<xsl:template match="story/title">
<h1>
<xsl:apply-templates />
</h1>
</xsl:template>
</xsl:stylesheet>
Output:
<html>
<body>
<h1>Table of Contents</h1>
<p><a href="#d2e5">Chapter 1</a></p>
<p><a href="#d2e17">Chapter 2</a></p>
<p><a href="#d2e29">Chapter 3</a></p>
<h2><a name="d2e5"></a>Chapter 1
</h2>
<p>para 1</p>
<p>item 1</p>
<h2><a name="d2e17"></a>Chapter 2
</h2>
<p>item 2</p>
<p>For while they sit contriving, shall the rest</p>
<h2><a name="d2e29"></a>Chapter 3
</h2>
<p>para 2</p>
<p>para A</p>
</body>
</html>