【问题标题】:Calculating topic number, where went wrong?计算话题号,哪里出错了?
【发布时间】:2023-03-22 21:41:01
【问题描述】:

我想为每个主题标题生成编号。

有1个Ditamap,结构如下

<map>
  <chapter>
     <topicref href="ditafile1#topic1">
     <topicref href="ditafile1#topic2">
     <topicref href="ditafile1#topic3">
     and so forth
  </chapter>

  <chapter>
     <topicref href="ditafile2#topic2-1>
     <topicref href="ditafile2#topic2-2>
     and so forth
  </chapter>
</map>

2个相同结构的Dita文件

Dita 文件 1:

<topic>
      <topic>
          <title>Introduction</title>
      </topic>
      <topic id="topic1>
          <title>Number 1</title>
      </topic>
      <topic id="topic2>
          <title>Number 2</title>
      </topic>
  </topic>

Dita 文件 2

<topic>
      <topic id="topic2-1">
          <title>Number 2-1</title>
      </topic>
      <topic>
          <title></title>
      </topic>
      <topic id="topic2-2">
          <title>Number 2-2</title>
      </topic>
  </topic>

预期结果:

1.1 Number 1

// 1.1 根据匹配的id和href生成

1.2 Number 2

// 1.2 根据匹配的id和href生成

2.1 数字 2-1

2.2 数字 2-2


如您所见,订单不是结构化的。我需要调用ditamap,基于ditamap结构,比较dita主题id和ditamap href#,如果匹配数字标题。

下面是我的代码

在(主题/主题/标题)

<xsl:template match="topic/topic/title">
   <xsl:for-each select="map">
       <xsl:number count="chapter" format="1. "/>

       <xsl:for-each select="document(@href)/chapter/topicref">
           <xsl:number count="chapter|topicref" level="multiple" format="1.1. "></xsl:number>

        // if dita map href matches topic id {
           <h2>
        <xsl:value-of select="."/> <xsl:value-of select="text()"/>
           </h2>             
         }
       </xsl:for-each>           
   </xsl:for-each>

</xsl:template>

谢谢。

【问题讨论】:

  • 如果您能显示一个更完整的输入文件示例将会很有帮助 - 您的 &lt;xsl:for-each select="bookmap"&gt; 行有点令人困惑,因为您的示例中的任何地方都没有任何 &lt;bookmap&gt; 元素已经给出了,更不用说作为topic/topic/title 结构的子元素的&lt;bookmap&gt; 元素了。如果您可以提供一个 XML 格式的预期输出示例以及您的代码生成的任何实际输出示例,这也会很有帮助。
  • 嗨,谢谢。 ditamap 的结构保持不变,只是命名我放错了。是的, 来自 ditamap 文件,而不是 topic/topic/title 的子文件,有什么方法可以调用 ditamap 吗?上面的代码是不完整的,大致是如何实现我想要的想法。
  • 您在使用 DITA-OT 吗?
  • 我正在用 xslt 写作。

标签: xslt


【解决方案1】:

为了澄清前提,我制作了以下地图和主题文件。

[test.ditamap]

<?xml version="1.0" encoding="UTF-8"?>
<map>
    <chapter>
        <topicref href="ditafile1.xml#topic1"/>
        <topicref href="ditafile1.xml#topic2"/>
        <topicref href="ditafile1.xml#topic3"/>
        <!-- and so forth -->
    </chapter>

    <chapter>
        <topicref href="ditafile2.xml#topic2-1"/>
        <topicref href="ditafile2.xml#topic2-2"/>
        <!-- and so forth -->
    </chapter>
</map>

[ditafile1.xml]

<?xml version="1.0" encoding="UTF-8"?>
<topic id="topic0">
    <title/>
    <topic>
        <title>Introduction</title>
    </topic>
    <topic id="topic1">
        <title>Number 1</title>
    </topic>
    <topic id="topic2">
        <title>Number 2</title>
    </topic>
    <topic id="topic3">
        <title>Number 3</title>
    </topic>
</topic>

[ditafile2.xml]

<?xml version="1.0" encoding="UTF-8"?>
<topic>
    <title/>
    <topic id="topic2-1">
        <title>Number 2-1</title>
    </topic>
    <topic>
        <title></title>
    </topic>
    <topic id="topic2-2">
        <title>Number 2-2</title>
    </topic>
</topic>

输入 test.ditamap 并输出文本的样式表。

[convert.xsl]

<?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="xs"
    version="2.0">

    <xsl:output method="text"/>

    <xsl:template match="/">
        <xsl:apply-templates select="//topicref"/>
    </xsl:template>

    <xsl:template match="topicref[@href]">
        <xsl:variable name="href" as="xs:string" select="string(@href)"/>
        <xsl:variable name="topicFile" as="xs:string" select="substring-before($href,'#')"/>
        <xsl:variable name="id" as="xs:string" select="substring-after($href,'#')"/>
        <xsl:variable name="levelStr" as="xs:string">
            <xsl:number level="multiple" count="chapter|topicref" format="1.1"/>
        </xsl:variable>
        <xsl:variable name="title" as="text()*">
            <xsl:apply-templates select="document(concat(string(resolve-uri('.', static-base-uri())),$topicFile))//topic[string(@id) eq $id]/title"/>
        </xsl:variable>
        <xsl:value-of select="$levelStr"/>
        <xsl:text> </xsl:text>
        <xsl:value-of select="$title"/>
        <xsl:text>&#x0A;</xsl:text>
    </xsl:template>

</xsl:stylesheet>

注意:所有数据文件和样式表都存在于同一个文件夹中

[输出结果]

1.1 Number 1
1.2 Number 2
1.3 Number 3
2.1 Number 2-1
2.2 Number 2-2

但是,DITA 映射和主题不应以这种方式处理。应该使用 DITA-OT 处理它们。

【讨论】:

  • 您好,谢谢您,我已经进行了更改,并且能够针对标题和输出完全符合我的要求。但是,它会打开并使用输出创建一个新的 xml 预览。如何写入 dita 文件或生成的 html 文件?再次感谢您
  • @LydiaPiglet 如果您遇到其他问题,最好提交新帖子。
  • 您好,感谢您的回复。我确实创建了一个新线程stackoverflow.com/questions/41054839/…。预期的输出显示在新的 xml 编辑器中,只是生成的 html 文件没有完全生成。第一行模板匹配导致了问题,我在想你的代码是如何处理的,因为我有一个主题/主题/标题的模板匹配并将其替换为 h2,你的模板匹配 topicref [@href],它将覆盖标题?
  • 嗨,我改为
猜你喜欢
  • 2020-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-11
  • 1970-01-01
  • 1970-01-01
  • 2016-08-02
相关资源
最近更新 更多