Axis: ancestor |
---|
Element</th><td>Node-set</th> |
</table></source>
Is product the ancestor of "Java"?
<source lang="xml">
File: Data.xml
<?xml version = "1.0"?>
<product>
<books>
<book>
Getting Started with Microsoft Visual C++
</book>
<book>Java</book>
</books>
</product>
File: Transform.xslt
<?xml version = "1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/product">
Is product the ancestor of "Java"?
<xsl:if
test="name(//node()[. = "Java"]/ancestor::product) = "product"">
Yes
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
Is product the ancestor of "Java"?
Yes</source>
select ancestor::*
<source lang="xml">
File: Data.xml
<book>
<chapter>
<title>A</title>
<para>para1</para>
<para>para2</para>
</chapter>
<chapter>
<title>B</title>
<para>line 2</para>
<para>line 3</para>
</chapter>
<afterword>
<para>line 4</para>
</afterword>
<appendix>
<title>The Author</title>
<para>line 5</para>
<para>line 6</para>
</appendix>
</book>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" />
<xsl:template match="para">
Unaccounted for para element
Ancestors
<xsl:for-each select="ancestor::*">
<xsl:value-of select="name()" />
<xsl:if test="position() != last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
Content
<xsl:apply-templates />
</xsl:template>
<xsl:template match="book">
<html>
<body>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="title">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="chapter/para">
<paragraph>
<xsl:apply-templates />
</paragraph>
</xsl:template>
<xsl:template match="appendix/para">
<paragraph>
<xsl:apply-templates />
</paragraph>
</xsl:template>
</xsl:stylesheet></source>
select="ancestor::names/child::name[1]/child::family"
<source lang="xml">
File: Data.xml
<?xml version="1.0"?>
<names>
<name>
<given>A</given>
<family>B</family>
</name>
</names>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="/">
<xsl:apply-templates select="child::names" />
</xsl:template>
<xsl:template match="child::names">
<xsl:apply-templates select="child::name[18]" />
</xsl:template>
<xsl:template match="child::name[18]">
<xsl:value-of
select="ancestor::names/child::name[1]/child::given" />
<xsl:text> </xsl:text>
<xsl:value-of
select="ancestor::names/child::name[1]/child::family" />
<xsl:text> is first on the list, and </xsl:text>
<xsl:value-of select="child::given" />
<xsl:text> </xsl:text>
<xsl:value-of select="child::family" />
<xsl:text> is last.</xsl:text>
</xsl:template>
</xsl:stylesheet></source>