我正在转换一些XML,将每个名为alt-title的元素重命名为Running_Head,条件是属性alt-title-type等于“running”。
因此,下面的代码使用的是行<xsl:when test="starts-with(@alt-title-type, 'running-head')">,它工作得很好。但是,当我将其更改为以下任何一种时:
<xsl:when test="ends-with(@alt-title-type, 'running-head')"><xsl:when test="matches(@alt-title-type, 'running-head')">引发...this错误:
错误:XSLTProcessor::transformToXml() xsltprocessor.transformtoxml: xmlXPathCompiledEval: 2对象留在堆栈中。
因此,似乎starts-with函数正在工作,而ends-with和matches则不起作用。
下面是我使用starts-with的XSL,它似乎正在正常工作:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" indent="yes" method="xml" />
<!-- Running_Head -->
<xsl:template match="@*|node()">
<xsl:choose>
<xsl:when test="starts-with(@alt-title-type, 'running-head')">
<xsl:element name="Running_Head">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template> <!-- end of Running_Head -->
</xsl:stylesheet>...and这里是正在转换的XML:
<root-node>
<alt-title alt-title-type="running-head">
This is working
</alt-title>
<alt-title alt-title-type="asdfng-head">
asdfasdf
</alt-title>
<alt-title>
asdfasdf
</alt-title>
<alt-title alt-title-type="running-head">
This is also working
</alt-title>
</root-node>我正在http://xslt.online-toolz.com/tools/xslt-transformation.php和http://www.xsltcake.com/上测试这个。
发布于 2012-07-29 22:32:26
正如其他人指出的那样,XSLT1.0处理器不支持大多数XPath 2.0函数(如matches()和ends-with())。
此外,在实现当前需求的转换中,根本不需要这些函数。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="alt-title[@alt-title-type='running-head']">
<Running_Head>
<xsl:apply-templates select="@*|node()"/>
</Running_Head>
</xsl:template>
</xsl:stylesheet>当此转换应用于所提供的XML文档时,则为。
<root-node>
<alt-title alt-title-type="running-head">
This is working
</alt-title>
<alt-title alt-title-type="asdfng-head">
asdfasdf
</alt-title>
<alt-title>
asdfasdf
</alt-title>
<alt-title alt-title-type="running-head">
This is also working
</alt-title>
</root-node>被通缉的,正确的reault产生了:
<root-node>
<Running_Head alt-title-type="running-head">
This is working
</Running_Head>
<alt-title alt-title-type="asdfng-head">
asdfasdf
</alt-title>
<alt-title>
asdfasdf
</alt-title>
<Running_Head alt-title-type="running-head">
This is also working
</Running_Head>
</root-node>解释
正确使用模板、匹配模式和重写身份规则。
发布于 2012-07-29 21:07:01
只有XPath 2.0具有matches和ends-with函数。
在XPath 1.0中,必须编写ends-with
$suffix = substring($target, string-length($target) - string-length($suffix) + 1)它没有正则表达式功能,但可能
包含($target,$substring)
如果不使用正则表达式元字符,则为所需。
发布于 2012-07-29 21:07:45
您将模板声明为XSLT1.0,但使用的是2.0函数ends-with和matches。使用XSL2.0处理器(或在缺失的函数周围工作),并将文档声明为XSLT2.0。
请注意,您得到的错误是这些服务使用的XSL处理器的内部错误(看起来它们不能正确处理未定义的函数)。
https://stackoverflow.com/questions/11712884
复制相似问题