![](/img/trans.png)
[英]In XSLT is it possible to use the value of an xpath expression in a call to a template using an param
[英]Is it possible to use a Dynamic xPath expression in a xslt style sheet?
我想在xpath表達式中使用xslt參數的值。 具體來說,作為<xsl:if
表達式中的not()
調用的一部分。
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- my_param contains a string '/foo/bar', passed in from ant -->
<!-- the 'no' is just a default value -->
<xsl:param name="my_param">no</xsl:param>
<xsl:variable name="var_myparam" select="$my_param" />
<!-- ... -->
<!-- this works -->
<xsl:if test="not(/foo/bar)" /> <!-- expression returns boolean true -->
<!-- ... -->
</xsl:if>
<!-- I can't figure out how to do this the right way -->
<!-- None of these appear to work -->
<xsl:if test="not($var_myparam)" /> <!-- expression returns boolean false -->
<!-- ... -->
</xsl:if>
<xsl:if test="not({$var_myparam})" /> <!-- complains Required attribute 'test' is missing -->
<!-- ... -->
</xsl:if>
<xsl:if test="not({$myparam})" /> <!-- complains Required attribute 'test' is missing -->
<!-- ... -->
</xsl:if>
<xsl:if test="not($myparam)" /> <!-- expression returns boolean false -->
<!-- ... -->
</xsl:if>
</xsl:transform>
我有點不清楚在xslt樣式表中創建動態xpath表達式的正確語法是什么。 對於參數,變量以及兩者的擴展方式之間的差異,我也有點模糊。 例如,有了參數,我知道有時我需要括號,有時我不需要。
<!-- when creating an element, it seems I need brackets -->
<xsl:element name="{$my_param}" />
<!-- when selecting an element value for the output stream, no brackets are needed -->
<xsl:value-of select="$my_param"/>
任何一般/特定的幫助將不勝感激。
查看http://www.exslt.org/ 。 具體來看動態:評估模塊。
XSLT不支持XPath的動態評估 - 這就是為什么你需要使用像EXSLT evaluate
那樣的擴展函數來實現它。
如果您不想使用擴展函數(並且有充分的理由不這樣做),另一種可能性是在執行轉換之前查詢輸入文檔的DOM,並將節點集作為參數傳遞給轉換。
編輯
關於那些花括號:這些是屬性值模板(AVT)。 這些在XSLT轉換中在語義上是等效的:
<foo>
<xsl:attribute name="bar">
<xsl:value-of select="XPathExpression"/>
</xsl:attribute>
</foo>
<foo bar="{XPathExpression}"/>
第二個只是第一個的捷徑。
關於變量和參數:從語法上講,變量和參數之間沒有區別; 在XPath表達式中引用$foo
任何地方,無論foo
是由xsl:variable
還是xsl:param
定義,它都將起作用。
兩者之間的區別在於它們是如何填充的。 變量填充在xsl:variable
聲明中。 使用xsl:param
在命名模板中聲明xsl:param
,但是使用xsl:with-param
調用命名模板的任何命令都會填充它們,例如:
<xsl:call-template name="foo">
<xsl:with-param name="bar" select="XPathExpression"/>
</xsl:call-template>
<xsl:template name="foo">
<xsl:param name="bar"/>
...
對此有一個很大的例外是xsl:stylesheet
的子代。 沒有辦法在變換中填充這些參數; 那些是外部填充的,由任何(特定於環境)機制調用轉換。
一個非常常見的用例是彌補XSLT沒有系統日期功能的事實。 所以你會看到類似的東西:
<xsl:stylesheet ...
<xsl:param name="system-date"/>
...
然后,在調用轉換時,這樣的事情(在C#中):
XsltArgumentList args = new XsltArgumentList();
args.AddParam("system-date", "", DateTime.Now.ToString("s"));
xslt.Transform(input, args, result);
嘗試一些變化:
<xsl:if test="not($var_myparam = 'no'))">
</xsl:if>
問題在於XPath如何評估布爾值。 在XPath中,任何非空字符串都將計算為true。 查看關於xpath布爾值的這篇文章 。
至於你的其他問題......變量和參數在XPath表達式中的作用相同。 兩者都以$ var的形式引用。
對於任何名為“select”的XSLT屬性,您不需要括號。 括號用於所謂的“屬性值模板”。 您在XSLT期待字符串時使用它們,例如:
<xsl:template match="name-a">
<xsl:variable name="old-name" select="name(.)" />
<name-b old-name="{$old-name}" new-attribute="hello" />
</xsl:template>
XSLT規范討論了AVT , 這個頁面也是如此。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.