Thanks Michael,
Does this mean I can't find an ancestor value via the key function or my syntax is incorrect?
Here is some sample code:
input:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<chapter id="chapter1">
<sec1>
<para id="para1">This is a para1</para>
</sec1>
</chapter>
<chapter id="a2">
<sec1 id="chap2sec1">
<title>Chapter 2 ,section 2 Title</title>
<para>This is a para2</para>
</sec1>
</chapter>
<chapter id="a3">
<sec1>
<para>This is a para3, see a reference for <xref href="para1">Here!</xref>
</para>
</sec1>
</chapter>
<chapter id="a4">
<sec1 id="sec1chap4">
<title>Chapter 4 ,section 1 Title</title>
<para id="para4">This is a para4 <bold>BB</bold>
</para>
</sec1>
</chapter>
<chapter id="a5">
<sec1>
<para>This is a para5 see a reference for <xref href="para4">Here!</xref>
</para>
</sec1>
</chapter>
</root>
Here is a sample xslt:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="fn xs">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="para-key" match="para" use="@id"/>
<xsl:key name="chapter-key" match="chapter" use="descendant::*/@id"/>
<xsl:key name="section-key" match="sec1" use="descendant::*/@id"/>
<xsl:template match="/">
<root>
<xsl:for-each select="/root/chapter">
<new>
<xsl:for-each select="sec1">
<section>
<xsl:apply-templates/>
</section>
</xsl:for-each>
</new>
</xsl:for-each>
</root>
</xsl:template>
<xsl:template match="para">
<para>
<xsl:apply-templates/>
</para>
</xsl:template>
<xsl:template match="title">
<title>
<xsl:apply-templates/>
</title>
</xsl:template>
<xsl:template match="xref">
<xsl:text>Found para </xsl:text>
<xsl:value-of select="key('para-key', @href)"/>
<xsl:text> In chapter </xsl:text>
<xsl:number level="any" count="chapter" select="key('chapter-key', @href)"/>
<xsl:text> </xsl:text>
<xsl:value-of select="key('section-key', @href)/ancestor::sec1/title"/>
</xsl:template>
</xsl:stylesheet>