Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt 递归函数中的变量访问_Xslt - Fatal编程技术网

Xslt 递归函数中的变量访问

Xslt 递归函数中的变量访问,xslt,Xslt,我正在创建一个递归XSLT函数,它运行在所有Xml文件子文件上 <xsl:call-template name="testing"> <xsl:with-param name="root" select="ItemSet"></xsl:with-param> </xsl:call-template> XML代码: <ItemSet> <Item> 1 <iteml1> 1

我正在创建一个递归XSLT函数,它运行在所有Xml文件子文件上

<xsl:call-template name="testing">
  <xsl:with-param name="root" select="ItemSet"></xsl:with-param>
</xsl:call-template>

XML代码:

<ItemSet>
  <Item>
    1
    <iteml1>
      1.1
    </iteml1>
    <iteml1>
      1.2
    </iteml1>
  </Item>

  <Item>
    2
    <iteml1>
      2.1
      <iteml2>
        2.1.1
      </iteml2>
    </iteml1>
  </Item>
</ItemSet>

您有两种选择:如果这只在一个地方使用,您可以在样式表根中创建一个变量并全局使用它


否则,您需要做的是有两个参数,其中一个参数的传递方式与每次调用完全相同,因此除了调用
,您还可以在
下面添加
并定义
。然后,您可以在需要在堆栈顶部传递的原始值的任何位置使用$baseroot代替$root。

所需的处理可以高效地实现,而无需显式递归:

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

 <xsl:template match="*[parent::*]">
   <xsl:param name="pPath"/>
     <xsl:value-of select="$pPath"/>

  <xsl:variable name="vValue" select=
   "normalize-space(text()[1])"/>
  <xsl:value-of select="$vValue"/> <br/>

    <xsl:apply-templates select="*">
     <xsl:with-param name="pPath" select=
      "concat($pPath, $vValue, ': ')"/>
    </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="text()"/>
</xsl:stylesheet>
<ItemSet>
    <Item>
     1
        <iteml1>
         1.1
        </iteml1>
        <iteml1>
         1.2
        </iteml1>
    </Item>

 <Item>
    2     
        <iteml1>
         2.1       
            <iteml2>
             2.1.1
            </iteml2>
        </iteml1>
    </Item>
</ItemSet>


在提供的XML文档上应用此转换时:

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

 <xsl:template match="*[parent::*]">
   <xsl:param name="pPath"/>
     <xsl:value-of select="$pPath"/>

  <xsl:variable name="vValue" select=
   "normalize-space(text()[1])"/>
  <xsl:value-of select="$vValue"/> <br/>

    <xsl:apply-templates select="*">
     <xsl:with-param name="pPath" select=
      "concat($pPath, $vValue, ': ')"/>
    </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="text()"/>
</xsl:stylesheet>
<ItemSet>
    <Item>
     1
        <iteml1>
         1.1
        </iteml1>
        <iteml1>
         1.2
        </iteml1>
    </Item>

 <Item>
    2     
        <iteml1>
         2.1       
            <iteml2>
             2.1.1
            </iteml2>
        </iteml1>
    </Item>
</ItemSet>

1.
1.1
1.2
2.
2.1
2.1.1
产生所需结果

1<br/>1: 1.1<br/>1: 1.2<br/>2<br/>2: 2.1<br/>2: 2.1: 2.1.1<br/>
1
1:1.1
1:1.2
2:2.1
2:2.1:2.1
并在浏览器中显示为

1<br/>1: 1.1<br/>1: 1.2<br/>2<br/>2: 2.1<br/>2: 2.1: 2.1.1<br/>

1:1.1
1:1.2
2:2.1
2:2.1:2.1
的副本。很抱歉,这很刻薄,但更正打字错误和不忽略大小写可以显著提高可读性。我更正了一些格式错误(按
Ctrl+K
格式化代码),但我仍然不确定我是否理解您问题的全部范围。请随意编辑更多内容。@hassan sleh:您可以提出一个新的问题和新的要求,请通过评论让我知道。无意中,你的评论与我的答案不符——小心。我希望@Flynn1179能够表现出理解:)