XSLT V1.0-如何将不平衡的分组分组



我正在尝试使用XML元素上的XSLT来实现分组,这些属性实际上并未共享任何共同值,我不确定是否应该使用Muenchian分组,尽管我是否应该使用Muenchian分组已经将我的元素与我的代码中的此方法分组。我还在论坛上搜索了,但是没有运气,因为大多数分组似乎都发生在具有共同价值的属性上。

更具体地说,我要实现的目标是在pdf上打印,一行,用于一个以上的记录,这些记录具有特定的ID,该记录具有属性ty上具有值" PC"的特定ID(att ty =" pc")。所有这些都应该与我已经存在的分组一起发生。

我的XML代码的示例:

<Document>
    <RecordDetails>
        <Record>
            <contact id="0001" title="Mr" forename="John" surname="Smith" ST='M'/>
            <AggSet>                
                <Att Ty="Addr" Id="43 Menelaou Street" />
                <Att Ty="PC"   Id="15230" />
                <Att Ty="Num"  Id="2580052635" />             
            </AggSet>
            <Charge Amount="3.000" PT="P" />
        </Record>
        <Record>
            <contact id="0001" title="Mr" forename="John" surname="Smith" ST='M'/>
            <AggSet>                
                <Att Ty="Addr" Id="65 Dankan Street" />
                <Att Ty="PC"   Id="15236" />
                <Att Ty="Num"  Id="2580052635" />             
            </AggSet>
            <Charge Amount="10.000" PT="P" />
        </Record>
        <Record>
            <contact id="0002" title="Dr" forename= "Amy" surname="Jones" ST='Y'/>
            <AggSet>                
                <Att Ty="Addr" Id="28 Karman Street" />
                <Att Ty="PC"   Id="15237" />
                <Att Ty="Num"  Id="2584552635" />             
            </AggSet>
            <Charge Amount="-2.000" PT="P" />
        </Record>
        <Record>    
            ...
        </Record>
    </RecordDetails>
</Document>

因此,例如,对于记录2,3,我只想打印1行,因为它们的邮政编码属于我的同一区域,因为ty =" pc"是指帖子,我正在尝试将更大的面积基础。

我正在使用Apache FOP上的colloct XSL:

<xsl:key name="ct" match="Record[Charge/@PT='P']" use="@ST"/>

<xsl:template match ="RecordDetails">
    <xsl:for-each select="Record[generate-id(.)=generate-id(key('ct',@ST)[1])]">
        <xsl:if test="@ST='M' and (./AggSet/Att[@Ty='PC']/@Id='15236' or ./AggSet/Att[@Ty='TZ']/@Id='15237' or ... )  ">
            <fo:table-row>                          
                    <xsl:apply-templates select="."/>                            
            </fo:table-row>
        </xsl:if> 
        <xsl:for-each select="key('ct',@ST)">                       
            <xsl:choose>                                    
                 <xsl:when test="@ST='M' and (./AggSet/Att[@Ty='PC']/@Id='15236' or ./AggSet/Att[@Ty='TZ']/@Id='15237' or ... )  "> 
                 </xsl:when>                     
                 <xsl:otherwise>
                  <fo:table-row>
                    <xsl:apply-templates select="."/>
                  </fo:table-row>
                 </xsl:otherwise>
            </xsl:choose>             
        </xsl:for-each>                             
    </xsl:for-each>
</xsl:template>
<xsl:template match="Record">
    <fo:table-cell>
        <xsl:choose>
            <xsl:when test="@ST='M' and (./AggSet/Att[@Ty='PC']/@Id='15236' or ./AggSet/Att[@Ty='TZ']/@Id='15237' or ... )">
                <fo:block text-align="center">
                    <xsl:text>Greater area</xsl:text>
                </fo:block>
            </xsl:when>
            <xsl:otherwise>
                <fo:block text-align="center">
                    <xsl:value-of select="./AggSet/Att[@Ty='PC']/@Id" />
                </fo:block>
            </xsl:otherwise>
        </xsl:choose>
    </fo:table-cell>
    <fo:table-cell>
        <xsl:choose>
            <xsl:when test="@ST='M' and (./AggSet/Att[@Ty='PC']/@Id='15236' or ./AggSet/Att[@Ty='TZ']/@Id='15237' or ... )">
                <fo:block text-align="center">
                    <xsl:value-of select="sum(//Record[@ST='M' and (./AggSet/Att[@Ty='PC']/@Id='15236' or ./AggSet/Att[@Ty='TZ']/@Id='15237' or ... )]/contact/Charge/@Amount)" />
                </fo:block>
            </xsl:when>
            <xsl:otherwise>
                <fo:block text-align="center">
                    <xsl:value-of select="./Charge/@Amount" />
                </fo:block>
            </xsl:otherwise>
        </xsl:choose>
    </fo:table-cell>
</xsl:template>

尽管我过去曾在现有分组中实际上共享一个共同属性值的元素实现了这种逻辑,但是上述代码对我想要的汇总根本没有任何行,我想知道我的或条件以及由于某种原因变成错误的。

我想念什么吗?任何帮助将不胜感激,

谢谢

编辑:正如Tomalak在我的情况下指出的那样,我试图做的是实施手动组,这意味着代码内确实是经过硬编码的条件。现在没有通用方法可以为我计算这些值。

这种方法怎么样:

<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:my="http://tempuri.org/config"
  exclude-result-prefixes="my"
>
  <my:config>
    <PC_group>
      <item>15236</item>
      <item>15237</item>
    </PC_group>
    <!-- more groups like this... -->
  </my:config>
  <!-- create a reference to our own config -->
  <xsl:variable name="config" select="document('')/*/my:config" />
  <xsl:variable name="PC_group" select="$config/PC_group" />
  <xsl:template match="RecordDetails">
    <grouped_RecordDetails>
      <xsl:apply-templates mode="group" select="Record[Charge/@PT='P']" />
    </grouped_RecordDetails>
  </xsl:template>
  <xsl:template match="Record" mode="group">
    <xsl:variable name="myPC" select="AggSet/Att[@Ty = 'PC']/@Id" />
    <!-- select all the PCs in this group -->
    <xsl:variable name="groupPCs" select="$PC_group[item = $myPC]/item" />
    <!-- identify all other members of this group -->
    <xsl:variable name="groupMembers" select=". | ../Record[
      Charge/@PT='P' and AggSet/Att[@Ty = 'PC']/@Id = $groupPCs
    ]" />
    <!-- do the actual grouping, just like the Muenchian method... -->
    <xsl:if test="generate-id() = generate-id($groupMembers[1])">
      <!--
        we are at the first Record in this group now
        all the other group members are at $groupMembers
        output whatever details you like here
      -->
      <xsl:copy-of select="." />
    </xsl:if>
  </xsl:template>
  <xsl:template match="text()[normalize-space() = '']" />
</xsl:stylesheet>

样本输出

<grouped_RecordDetails>
  <Record>
    <contact id="0001" title="Mr" forename="John" surname="Smith" ST="M" />
    <AggSet>
      <Att Ty="Addr" Id="43 Menelaou Street" />
      <Att Ty="PC" Id="15230" />
      <Att Ty="Num" Id="2580052635" />
    </AggSet>
    <Charge Amount="3.000" PT="P" />
  </Record>
  <Record>
    <contact id="0001" title="Mr" forename="John" surname="Smith" ST="M" />
    <AggSet>
      <Att Ty="Addr" Id="65 Dankan Street" />
      <Att Ty="PC" Id="15236" />
      <Att Ty="Num" Id="2580052635" />
    </AggSet>
    <Charge Amount="10.000" PT="P" />
  </Record>
</grouped_RecordDetails>

编辑:当然,如果您喜欢:

<!-- identify all members of this group -->
<xsl:variable name="groupMembers" select="
  . | ../Record[
    Charge/@PT = 'P' 
    and AggSet/Att[@Ty = 'PC']/@Id = $PC_group[
          item = current()/AggSet/Att[@Ty = 'PC']/@Id
        ]/item
  ]
" />

将其分成几个变量使得易于遵循。

相关内容

  • 没有找到相关文章

最新更新