Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt XSL:number count中的XSL id()函数_Xslt - Fatal编程技术网

Xslt XSL:number count中的XSL id()函数

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>

在将XML转换为HTML时,我尝试将外部参照元素作为链接输出,该链接具有自动生成的章节号,该章节号是从外部参照所引用的章节元素中提取的

例如,使用如下XML源文件:

<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解决方案适合我。非常感谢!