XSL:将字符串值传递给模板匹配

时间:2018-09-06 14:36:40

标签: java xslt

我想将xpath字符串从Java传递到XSL模板。我尝试过这些

<xsl:template
      match="string($fullxpath)">

<xsl:template
      match="$fullxpath">

这有效

xpath value: <xsl:text/>
      <xsl:value-of select="$fullxpath" />

但是比赛不起作用。请帮忙!!

2 个答案:

答案 0 :(得分:1)

通常这是不可能的,您将需要学习区分允许XPath expression计算值的属性(例如select https://www.w3.org/TR/xslt-30/#value-of的属性)和xsl:value-of的属性(例如patterns https://www.w3.org/TR/xslt-30/#defining-templatesmatch)。

然而,在XSLT 3中,有一个新的选项,即所谓的阴影属性https://www.w3.org/TR/xslt-30/#shadow-attributes和静态参数https://www.w3.org/TR/xslt-30/#static-params

xsl:template

https://xsltfiddle.liberty-development.net/jyH9rMM

如您所见,阴影属性<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="#all" version="3.0"> <xsl:param name="pattern1" as="xs:string" static="yes" select="'foo/bar'"/> <xsl:mode on-no-match="shallow-copy"/> <xsl:template _match="{$pattern1}"/> </xsl:stylesheet> 设置为静态参数值。

一种不同的,更复杂的方法,但在XSLT 3和Saxon 9.8所有版本或其他兼容的XSLT 3实现中也是可能的,它会根据需要生成新的样式表,并使用_match函数https://www.w3.org/TR/xpath-functions/#func-transform执行。您需要在XSLT中生成具有不同名称空间的样式表代码,您可以将其命名为XSLT名称空间:

transform

https://xsltfiddle.liberty-development.net/jyH9rMM/2

答案 1 :(得分:0)

你是说

<xsl:template
      match="*[name() = $fullxpath]">

意思是,“匹配任何元素,但条件是它们的名称等于变量fullxpath的文本内容”