我是以下的XML。
<?xml version="1.0" encoding="UTF-8"?>
<chapter>
<toc>
<toc-part>
<toc-div>
<toc-item>
<toc-title>8.1 Introduction</toc-title>
<toc-pg>749</toc-pg>
<toc-subitem level="1">
<toc-title>Importance and vitality of contracts</toc-title>
<toc-pg>750</toc-pg>
</toc-subitem>
</toc-item>
<toc-item>
<toc-title>8.2 The Electronic Transactions Act</toc-title>
<toc-pg>751</toc-pg>
<toc-subitem level="1">
<toc-title>Introduction</toc-title>
<toc-pg>751</toc-pg>
</toc-subitem>
</toc-item>
</toc-div>
</toc-part>
</toc>
<page num="747"/>
<section level="sect1">
<title><content-style font-style="bold">8.1 INTRODUCTION</content-style></title>
<para num="8.1.1">One definition of information technology (‘IT’) </para>
<para num="8.1.2">Without doubt,</para>
<section level="sect2">
<title><content-style font-style="bold">Importance and vitality of contracts</content-style></title>
<para num="8.1.7">Contracts are of fundamental importance to IT transactions. As the Court of Appeal noted in </para>
<para num="8.1.8">The case is also</para>
</section>
</section>
<section level="sect1">
<title><content-style font-style="bold">8.2 THE ELECTRONIC TRANSACTIONS ACT</content-style></title>
<section level="sect2">
<title><content-style font-style="bold">Introduction</content-style></title>
<para num="8.2.1">TheThere are key provisions on electronic contracts, electronic and digital signatures and electronic records.</para>
</section>
<section level="sect2">
<title><content-style font-style="bold">Purpose and construction</content-style></title>
<para num="8.2.5">The ETA has an uncommon feature among the body of</para>
</section>
</section>
</chapter>
以及下面的XSLT
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:ntw="Number2Word.uri"
exclude-result-prefixes="ntw">
<xsl:output method="html"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="ThisDocument" select="document('')"/>
<xsl:template match="/">
<xsl:text disable-output-escaping="yes"><![CDATA[<!DOCTYPE html>]]></xsl:text>
<html>
<body>
<xsl:apply-templates select="chapter" />
</body>
</html>
</xsl:template>
<xsl:template match="chapter">
<xsl:apply-templates />
</xsl:template>
<xsl:template name="toc-item" match="chapter/toc/toc-part/toc-div/toc-item">
<xsl:variable name="tocpg">
<xsl:value-of select="concat('P',current()/toc-pg/text())"/>
</xsl:variable>
<xsl:variable name="tocpgtag" select="translate($tocpg,'.', '-')"/>
<xsl:variable name="chapternumber">
<!-- Get num attribute of parent node -->
<xsl:value-of select="ancestor::chapter[1]/@num"/>
</xsl:variable>
<xsl:variable name="strl">
<xsl:value-of select="string-length(substring-after(@num,'.'))"/>
</xsl:variable>
<xsl:variable name="Brac">
<xsl:choose>
<xsl:when test="contains(substring-after(@num,'.'),'.')">
<xsl:value-of select="3"/>
</xsl:when>
<xsl:when test="contains(@num,'(')">
<xsl:value-of select="2"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="1"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="NewL">
<xsl:value-of select="string-length(substring-before(current()/toc-pg,'.'))"/>
</xsl:variable>
<xsl:variable name="newNum">
<xsl:choose>
<xsl:when test="$NewL=1">
<xsl:value-of select="concat('0',substring-before(current()/toc-pg/text(),'.'))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-before(current()/toc-pg/text(),'.')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="d">
<xsl:value-of select="concat('toc-item-',$ThisDocument//ntw:nums[@num=$Brac]/@word,'-level')"/>
</xsl:variable>
<xsl:variable name="new">
<xsl:value-of select="concat('er:#LRSC_CH_',$newNum,'/',$tocpgtag)"/>
</xsl:variable>
<table class="{$d}">
<tbody>
<tr>
<td class="toc-item-num">
<xsl:value-of select="current()/@num"/>
</td>
<td class="toc-title">
<xsl:value-of select="current()/toc-title"/>
</td>
<xsl:variable name="toc-title" select="upper-case(current()/toc-title/text())" />
<td class="toc-pg">
<xsl:variable name="refe">
<xsl:choose>
<xsl:when test="//section[title/*[upper-case(text()) = $toc-title]]/section[1]/para[1]/@num">
<xsl:value-of select="//section[title/*[upper-case(text()) = $toc-title]]/section[1]/para[1]/@num[1]"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="//section[title/*[upper-case(text()) = $toc-title]]/para[1]/@num[1]"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="full">
<xsl:value-of select="$refe"/>
</xsl:variable>
<xsl:variable name="pre">
<xsl:value-of select="substring-before($refe,'.')"/>
</xsl:variable>
<xsl:variable name="post">
<xsl:value-of select="substring-after($refe,'.')"/></xsl:variable>
<xsl:variable name="siz">
<xsl:choose>
<xsl:when test="$pre>'1'">
<xsl:value-of select="concat('LRSC_CH_',$pre,'/P',translate($full,'.','-'))"></xsl:value-of>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('LRSC_CH_0',$pre,'/P',translate($full,'.','-'))"></xsl:value-of>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<a href="{$siz}">
<xsl:value-of select="./toc-pg"/>
</a>
</td>
</tr>
</tbody>
</table>
<xsl:if test="./toc-subitem">
<xsl:for-each select="./toc-subitem">
<xsl:apply-templates select="."/>
</xsl:for-each>
</xsl:if>
<!--</table>-->
</xsl:template>
<xsl:template match="toc-subitem">
<xsl:variable name="tocpg">
<xsl:value-of select="concat('P',current()/toc-pg/text())"/>
</xsl:variable>
<xsl:variable name="tocpgtag" select="translate($tocpg,'.', '-')"/>
<xsl:variable name="chapternumber">
<!-- Get num attribute of parent node -->
<xsl:value-of select="ancestor::chapter[1]/@num"/>
</xsl:variable>
<xsl:variable name="strl">
<xsl:value-of select="string-length(substring-after(@num,'.'))"/>
</xsl:variable>
<xsl:variable name="Brac">
<xsl:choose>
<xsl:when test="contains(substring-after(@num,'.'),'.')">
<xsl:value-of select="3"/>
</xsl:when>
<xsl:when test="contains(@num,'(')">
<xsl:value-of select="2"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="1"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="NewL">
<xsl:value-of select="string-length(substring-before(current()/toc-pg,'.'))"/>
</xsl:variable>
<xsl:variable name="newNum">
<xsl:choose>
<xsl:when test="$NewL=1">
<xsl:value-of select="concat('0',substring-before(current()/toc-pg/text(),'.'))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-before(current()/toc-pg/text(),'.')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="d">
<xsl:value-of select="concat('toc-item-',$ThisDocument//ntw:nums[@num='2']/@word,'-level')"/>
</xsl:variable>
<xsl:variable name="new">
<xsl:value-of select="concat('er:#LRSC_CH_',$newNum,'/',$tocpgtag)"/>
</xsl:variable>
<table class="{$d}">
<tbody>
<tr>
<td class="toc-item-num">
<xsl:value-of select="current()/@num"/>
</td>
<td class="toc-title">
<xsl:value-of select="current()/toc-title"/>
</td>
<xsl:variable name="toc-title" select="current()/toc-title/*" />
<!--
<td class="toc-pg">
<xsl:variable name="refe">
<xsl:choose>
<xsl:when test="//section[title/*[upper-case(text()) = $toc-title]]/section[1]/para[1]/@num[1]">
<xsl:value-of select="//section[title/*[upper-case(text()) = $toc-title]]/section[1]/para[1]/@num[1]"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="//section[title/*[upper-case(text()) = $toc-title]]/para[1]/@num[1]"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="full">
<xsl:value-of select="$refe"/>
</xsl:variable>
<xsl:variable name="pre">
<xsl:value-of select="substring-before($refe,'.')"/>
</xsl:variable>
<xsl:variable name="post">
<xsl:value-of select="substring-after($refe,'.')"/></xsl:variable>
<xsl:variable name="siz">
<xsl:choose>
<xsl:when test="$pre>'1'">
<xsl:value-of select="concat('LRSC_CH_',$pre,'/P',translate($full,'.','-'))"></xsl:value-of>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('LRSC_CH_0',$pre,'/P',translate($full,'.','-'))"></xsl:value-of>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<a href="{$siz}">
<xsl:value-of select="./toc-pg"/>
</a>
</td>
-->
<td class="toc-pg">
<xsl:variable name="refe">
<xsl:choose>
<xsl:when test="//section[title/node()/text() = $toc-title]/para[1]/@num[1]">
<xsl:value-of select="//section[title/node()/text() = $toc-title]/para[1]/@num[1]"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="//section[title/node()/text() = $toc-title]/section[1]/para[1]/@num"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="full">
<xsl:value-of select="$refe"/>
</xsl:variable>
<xsl:variable name="pre">
<xsl:value-of select="substring-before($refe,'.')"/>
</xsl:variable>
<xsl:variable name="post">
<xsl:value-of select="substring-after($refe,'.')"/></xsl:variable>
<xsl:variable name="siz">
<xsl:choose>
<xsl:when test="$pre>'1'">
<xsl:value-of select="concat('LRSC_CH_',$pre,'/P',translate($full,'.','-'))"></xsl:value-of>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('LRSC_CH_0',$pre,'/P',translate($full,'.','-'))"></xsl:value-of>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<a href="{$siz}">
<xsl:value-of select="./toc-pg"/>
</a>
</td>
</tr>
</tbody>
</table>
<xsl:if test="./toc-subitem">
<xsl:apply-templates/>
</xsl:if>
<!--</table>-->
</xsl:template>
<ntw:nums num="1" word="first"/>
<ntw:nums num="2" word="second"/>
<ntw:nums num="3" word="third"/>
<ntw:nums num="4" word="forth"/>
<ntw:nums num="5" word="fifth"/>
<ntw:nums num="6" word="sixth"/>
<ntw:nums num="7" word="seventh"/>
<ntw:nums num="8" word="eighth"/>
<ntw:nums num="9" word="nighth"/>
<ntw:nums num="10" word="tenth"/>
</xsl:stylesheet>
这里我想要实现的是,首先捕获toc-title
,然后通过XML文档进行搜索,找到toc-title
后我需要获取para num
{1}}关注它。在我的XSLT中,它适用于toc-item
,但在使用toc-subitem
时,它会拉错值,而下面是我得到的输出的一部分。
<table class="toc-item-first-level">
<tbody>
<tr>
<td class="toc-item-num"></td>
<td class="toc-title">8.1 Introduction</td>
<td class="toc-pg">
<a href="LRSC_CH_8/P8-1-7">749</a>
</td>
</tr>
</tbody>
</table>
<table class="toc-item-second-level">
<tbody>
<tr>
<td class="toc-item-num"></td>
<td class="toc-title">Importance and vitality of contracts</td>
<td class="toc-pg">
<a href="LRSC_CH_0/P">750</a>
</td>
</tr>
</tbody>
</table>
<table class="toc-item-first-level">
<tbody>
<tr>
<td class="toc-item-num"></td>
<td class="toc-title">8.2 The Electronic Transactions Act</td>
<td class="toc-pg">
<a href="LRSC_CH_8/P8-2-1">751</a>
</td>
</tr>
</tbody>
</table>
<table class="toc-item-second-level">
<tbody>
<tr>
<td class="toc-item-num"></td>
<td class="toc-title">Introduction</td>
<td class="toc-pg">
<a href="LRSC_CH_0/P">751</a>
</td>
</tr>
</tbody>
</table>
<table class="toc-item-second-level">
<tbody>
<tr>
<td class="toc-item-num"></td>
<td class="toc-title">Purpose and construction</td>
<td class="toc-pg">
<a href="LRSC_CH_0/P">752</a>
</td>
</tr>
</tbody>
</table>
预期输出如下。
<table class="toc-item-first-level">
<tbody>
<tr>
<td class="toc-item-num"></td>
<td class="toc-title">8.1 Introduction</td>
<td class="toc-pg">
<a href="LRSC_CH_8/P8-1-7">749</a>
</td>
</tr>
</tbody>
</table>
<table class="toc-item-second-level">
<tbody>
<tr>
<td class="toc-item-num"></td>
<td class="toc-title">Importance and vitality of contracts</td>
<td class="toc-pg">
<a href="LRSC_CH_8/P8-1-7">750</a>
</td>
</tr>
</tbody>
</table>
<table class="toc-item-first-level">
<tbody>
<tr>
<td class="toc-item-num"></td>
<td class="toc-title">8.2 The Electronic Transactions Act</td>
<td class="toc-pg">
<a href="LRSC_CH_8/P8-2-1">751</a>
</td>
</tr>
</tbody>
</table>
<table class="toc-item-second-level">
<tbody>
<tr>
<td class="toc-item-num"></td>
<td class="toc-title">Introduction</td>
<td class="toc-pg">
<a href="LRSC_CH_8/P8-2-1">751</a>
</td>
</tr>
</tbody>
</table>
<table class="toc-item-second-level">
<tbody>
<tr>
<td class="toc-item-num"></td>
<td class="toc-title">Purpose and construction</td>
<td class="toc-pg">
<a href="LRSC_CH_9/P8-2-5">752</a>
</td>
</tr>
</tbody>
</table>
在toc-subitem
中,我将值设为<a href="LRSC_CH_0/P">
,请告诉我哪里出错了。
由于
答案 0 :(得分:1)
我添加了样式表中缺少的以下块(没有它你的输出不一样):
<ntw:words>
<ntw:nums num="1" word="first" />
<ntw:nums num="2" word="second" />
</ntw:words>
我注意到你的代码有很多代码重复,这是一个常见的错误来源。还有许多不必要的变量,这增加了复杂性。我检查了项目和子项目模板之间的差异,并注意到正在使用upper-case()
函数测试字符串的那个,而另一个不是(它也略有不同,尽管t似乎指的是相同的元素)。所以我从不起作用的模板中替换了$toc-title
和$refe
变量定义,在其位置粘贴了您正在使用的模板中使用的完全相同的代码,并且正确地复制了值
为了解决您的问题复制这两个变量的定义来自toc-item
模板:
<xsl:variable name="toc-title" select="current()/toc-title/*" />
<xsl:variable name="refe">...</xsl:variable>
并将其粘贴到toc-subitem
模板中那些不会选择任何内容的变量的定义上。
我建议您花一些时间来重构样式表。您将更好地理解它是如何工作的,它将变得更简单,更不容易出错,并且可能具有原始尺寸的1/4或更小。首先删除重复和不必要的变量。
<强>更新强>
这些是我在您发布的XSLT文件中所做的仅更改,以使其正常工作(从$toc-title
模板复制了$refe
和toc-item
个定义):
<xsl:stylesheet ...> ...
<xsl:template match="toc-subitem">
...
<xsl:variable name="toc-title" select="upper-case(current()/toc-title/text())" />
...
<xsl:variable name="refe">
<xsl:choose>
<xsl:when test="//section[title/*[upper-case(text()) = $toc-title]]/section[1]/para[1]/@num">
<xsl:value-of select="//section[title/*[upper-case(text()) = $toc-title]]/section[1]/para[1]/@num[1]"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="//section[title/*[upper-case(text()) = $toc-title]]/para[1]/@num[1]"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
...
...