我正在尝试在 xml 元素上使用 xslt 来实现一个分组,这些属性并不真正共享任何共同值,我不确定是否应该使用muenchian 分组 ,尽管我已经在我的代码中使用这种方法对我的元素进行了分组。我也在论坛中搜索过,但没有运气,因为大多数分组似乎都发生在具有共同值的属性上。
更具体地说,我想要实现的是在 pdf 上打印,一行用于多个记录,这些记录具有 Att 元素的特定 ID,属性 Ty(Att ty="PC")上的值为“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 上使用以下 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>
尽管我过去已经为在现有分组中实际共享公共属性值的元素实现了这个逻辑,但上面的代码根本没有为我想要的聚合提供任何行,我想知道我的 OR 条件和 for 是否有问题出于某种原因,它变得虚假。
我错过了什么吗?任何帮助将非常感激,
谢谢
编辑:正如 Tomalak 在我的案例中指出的那样,我想要做的是实现手动组,这意味着代码中确实是硬编码的条件。现在没有通用的方法可以为我计算这些值。