Xslt XSL:number count中的XSL id()函数
在将XML转换为HTML时,我尝试将外部参照元素作为链接输出,该链接具有自动生成的章节号,该章节号是从外部参照所引用的章节元素中提取的 例如,使用如下XML源文件:Xslt XSL:number count中的XSL id()函数,xslt,Xslt,在将XML转换为HTML时,我尝试将外部参照元素作为链接输出,该链接具有自动生成的章节号,该章节号是从外部参照所引用的章节元素中提取的 例如,使用如下XML源文件: <chapter id="a_chapter"> <title>Title</title> <para>...as seen in <xref linkend="some_other_chapter">.</para> </chapter>
<chapter id="a_chapter">
<title>Title</title>
<para>...as seen in <xref linkend="some_other_chapter">.</para>
</chapter>
<chapter id="some_other_chapter">
<title>Title</title>
<para>Some text here.</para>
</chapter>
<section id="a_chapter">
<h1>Title</h1>
<p>...as seen in <a href="#some_other_chapter">Chapter 2</a>.</p>
</section>
标题
…如图所示。
标题
这里有一些文字。
如果有两章且外部参照引用第二章,则生成的HTML中的外部参照应如下输出:
<chapter id="a_chapter">
<title>Title</title>
<para>...as seen in <xref linkend="some_other_chapter">.</para>
</chapter>
<chapter id="some_other_chapter">
<title>Title</title>
<para>Some text here.</para>
</chapter>
<section id="a_chapter">
<h1>Title</h1>
<p>...as seen in <a href="#some_other_chapter">Chapter 2</a>.</p>
</section>
标题
…如图所示
但我不知道如何计算xref@linkend所指的chapter元素。我尝试使用xsl:number,但无法在count中使用id()函数:
<xsl:template match="xref">
<xsl:variable name="label">
<xsl:when test="id(@linkend)[self::chapter]"><xsl:text>Chapter </xsl:text></xsl:when>
</xsl:variable
<xsl:variable name="count">
<xsl:if test="id(@linkend)[self::chapter]"><xsl:number count="id(@linkend)/chapter" level="any" format="1"/></xsl:if>
</xsl:variable>
<a href="#{@linkend}">
<xsl:value-of select="$label"/><xsl:value-of select="$count"/>
</a>
</xsl:template>
章
最简单的方法可能是为章节
使用一个模版来制作标签
小例子
XML输入
<doc>
<chapter id="a_chapter">
<title>Title</title>
<para>...as seen in <xref linkend="some_other_chapter"/>.</para>
</chapter>
<chapter id="some_other_chapter">
<title>Title</title>
<para>Some text here.</para>
</chapter>
</doc>
<doc>
<chapter id="a_chapter">
<title>Title</title>
<para>...as seen in <a href="#some_other_chapter">Chapter 2</a>.</para>
</chapter>
<chapter id="some_other_chapter">
<title>Title</title>
<para>Some text here.</para>
</chapter>
</doc>
标题
…如图所示。
标题
这里有一些文字。
XSLT1.0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="xref">
<a href="#{@linkend}">
<xsl:apply-templates select="/*/chapter[@id=current()/@linkend]" mode="label"/>
</a>
</xsl:template>
<xsl:template match="chapter" mode="label">
<xsl:text>Chapter </xsl:text>
<xsl:number/>
</xsl:template>
</xsl:stylesheet>
章
输出
<doc>
<chapter id="a_chapter">
<title>Title</title>
<para>...as seen in <xref linkend="some_other_chapter"/>.</para>
</chapter>
<chapter id="some_other_chapter">
<title>Title</title>
<para>Some text here.</para>
</chapter>
</doc>
<doc>
<chapter id="a_chapter">
<title>Title</title>
<para>...as seen in <a href="#some_other_chapter">Chapter 2</a>.</para>
</chapter>
<chapter id="some_other_chapter">
<title>Title</title>
<para>Some text here.</para>
</chapter>
</doc>
标题
…如图所示。
标题
这里有一些文字。
最简单的方法是为章节
使用模版制作标签
小例子
XML输入
<doc>
<chapter id="a_chapter">
<title>Title</title>
<para>...as seen in <xref linkend="some_other_chapter"/>.</para>
</chapter>
<chapter id="some_other_chapter">
<title>Title</title>
<para>Some text here.</para>
</chapter>
</doc>
<doc>
<chapter id="a_chapter">
<title>Title</title>
<para>...as seen in <a href="#some_other_chapter">Chapter 2</a>.</para>
</chapter>
<chapter id="some_other_chapter">
<title>Title</title>
<para>Some text here.</para>
</chapter>
</doc>
标题
…如图所示。
标题
这里有一些文字。
XSLT1.0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="xref">
<a href="#{@linkend}">
<xsl:apply-templates select="/*/chapter[@id=current()/@linkend]" mode="label"/>
</a>
</xsl:template>
<xsl:template match="chapter" mode="label">
<xsl:text>Chapter </xsl:text>
<xsl:number/>
</xsl:template>
</xsl:stylesheet>
章
输出
<doc>
<chapter id="a_chapter">
<title>Title</title>
<para>...as seen in <xref linkend="some_other_chapter"/>.</para>
</chapter>
<chapter id="some_other_chapter">
<title>Title</title>
<para>Some text here.</para>
</chapter>
</doc>
<doc>
<chapter id="a_chapter">
<title>Title</title>
<para>...as seen in <a href="#some_other_chapter">Chapter 2</a>.</para>
</chapter>
<chapter id="some_other_chapter">
<title>Title</title>
<para>Some text here.</para>
</chapter>
</doc>
标题
…如图所示。
标题
这里有一些文字。
在XSLT 1.0中,在调用
之前更改上下文:
在XSLT1.0中,在调用
之前更改上下文:
我认为你使用count
错误。请提供适当的解决方案,例如,我认为您错误地使用了count
。请提供适当的解决方案,例如,谢谢您的详细回答。我使用的是XSLT1.0,for-each解决方案适合我。非常感谢!谢谢你详尽的回答。我使用的是XSLT1.0,for-each解决方案适合我。非常感谢!