0

我有一个 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>
4

1 回答 1

5

XSLT 中正常的默认处理模式是为每个元素编写一个模板规则,并调用 apply-templates 来处理它的子元素:

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

这自然是递归的:每当你处理一个元素时,你都会调用它的后代的处理。

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

于 2012-05-22T19:04:17.667 回答