我想为每个主题标题生成编号。
有一个具有以下结构的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编号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 #,如果匹配标题号。
下面是我的密码
(题目/题目/标题)
<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>谢谢。
发布于 2016-12-08 11:12:46
为了澄清前提条件,我制作了以下地图和主题文件。
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>
</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来处理。
https://stackoverflow.com/questions/41011437
复制相似问题