在 xpath/xslt 中使用括号 [英] using parentheses in xpath/xslt
问题描述
我正在尝试使用括号来覆盖 xslt 中 xpath 表达式中的默认运算符优先级,但没有运气.例如:
<xsl:output encoding="utf-8" standalone="yes"/><xsl:template match="/"><xsl:apply-templates select="*"/></xsl:模板><xsl:template match="@* | node()"><xsl:copy><xsl:apply-templates select="@* | node()"/></xsl:copy></xsl:模板><!--这些应该可以工作,但不能--><xsl:template match="//(X|Y|Z)/AABBCC"/><xsl:template match="(book/author)[last()]"/></xsl:stylesheet>
Visual Studio 2010 不会编译此返回:
表达式中出现意外标记 '('.//-->(<-- X|Y|Z)/AABBCC
表达式中出现意外标记 '('.-->(<-- book/author)[last()]
然而第二个例子来自 MSDN:
http://msdn.microsoft.com/en-us/library/ms256086.aspx一个>
许多参考文献都说你可以这样使用括号:
http://saxon.sourceforge.net/saxon6.5.3/expressions.html>
http://www.stylusstudio.com/xsllist/200207/post90450.html>
http://www.mulberrytech.com/quickref/XSLT_1quickref-v2.pdf>
这是 xpath 1.0 还是 2.0 的东西……还是我还缺少其他东西?如果它是一个 xpath 2.0 的东西,是否有一个很好的 xpath 1.0 方式来做同样的事情?
您必须了解 xsl:template
的 match
属性不允许任何 XPath 表达式而是所谓的模式:https://www.w3.org/TR/1999/REC-xslt-19991116#patterns,XPath 表达式的子集.
因此,虽然 (book/author)[last()]
是语法正确的 XPath 1.0 表达式,但我认为它不是语法正确的 XSLT 1.0 模式,但不允许使用括号.
我不认为 //(X|Y|Z)/AABBCC
是允许的 XPath 1.0 表达式(当然也不是模式)而是 match="X/AABBCC |Y/AABBCC | Z/AABBCC"
应该可以.
I'm trying to use parentheses to override default operator precedence in an xpath expression within an xslt with no luck. For example:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl"
version="1.0">
<xsl:output encoding="utf-8" standalone="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!--these should work but don't-->
<xsl:template match="//(X|Y|Z)/AABBCC"/>
<xsl:template match="(book/author)[last()]"/>
</xsl:stylesheet>
Visual Studio 2010 won't compile this returning:
Unexpected token '(' in the expression. // -->(<-- X|Y|Z)/AABBCC
Unexpected token '(' in the expression. -->(<-- book/author)[last()]
Yet the second example is from MSDN:
http://msdn.microsoft.com/en-us/library/ms256086.aspx
and numerous references say you can use parentheses in this way:
http://saxon.sourceforge.net/saxon6.5.3/expressions.html
http://www.stylusstudio.com/xsllist/200207/post90450.html
http://www.mulberrytech.com/quickref/XSLT_1quickref-v2.pdf
Is this an xpath 1.0 vs 2.0 thing...or is there something else i'm missing? If it's an xpath 2.0 thing, is there a nice xpath 1.0 way to do the same thing?
You have to understand that a match
attribute of an xsl:template
does not allow any XPath expression but rather only so called patterns: https://www.w3.org/TR/1999/REC-xslt-19991116#patterns, a subset of XPath expressions.
So while (book/author)[last()]
is a syntactically correct XPath 1.0 expression I don't think it is a syntactically correct XSLT 1.0 pattern, the parentheses are not allowed.
I don't think //(X|Y|Z)/AABBCC
is an allowed XPath 1.0 expression (nor a pattern of course) but match="X/AABBCC | Y/AABBCC | Z/AABBCC"
should do.
这篇关于在 xpath/xslt 中使用括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!