这是输入XML:
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "students.xsl"?>
<book>
<chapter label="Chapter 1">
<para>Text Text<link role="kwd" linkend="Gloss_1">Term 1</link> Text</para>
<para>Text Text Text<link role="kwd" linkend="Gloss_2">Term 2</link></para>
<section>
<title>Key Terms</title>
<itemizedlist mark="none">
<listitem><para><link role="kwd" linkend="Gloss_1">Term 1</link> Def 1</para></listitem>
<listitem><para><link role="kwd" linkend="Gloss_2">Term 2</link> Def 2</para></listitem>
</itemizedlist>
</section>
</chapter>
<chapter label="Chapter 2">
<para>Text Text<link role="kwd" linkend="Gloss_3">Term 3</link> Text</para>
<para>Text Text Text<link role="kwd" linkend="Gloss_1">Term 1</link></para>
<section>
<title>Key Terms</title>
<itemizedlist mark="none">
<listitem><para><link role="kwd" linkend="Gloss_1">Term 1</link> Def 3</para></listitem>
<listitem><para><link role="kwd" linkend="Gloss_3">Term 3</link> Def 1</para></listitem>
</itemizedlist>
</section>
</chapter>
<chapter label="Chapter 3">
<para>Text Text<link role="kwd" linkend="Gloss_4">Term 4</link> Text</para>
<para>Text Text Text<link role="kwd" linkend="Gloss_2">Term 2</link></para>
<para>Text Text Text<link role="kwd" linkend="Gloss_5">Term 5</link></para>
<section>
<title>Key Terms</title>
<itemizedlist mark="none">
<listitem><para><link role="kwd" linkend="Gloss_2">Term 2</link> Def 2</para></listitem>
<listitem><para><link role="kwd" linkend="Gloss_4">Term 4</link> Def 4</para></listitem>
<listitem><para><link role="kwd" linkend="Gloss_5">Term 5</link> Def 5</para></listitem>
</itemizedlist>
</section>
</chapter>
<glossary>
<glossentry xml:id="Gloss_1"><glossterm>Term 1</glossterm><glossdef>Def 1</glossdef></glossentry>
<glossentry xml:id="Gloss_2"><glossterm>Term 2</glossterm><glossdef>Def 2</glossdef></glossentry>
<glossentry xml:id="Gloss_3"><glossterm>Term 3</glossterm><glossdef>Def 3</glossdef></glossentry>
<glossentry xml:id="Gloss_4"><glossterm>Term 4</glossterm><glossdef>Def 4</glossdef></glossentry>
<glossentry xml:id="Gloss_5"><glossterm>Term 5</glossterm><glossdef>Def 5</glossdef></glossentry>
</glossary>
</book>
输出为:
<?xml version="1.0" encoding="utf-8"?>
<glossary>
<row>
<col1>Chapter 1</col1>
<col1>Chapter 2</col1>
<col2>Term 1</col2>
</row>
<row>
<col1>Chapter 1</col1>
<col1>Chapter 3</col1>
<col2>Term 2</col2>
</row>
<row>
<col1>Chapter 2</col1>
<col2>Term 3</col2>
</row>
<row>
<col1>Chapter 3</col1>
<col2>Term 4</col2>
</row>
<row>
<col1>Chapter 3</col1>
<col2>Term 5</col2>
</row>
</glossary>
我的代码是:
<xsl:result-document href="out.xml">
<glossary>
<xsl:for-each select="book/glossary/glossentry">
<row>
<xsl:for-each select="key('num', @xml:id)">
<col1>
<xsl:value-of select="ancestor::chapter/@label"/>
</col1>
</xsl:for-each>
<col2><xsl:value-of select="glossterm"/></col2>
</row>
</xsl:for-each>
</glossary>
</xsl:result-document>
词汇表项目在书中列出了两次-每章末尾列出一项,末尾列出合并项。我想获得本书结尾处列出的每个词汇表术语的章号。我尝试了各种方法,但无法获得祖先元素的独特价值。有人可以帮忙吗?
答案 0 :(得分:2)
我认为您可以简单地改变观点:
<xsl:key name="chapter-by-link" match="chapter" use="descendant::link/@linkend" />
<xsl:template match="/">
<!-- other stuff -->
<glossary>
<xsl:for-each select="book/glossary/glossentry">
<row>
<xsl:for-each select="key('chapter-by-link', @xml:id)">
<col1>
<xsl:value-of select="@label"/>
</col1>
</xsl:for-each>
<col2>
<xsl:value-of select="glossterm"/>
</col2>
</row>
</xsl:for-each>
</glossary>
</xsl:template>
答案 1 :(得分:1)
我想选择ancestor::chapter/@label
属性就足够了,因为通过任何选择步骤的节点都可以消除重复项,因此您只需更改
<xsl:for-each select="key('num', @xml:id)">
<col1>
<xsl:value-of select="ancestor::chapter/@label"/>
</col1>
</xsl:for-each>
到
<xsl:for-each select="key('num', @xml:id)/ancestor::chapter/@label">
<col1>
<xsl:value-of select="."/>
</col1>
</xsl:for-each>
或者将代码组织到小的模板中以获得更简洁的代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">
<xsl:output indent="yes"/>
<xsl:key name="ref" match="link[@role = 'kwd']" use="@linkend"/>
<xsl:template match="book">
<glossary>
<xsl:apply-templates select="glossary/glossentry"/>
</glossary>
</xsl:template>
<xsl:template match="glossentry">
<row>
<xsl:apply-templates select="key('ref', @xml:id)/ancestor::chapter/@label"/>
<col2>
<xsl:value-of select="glossterm"/>
</col2>
</row>
</xsl:template>
<xsl:template match="chapter/@label">
<col1>
<xsl:value-of select="."/>
</col1>
</xsl:template>
</xsl:stylesheet>