我有以下的XML。
<?xml version="1.0" encoding="UTF-8"?>
<chapter>
<toc>
<toc-part>
<toc-div>
<toc-item>
<toc-title>2.1 Introduction</toc-title>
<toc-pg>179</toc-pg>
</toc-item>
</toc-div>
</toc-part>
</toc>
<section level="sect1">
<title><content-style font-style="bold">2.1 INTRODUCTION</content-style></title>
<section level="sect2">
<title><content-style font-style="bold">The bank</content-style></title>
<para num="2.1.1">
</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 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>
<td class="toc-pg">
<xsl:variable name="refe">
<xsl:choose>
<xsl:when test="//section/title/content-style[upper-case(./text())=upper-case(current()/toc-title/text())]/section[1]/para[1]/@num[1]">
<xsl:value-of select="//section/title/content-style[upper-case(./text())=upper-case(current()/toc-title/text())]/section[1]/para[1]/@num[1]"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="//section/title/content-style[upper-case(text())=upper-case(current()/toc-title/text())]/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>
<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:style-sheet>
这里我已声明<xsl:when test="//section/title/content-style[upper-case(./text())=upper-case(current()/toc-title/text())]/section[1]/para[1]/@num[1]">
获取以下页面数量的section标签,但是当它成功时它没有被正确捕获
<xsl:when test="//section[upper-case(title/content-style/text())=upper-case(current()/toc-title/text())]/section[1]/para[1]/@num[1]">
它给我一个错误,如下所示。
XPTY0004: The supplied sequence ('3' item(s)) has the wrong occurrence to match the sequence type xs:string ('zero or one')
请让我知道我哪里出错了以及如何抓住序列。
由于
答案 0 :(得分:1)
这是你目前的表达......
<xsl:when test="//section/title/content-style[upper-case(./text())
= upper-case(current()/toc-title/text())]/section[1]/para[1]/@num[1]">
如果您删除 toc-title 上的条件检查,则会变为此
<xsl:when test="//section/title/content-style/section[1]/para[1]/@num[1]">
这应该(希望)突出显示您的问题,因为它正在内容样式对象下寻找部分元素。查看XML,情况并非如此。
由于部分元素位于另一个部分元素下,您可能需要在此处嵌套条件。试试这个
<xsl:when test="//section[title/content-style[upper-case(text())
= upper-case(current()/toc-title/text())]]/section[1]/para[1]/@num">
这里可能存在使用变量的情况,以提高可读性:
<xsl:variable name="toc-title" select="upper-case(current()/toc-title/text())" />
<xsl:choose>
<xsl:when test="//section[title/content-style[upper-case(text()) = $toc-title]]/section[1]/para[1]/@num">
话虽如此,当我使用替代表达式时,我无法重新创建错误。