在循环情况下处理潜在循环时如何编写更好的XSLT?

时间:2012-03-02 21:46:15

标签: xslt

我有这样的XML:

<?xml version="1.0" encoding="UTF-8"?>
<row>
    <cell colname="1"><Name>SomeValue<Ref format="Ref" Idref="ABC"/></Name></cell>
    <cell colname="2"><Selection>
        <Config><Configuration>AAA</Configuration></Config>
        <Config><Configuration>BBB</Configuration></Config>
    </Selection></cell>
    <cell colname="6"><Notes>
        <Text><IteratorDef>
            <IteratorTag Id="ABC">DDi</IteratorTag>
            <IteratorList>
                <IteratorChoice>05</IteratorChoice>
                <IteratorChoice>10</IteratorChoice>
                <IteratorChoice>15</IteratorChoice>
            </IteratorList>
        </IteratorDef></Text>
    </Notes></cell>
</row>

我想得到这样的结果:

Ref指向IteratorTag:    有两种配置即AAA和BBB。对于每个配置,有几个IteratorChoice值。每个都需要与Name值+ IteratorTag值连接,因此SomeValue + DDi + 05,SomeValue + DDi + 10和SomeValue + DDi + 15。

   AAA
   SomeValueDDi05
   SomeValueDDi10
   SomeValueDDi15

   BBB
   SomeValueDDi05
   SomeValueDDi10
   SomeValueDDi15

我的想法是在一个循环中写一个循环,每个键入IteratorList和Configuration标签(配置循环将在IteratorList循环中),这是使用XSLT的正确方法吗?

TIA,

约翰

1 个答案:

答案 0 :(得分:2)

解决方案1:这仅适用于提供的输入 这是代码:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" indent="yes"/>
  <xsl:variable name="newline" select="'&#13;'"/>

  <xsl:template match="row/cell/Selection/Config/Configuration">
    <xsl:value-of select="concat(.,$newline)"/><!--AAA, BBB, etc-->

    <xsl:for-each select="/row/cell/Notes/Text/IteratorDef/IteratorList/IteratorChoice">
      <xsl:variable name="zstring" select="."/>
      <xsl:for-each select="ancestor::IteratorDef/IteratorTag">
        <xsl:variable name="ystring" select="concat(.,$zstring)"/>
        <xsl:for-each select="ancestor::row/cell/Name">
          <xsl:value-of select="concat(normalize-space(.),$ystring,$newline)"/>
        </xsl:for-each>
      </xsl:for-each>
    </xsl:for-each>

    <!--this is for extra line space after each CONFIGURATION block-->
    <xsl:value-of select="$newline"/>
  </xsl:template>
  <xsl:template match="text()"/>
</xsl:stylesheet>
  

编辑:解决方案2 ..我没有使用反向XPath但完整的XPath ..   这适用于健壮的XML。方法略有不同..这里我   我从高级别(XPath)到较低级别。这是代码:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" indent="yes"/>
  <xsl:variable name="newline" select="'&#13;'"/>

  <xsl:template match="row/cell/Selection/Config/Configuration">
    <xsl:value-of select="concat(.,$newline)"/><!--AAA, BBB, etc-->

    <xsl:for-each select="/row/cell/Name">
      <xsl:variable name="astring" select="normalize-space(.)"/>
      <xsl:for-each select="/row/cell/Notes/Text/IteratorDef/IteratorTag">
        <xsl:variable name="bstring" select="concat($astring,normalize-space(.))"/>
        <xsl:for-each select="/row/cell/Notes/Text/IteratorDef/IteratorList/IteratorChoice">
          <xsl:value-of select="concat($bstring,normalize-space(.),$newline)"/>
        </xsl:for-each>
      </xsl:for-each>
    </xsl:for-each>

    <!--this is for extra line space after each CONFIGURATION block-->
    <xsl:value-of select="$newline"/>
  </xsl:template>

  <!--disable all unwanted text nodes-->
  <xsl:template match="text()"/>
</xsl:stylesheet>

如果这是你的XML:

<?xml version="1.0" encoding="UTF-8"?>
<row>
  <cell colname="1">
    <Name>
      SomeValue<Ref format="Ref" Idref="ABC"/>
    </Name>
  </cell>
  <cell colname="2">
    <Selection>
      <Config>
        <Configuration>AAA</Configuration>
      </Config>
      <Config>
        <Configuration>BBB</Configuration>
      </Config>
      <Config>
        <Configuration>CCC</Configuration>
      </Config>
    </Selection>
  </cell>
  <cell colname="6">
    <Notes>
      <Text>
        <IteratorDef>
          <IteratorTag Id="ABC">DDi</IteratorTag>
          <IteratorTag Id="ABC">EEi</IteratorTag>
          <IteratorList>
            <IteratorChoice>05</IteratorChoice>
            <IteratorChoice>10</IteratorChoice>
            <IteratorChoice>15</IteratorChoice>
            <IteratorChoice>20</IteratorChoice>
          </IteratorList>
        </IteratorDef>
      </Text>
    </Notes>
  </cell>
</row>

这将是您的输出:

AAA
SomeValueDDi05
SomeValueDDi10
SomeValueDDi15
SomeValueDDi20
SomeValueEEi05
SomeValueEEi10
SomeValueEEi15
SomeValueEEi20

BBB
SomeValueDDi05
SomeValueDDi10
SomeValueDDi15
SomeValueDDi20
SomeValueEEi05
SomeValueEEi10
SomeValueEEi15
SomeValueEEi20

CCC
SomeValueDDi05
SomeValueDDi10
SomeValueDDi15
SomeValueDDi20
SomeValueEEi05
SomeValueEEi10
SomeValueEEi15
SomeValueEEi20