使用evaluate()方法

问题描述:

不过我得到下面的错误时抛出的XPath语法错误使用evaluate()方法

Error at xsl:param on line 6 of file:/E:/saxon/parastyleText.xsl: 
    XPST0003: XPath syntax error at char 0 on line 6 in {...le/@w:val[matches(., c 
oncat...}: 
    Invalid character '^' in expression 
Failed to compile stylesheet. 1 error detected. 

修改XSL:

<?xml version="1.0" encoding="utf-8"?> 
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"> 

    <xsl:param name="styleName" select="'articletitle'"/> 
    <xsl:param name="tagName" select="'//w:p[w:pPr/w:pStyle/@w:val[matches(., concat('^(',$styleName,')$'),'i')]]'"/> 

    <xsl:output method="text"/> 

    <xsl:template match="/"> 
     <xsl:value-of select="saxon:evaluate($tagName)" xmlns:saxon="http://saxon.sf.net/"/><xsl:text>&#10;</xsl:text> 
    </xsl:template> 

    </xsl:stylesheet> 

请不要答复,报价会令“标签名”作为字符串并删除这些引号。这个值实际上是作为字符串从java传递的,tats y用于测试目的,我已经将这个xpath作为字符串传递。

+2

您准确使用哪种XSLT处理器?它的文档是否支持EXSLT'dyn:evaluate'函数?为什么你提到XSLT 3.0版? EXSLT与XSLT 3.0版无关,它是为XSLT 1.0定义扩展的努力。 XSLT版本3.0在独立于EXSLT的动态路径评估中有其自己的方法http://www.w3.org/TR/xslt-30/#dynamic-xpath。当然,你需要一个支持XSLT 3.0版本的处理器。 –

+0

我怀疑任何XSLT ** 2.0 **处理器都支持EXSLT,而不是谈论任何XSLT 3.0处理器。在XSLT 3.0中,你可能想要使用'' - 看到它在这里定义:http://www.w3.org/TR/xslt-30/#element-evaluate –

+0

@Martin Honnen - 我一直使用撒克逊xslt处理器。版本9.1.0.8的文档说它支持evaluate()方法。我提到'XSLT 3.0版'是一个错误。我已纠正它。请看看... – Pippa

根据在线文档http://www.saxonica.com/documentation9.1/extensions/functions.html撒克逊9.1支持an evaluate function in the Saxon namespace http://saxon.sf.net/。所以用撒克逊9.1试试<xsl:value-of select="saxon:evaluate($tagName)" xmlns:saxon="http://saxon.sf.net/"/>。当然,如果你愿意的话,你可以将命名空间声明移动到xsl:stylesheet元素,我只是把它放在xsl:value-of这篇文章中,用于一个简短但完整的代码示例。

另请注意,使用名称为tagName的变量,您可能只需要一个元素名称,在这种情况下,使用<xsl:value-of select="*[local-name() eq $tagName]"/>就足够了。