[英]Referencing attribute value in xsl:param xsl:if test condition
I'm trying to retrieve the attribute value from an xsl:param and use it in an xsl:if test condition. 我试图从xsl:param检索属性值,并在xsl:if测试条件中使用它。 So given the following xml 所以给定以下xml
<product>
<title>The Maze / Jane Evans</title>
</product>
and the xsl 和xsl
<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:param name="test" select="Jane"/>
<xsl:template match="title[contains(., (REFERENCE THE SELECT ATTRIBUTE IN PARAM))]">
<h2>
<xsl:value-of select="substring-before(., '/')"/>
</h2>
<p>
<xsl:value-of select="substring-after(., '/')"/>
</p>
</xsl:template>
<xsl:template match="title">
<h2><xsl:value-of select="."/></h2>
</xsl:template>
</xsl:stylesheet>
I would like to get back 我想回来
The Maze
Jane Evans
You have a problem in this line : 您在这一行有问题 :
<xsl:param name="test" select="Jane"/>
This defines an xsl:param
named test
, whose value is the child element of the current node ('/') named Jane
. 这定义了一个名为test
的xsl:param
,其值是名为Jane
的当前节点('/')的子元素。 As the top element is <product>
and not <Jane>
, the test
parameter has the value of an empty node-set (and a string value -- the empty string). 由于顶部元素是<product>
而不是<Jane>
,因此test
参数具有空节点集的值(以及字符串值-空字符串)。
You want (notice the surrounding apostrophes): 您需要 (注意周围的撇号):
<xsl:param name="test" select="'Jane'"/>
The whole processing task can be implemented rather easily : 整个处理任务可以很容易地实现 :
This XSLT 1.0 transformation : 此XSLT 1.0转换 :
<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:param name="pTest" select="'Jane'"/>
<xsl:template match="title">
<xsl:choose>
<xsl:when test="contains(., $pTest)">
<h2>
<xsl:value-of select="substring-before(., '/')"/>
</h2>
<p>
<xsl:value-of select="substring-after(., '/')"/>
</p>
</xsl:when>
<xsl:otherwise>
<h2><xsl:value-of select="."/></h2>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document : 当应用于提供的XML文档时 :
<product>
<title>The Maze / Jane Evans</title>
</product>
produces the wanted, correct result : 产生想要的正确结果 :
<h2>The Maze </h2>
<p> Jane Evans</p>
Explanation : 说明 :
The XSLT 1.0 syntax forbids variable/parameter references in a match pattern. XSLT 1.0语法禁止以匹配模式引用变量/参数。 This is why we have a single template matching any title
and we specify inside the template the conditions for processing in a specific, wanted way. 这就是为什么我们有一个与任何title
匹配的模板,并且在模板内指定了以特定的所需方式进行处理的条件的原因。
An XSLT 2.0 solution : XSLT 2.0解决方案 :
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pTest" select="'Jane'"/>
<xsl:template match="title[contains(., $pTest)]">
<h2>
<xsl:value-of select="substring-before(., '/')"/>
</h2>
<p>
<xsl:value-of select="substring-after(., '/')"/>
</p>
</xsl:template>
<xsl:template match="title">
<h2><xsl:value-of select="."/></h2>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided XML document (above), again the same wanted, correct result is produced : 当此转换应用于提供的XML文档(如上)时,同样会产生所需的正确结果 :
<h2>The Maze </h2>
<p> Jane Evans</p>
Explanation : 说明 :
XSLT 2.0 doesn't have the limitations of XSLT 1.0 and variable/parameter references can be used within a match pattern. XSLT 2.0没有XSLT 1.0的限制,并且可以在匹配模式中使用变量/参数引用。
The term $test refers to the value of the test parameter. 术语$ test是指测试参数的值。 Use $test 使用$ test
eg: 例如:
<xsl:template match="title[contains(., $test)]">
<h2>
<xsl:value-of select="substring-before(., '/')"/>
</h2>
<p>
<xsl:value-of select="substring-after(., '/')"/>
</p>
</xsl:template>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.