1

XML を HTML に変換しているときに、xref が参照する章要素から引き出された自動生成された章番号を持つリンクとして xref 要素を出力しようとしています。

たとえば、次のような 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>

2 つの章があり、xref が 2 番目の章を参照している場合、生成された HTML の xref は次のように出力されます。

<section id="a_chapter">
  <h1>Title</h1>
  <p>...as seen in <a href="#some_other_chapter">Chapter 2</a>.</p>
</section>

しかし、xref @linkend が参照する章要素を数える方法がわかりません。xsl:number を使用してみましたが、カウント内で 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>

また、xsl:number カウントの値として「chapter」だけを使用しようとしましたが、すべての出力で「Chapter 0」が生成されました。

私はここから離れているのでしょうか、それとも愚かなxpathの間違いを犯しているだけですか? どんな助けでも大歓迎です。

4

2 に答える 2

0

最も簡単な方法は、モデレートされたテンプレートを使用しchapterてラベルを作成することです。

小さな例...

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>

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 <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>
于 2013-07-29T22:29:08.257 に答える