XSLT - 将变量导入xsl并在for-each循环中用作select表达式

时间:2015-05-16 11:53:38

标签: xml xslt

从外部我希望将某个变量传递给XSL(这里没有概率)并将其用作属性中的表达式"选择"一个xsl:for-each元素(prob)。

以下代码(简化,在适当的xml,样式表等元素中实现)将 NOT 工作,(但希望说明要完成的任务;注入的是示例字符串"书籍/ book"分配给param name searchstring)

...
<xsl:param name="searchstring" /> 

 <xsl:template match="/">
   <xsl:for-each select="$searchstring">
    <xsl:value-of select="title" />
   </xsl:for-each>
 </xsl:template>
...

因为&#34;注入&#34; param值是一个字符串,不会绑定到结果树片段,这显然是&#34; select&#34;属性。经过一段完整的胡须&#34-期间的研究,我很高兴能找到一条路......这个参数值如何变成一个合适的选择表达式?

最好的问候和thx已经

2 个答案:

答案 0 :(得分:1)

如果你真的需要传递一个XPath表达式来动态评估,那么你需要XSLT 3.0,它有http://www.w3.org/TR/xslt-30/#element-evaluate,或者你需要检查你是否更喜欢XSLT 1.0或2.0处理器提供像{{3这样的扩展函数}}。另一种方法是编写一个生成第二个样式表的样式表,这样您就可以将路径参数放在需要它们的位置。

答案 1 :(得分:0)

如果您能够对将作为参数传递的路径类型进行一些限制,则可以使用单个XSLT 1.0样式表来完成此

太糟糕了,你拒绝提供一个例子,迫使我自己编造:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:param name="path"/>

<xsl:template match="/">
    <selected-nodes>
        <xsl:apply-templates/>
    </selected-nodes>
</xsl:template>

<xsl:template match="*">
    <xsl:variable name="path-to-me">
        <xsl:for-each select="ancestor-or-self::*">
            <xsl:value-of select="concat('/', name())"/>
        </xsl:for-each>    
    </xsl:variable>
    <xsl:if test="$path-to-me=$path">
        <xsl:copy-of select="."/>
    </xsl:if>
    <xsl:apply-templates select="*"/>
</xsl:template>

</xsl:stylesheet>

<强> XML

<book>
    <chapter>
        <title>Chapter One</title>
        <section>
            <title>First Section</title>
                <subsection>
                    <title>Alpha</title>
                </subsection>
                <subsection>
                    <title>Bravo</title>
                </subsection>
                <subsection>
                    <title>Charlie</title>
                </subsection>
        </section>
        <section>
            <title>Second Section</title>
                <subsection>
                    <title>Delta</title>
                </subsection>
                <subsection>
                    <title>Echo</title>
                </subsection>
        </section>
    </chapter>      
    <chapter>
        <title>Chapter Two</title>
        <section>
            <title>Third Section</title>
                <subsection>
                    <title>Foxtrot</title>
                </subsection>
                <subsection>
                    <title>Golf</title>
                </subsection>
        </section>
        <section>
            <title>Fourth Section</title>
                <subsection>
                    <title>Hotel</title>
                </subsection>
                <subsection>
                    <title>India</title>
                </subsection>
                <subsection>
                    <title>Juliet</title>
                </subsection>
        </section>
    </chapter>      
</book>

<强>参数

$path = "/book/chapter/section/title"

<强>结果

<?xml version="1.0" encoding="UTF-8"?>
<selected-nodes>
   <title>First Section</title>
   <title>Second Section</title>
   <title>Third Section</title>
   <title>Fourth Section</title>
</selected-nodes>

<强>约束:

  1. 传递的参数必须是元素的完整绝对路径(尽管可以扩展该方法以包含其他节点类型);
  2. 不允许使用谓词(尽管该方法可以扩展为包含数字谓词);
  3. 假设源XML节点没有名称空间。