首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算主题数,哪里出错了?

计算主题数,哪里出错了?
EN

Stack Overflow用户
提问于 2016-12-07 07:12:49
回答 1查看 121关注 0票数 0

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

有一个具有以下结构的Ditamap

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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编号1

// 1.1是基于匹配id和href生成的。

1.2数字2

// 1.2是基于匹配id和href生成的。

2.1编号2-1

2.2编号2-2

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

下面是我的密码

(题目/题目/标题)

代码语言:javascript
复制
<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>

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-08 11:12:46

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

test.ditamap

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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>

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

输出结果

代码语言:javascript
复制
1.1 Number 1
1.2 Number 2
1.3 Number 3
2.1 Number 2-1
2.2 Number 2-2

但是,DITA映射和主题不应该以这种方式处理。应该用DITA来处理。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41011437

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档