【发布时间】:2014-01-22 13:43:03
【问题描述】:
我正在尝试在 xml 元素上使用 xslt 来实现分组,用于不真正共享任何共同值的属性,我不确定是否应该使用 muenchian grouping,尽管我已经使用此方法对元素进行了分组在我的代码中。我也在论坛中搜索过,但没有运气,因为大多数分组似乎都发生在具有共同值的属性上。
更具体地说,我想要实现的是在 pdf 上打印,一行用于多个记录,这些记录具有 Att 元素的特定 Id,属性 Ty 上的值为“PC”(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 上使用以下 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 是否有问题条件,并且由于某种原因它变成了错误的。
我错过了什么吗? 任何帮助将不胜感激,
谢谢
编辑: 正如 Tomalak 在我的案例中指出的那样,我正在尝试做的是实现手动组,这意味着代码中确实存在硬编码的条件。现在没有通用的方法可以为我计算这些值。
【问题讨论】:
-
您能否更详细地解释一下如何将不同的邮政编码分组?组是手动定义的还是可以计算的?这应该是可配置的还是硬编码的?
-
非常准确的问题,请查看我在页面底部编辑的答案。
标签: xml xslt-1.0 grouping muenchian-grouping