面对一些属性节点问题

时间:2014-02-06 08:29:33

标签: xslt

我的全长XSLT上有以下代码,并且在转换过程中会产生一些错误。 错误为XPTY0004: A sequence of more than one item is not allowed as the first argument of matches()

<xsl:template match="par[@class='tablecaption']" exclude-result-prefixes="html">
<p class="caption1">
<xsl:variable name="n2" select="./text()|./*/text()"/>
<xsl:attribute name="id">
<xsl:if test="matches($n2, '(Table)\s(\d+|[A-Z])(\.)(\d+)')">
<xsl:variable name="y2" select="replace($n2, '(Table)\s(\d+|[A-Z])(\.)(\d+)', '$4')"/>Tab<xsl:value-of select="normalize-space(substring($y2, 1, 2))"/></xsl:if>
</xsl:attribute>
<strong><xsl:apply-templates/></strong></p>
</xsl:template>

这里有什么不对? PLS

1 个答案:

答案 0 :(得分:0)

将变量n2更改为./descendant::text()[1]

<xsl:template match="par[@class='tablecaption']" exclude-result-prefixes="html">
<p class="caption1">
<xsl:variable name="n2" select="./descendant::text()[1]"/>
<xsl:attribute name="id">
<xsl:if test="matches($n2, '(Table)\s(\d+|[A-Z])(\.)(\d+)')">
<xsl:variable name="y2" select="replace($n2, '(Table)\s(\d+|[A-Z])(\.)(\d+)', '$4')"/>Tab<xsl:value-of select="normalize-space(substring($y2, 1, 2))"/></xsl:if>
</xsl:attribute>
<strong><xsl:apply-templates/></strong></p>
</xsl:template>

使用此输入

<root>
    <par class="tablecaption"> Table A.1 <bold>text2</bold></par>
</root>

它得到:

<root>
    <p class="caption1" id="Tab1"><strong> Table A.1 <bold>text2</bold></strong></p>
</root>