xslt - xsl : Can a named template return a node list?

标签 xslt xslt-2.0

我正在使用一个递归模板来搜索一些像这样的特定元素:

<xsl:template name="GetProdDependency">
    <xsl:param name="TechProd"></xsl:param>
    <xsl:param name="BeatenPath"></xsl:param>
    <xsl:variable name="TechProdArch" select="$TechProd/pro:own_slot_value[pro:slot_reference='technology_product_architecture']/pro:value"></xsl:variable>
    <xsl:variable name="TechProdArchNode" select="/node()/pro:simple_instance[pro:name=$TechProdArch]"></xsl:variable>
    <xsl:variable name="TechProdCompList" select="$TechProdArchNode/pro:own_slot_value[pro:slot_reference='contained_techProd_components']/pro:value"/>
    <xsl:for-each select="$TechProdCompList">
        <xsl:variable name="TechProdAsRole" select="/node()/pro:simple_instance[pro:name=current()]/pro:own_slot_value[pro:slot_reference='technology_product_as_role']/pro:value"/>
        <xsl:variable name="TechProdRole" select="/node()/pro:simple_instance[pro:name=$TechProdAsRole]/pro:own_slot_value[pro:slot_reference='role_for_technology_provider']/pro:value"/>
        <xsl:variable name="DepTechProd" select="/node()/pro:simple_instance[pro:name=$TechProdRole]"/>
        <!-- Check for beaten Path -->
        <!-- if $DepTechProd/pro:own_slot_value[pro:slot_reference='name']/pro:value in BeatenPath -->
        <xsl:if test="not($BeatenPath[string(.)=string($DepTechProd/pro:own_slot_value[pro:slot_reference='name']/pro:value)])"> 
            <!-- Do the recursion here! -->
            <!--<xsl:value-of select="$DepTechProd/pro:own_slot_value[pro:slot_reference='name']/pro:value"/> (type: <xsl:value-of select="$DepTechProd/pro:type"/> and Class: <xsl:value-of select="$DepTechProd/pro:name"/>)-->
            <!--<xsl:value-of select="$DepTechProd/pro:own_slot_value[pro:slot_reference='name']/pro:value"/>-->
            <xsl:value-of select="$DepTechProd"/>
            <xsl:call-template name="GetProdDependency">
                <xsl:with-param name="TechProd" select="$DepTechProd"></xsl:with-param>
                <xsl:with-param name="BeatenPath" select="$TechProd|$DepTechProd/pro:own_slot_value[pro:slot_reference='name']/pro:value"></xsl:with-param>
                <xsl:with-param name="Rev" select="$Rev + 1"></xsl:with-param>
            </xsl:call-template>
        </xsl:if>
    </xsl:for-each>
</xsl:template> 

这在搜索等中工作正常。

但是当我在原始调用者中得到结果时,我期望从调用中得到一个节点列表。

我称之为:
<xsl:variable name="DelPlist">
<xsl:call-template name="GetProdDependency">
    <xsl:with-param name="TechProd" select="$TechProd"></xsl:with-param>
    <xsl:with-param name="BeatenPath" select="$TechProd/pro:own_slot_value[pro:slot_reference='name']/pro:value"></xsl:with-param>
    <xsl:with-param name="Rev" select="1"></xsl:with-param>
</xsl:call-template>
</xsl:variable>

我希望得到一个我可以用 <xsl:for-each> 迭代的节点列表。但是如果我检查 count($DelPlist) ,结果是 1 并且我无法迭代。

有人可以帮忙吗?

最佳答案

您必须在其 as 属性 中指定模板结果的类型。

如果未指定,则类型为 document-node() ,然后为了迭代结果,您需要获取结果的子项。

解决方案 :使用 as 属性指定模板的返回类型。

下面是一个完整的例子 :

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
     <xsl:variable name="vNodes" as="element()*">
      <xsl:call-template name="genNodes"/>
     </xsl:variable>

     <xsl:for-each select="$vNodes">
      <xsl:value-of select="concat('&#xA;', position(), ': ')"/>
      <xsl:copy-of select="."/>
     </xsl:for-each>
 </xsl:template>

 <xsl:template name="genNodes" as="element()*">
  <a/>
  <b/>
  <c/>
 </xsl:template>
</xsl:stylesheet>

将此转换应用于任何 XML 文档(未使用)时,会生成所需的正确结果 :
1: <a/>
2: <b/>
3: <c/>

关于xslt - xsl : Can a named template return a node list?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8708785/

相关文章:

xml - Xpath 有没有更简单的方法来过滤多个 'contain' 条件?

xslt - 输出中的空格

xslt - XSLT-2.0:将current-group()的结果合并为一个

.net - XSLT:禁用整个文档中的输出转义

xml - 从 XSLT 中抛出异常

xml - 应用模板如何工作?

xslt - xsl :copy-of excluding parent

xml - 节点名称上的 XPath 通配符

xml - XSLT 将两个输入文件处理为一个输出文件

java - XML-XSLT : How to compare two dates which are in String