如何在XSL中实现递归?

时间:2012-05-22 18:53:42

标签: xslt

我有一个XML数据文档,其中包含名为“folio”的嵌套标记。它可以嵌套标签的最大级别是4.我需要递归处理每个作品集,直到我达到没有嵌套作品集并打印内容的级别。

我不确定如何在XSL中进行递归。这是一个示例文档。可以帮忙吗?

感谢。

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
    <reporting:root xmlns:reporting="http://www.xyzcorp.net/reporting">
      <reporting:default0 reporting:type="Portfolio">
        <reporting:header>
          <reporting:configuration>
        <reporting:columns>
          <reporting:column reporting:group="instrument" reporting:name="Ident" reporting:tag="ident" reporting:type="int"/>
          <reporting:column reporting:group="prices" reporting:name="Last (Time)" reporting:tag="lastTime" reporting:type="string"/>
          <reporting:column reporting:group="noGroup" reporting:name="RIC" reporting:tag="ric" reporting:type="string"/>
          <reporting:column reporting:group="instrument" reporting:name="Reference" reporting:tag="reference" reporting:type="string"/>
          <reporting:column reporting:group="result" reporting:name="Currency" reporting:tag="currency" reporting:type="string"/>
        </reporting:columns>
      </reporting:configuration>
    </reporting:header>
    <reporting:window reporting:Id="36674" reporting:level="0" reporting:name="MY_PORTFOLIO" reporting:parentId="11991">
      <reporting:folio>
        <reporting:folioid>1234</reporting:folioid>
        <reporting:folio>
          <reporting:folioid>9876</reporting:folioid>
          <reporting:line reporting:Id="67520135" reporting:level="1" reporting:name="INTERNATIONAL BUSINESS MACHINES CORP" reporting:parentId="36674" reporting:positionType="0">
            <reporting:ident>643633</reporting:ident>
            <reporting:reference>IBM.USD</reporting:reference>
            <reporting:currency>USD</reporting:currency>
          </reporting:line>
          <reporting:line reporting:Id="67520179" reporting:level="1" reporting:name="GENERAL ELECTRIC CO" reporting:parentId="36674" reporting:positionType="0">
            <reporting:ident>643635</reporting:ident>
            <reporting:ric>GE.N</reporting:ric>
            <reporting:reference>GE.USD</reporting:reference>
            <reporting:currency>USD</reporting:currency>
          </reporting:line>
        </reporting:folio>
      </reporting:folio>
    </reporting:window>
  </reporting:default0>
</reporting:root>

1 个答案:

答案 0 :(得分:5)

XSLT中正常的默认处理模式是为每个执行某些操作的元素编写模板规则,并调用apply-templates来处理其子代:

<xsl:template match="folio">
  <do something>
    <xsl:apply-templates/>
  </do something>
</xsl:template>

这自然是递归的:无论何时处理元素,都会调用其后代的处理。

所以你真的不需要学习如何在XSLT中进行递归;你几乎需要学习如何不去做。