仅针对 XSL 1.0 中的特定条件列出组内属性的唯一值

Posted

技术标签:

【中文标题】仅针对 XSL 1.0 中的特定条件列出组内属性的唯一值【英文标题】:List unique values of an attribute within a group only for specific criteria in XSL 1.0 【发布时间】:2015-09-06 01:45:46 【问题描述】:

当另一个属性具有特定值时,我需要列出组中某个属性的唯一值。在 XSL 1.0 中进行这项工作很难理解。

感谢另一篇文章,我现在定义了分组,以便在属性匹配特定条件时执行计数。但是,我很难仅列出一个特定属性的唯一值,其中另一个属性等于特定值,但仅在当前组的成员中。

与往常一样,这对于示例源数据和代码会更有意义。

这里是示例 XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Group1>
  <Group2>
    <LocationIdentification LocationID="0610390" />
    <LocationIdentification1 LocationQualifier="12" LocationID="USLGB"/>
  </Group2>
  <Group2>
    <LocationIdentification LocationID="0610612" />
    <LocationIdentification1 LocationQualifier="12" LocationID="USLAX"/>
  </Group2>
  <Group2>
    <LocationIdentification LocationID="0650004"/>
    <LocationIdentification1 LocationQualifier="12" LocationID="USLGB"/>
  </Group2>
  <Group2>
    <LocationIdentification LocationID="0650306"/>
    <LocationIdentification1 LocationQualifier="6" LocationID="USLAX"/>
   </Group2>
   <Group2>
    <LocationIdentification LocationID="0650220"/>
    <LocationIdentification1 LocationQualifier="12" LocationID="USLGB"/>
   </Group2>
</Group1>

我已将 XSL 设置为基于 LocationIdentification 节点中 LocationID 属性的前 3 个字符创建组。

XSL 1.0

<xsl:stylesheet version="1.0" 
<xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>

<xsl:key name="grp" match="Group2" use="substring(LocationIdentification/@LocationID, 1, 3)"/>

<xsl:template match="/Group1">
<table align="center" border="1">
    <thead>
        <tr>
            <th>Bay</th>
            <th>Units</th>
            <th>Locations</th>
        </tr>
    </thead>
    <tbody>
        <xsl:for-each select="Group2[generate-id()=generate-id(key('grp', substring(LocationIdentification/@LocationID, 1, 3))[1])]">
            <xsl:sort select="LocationIdentification/@LocationID"/>
            <xsl:variable name="curr-key" select="substring(LocationIdentification/@LocationID, 1, 3)" />           
            <xsl:variable name="curr-group" select="key('grp', $curr-key)" />
            <tr>
                <td>
                    <xsl:value-of select="$curr-key"/>
                </td>
                <td>
                    <xsl:value-of select="count($curr-group)"/>
                </td>
                <td>
                <!-- NEW CODE NEEDS TO GO HERE -->
                </td>
            </tr>
        </xsl:for-each>
    </tbody>
</table>
</xsl:template>

</xsl:stylesheet>

我需要弄清楚的是如何在 LocationQualifier='12' 的 LocationIdentification1 中列出 LocationID 的唯一值

期望的输出

<table align="center" border="1">
  <thead>
    <tr>
        <th>Bay</th>
        <th>Count</th>
        <th>Location(s)</th>
    </tr>
  </thead>
  <tbody>
   <tr>
    <td>061</td>
    <td>2</td>
    <td>USLGB USLAX</td>
  </tr>
  <tr>
    <td>065</td>
    <td>3</td>
    <td>USLGB</td>
  </tr>
</table>

请注意,Bay 65 的计数为 3,但只有 2 个成员的 LocationQualifier 为 12,并且它们都具有相同的 LocationID 值,因此输出应仅列出位置的 USLGB。

【问题讨论】:

【参考方案1】:

你需要再做一轮Muenchian分组:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>

<xsl:key name="grp" match="Group2" use="substring(LocationIdentification/@LocationID, 1, 3)"/>
<xsl:key name="loc12" match="LocationIdentification1[@LocationQualifier='12']" use="concat(substring(../LocationIdentification/@LocationID, 1, 3), '|', @LocationID)"/>

<xsl:template match="/Group1">
    <table align="center" border="1">
        <thead>
            <tr>
                <th>Bay</th>
                <th>Units</th>
                <th>Locations</th>
            </tr>
        </thead>
        <tbody>
            <xsl:for-each select="Group2[generate-id()=generate-id(key('grp', substring(LocationIdentification/@LocationID, 1, 3))[1])]">
                <xsl:sort select="LocationIdentification/@LocationID"/>
                <xsl:variable name="curr-key" select="substring(LocationIdentification/@LocationID, 1, 3)" />           
                <xsl:variable name="curr-group" select="key('grp', $curr-key)" />
                <tr>
                    <td>
                        <xsl:value-of select="$curr-key"/>
                    </td>
                    <td>
                        <xsl:value-of select="count($curr-group)"/>
                    </td>
                    <td>
                        <xsl:for-each select="$curr-group/LocationIdentification1[@LocationQualifier='12'][generate-id()=generate-id(key('loc12', concat($curr-key, '|', @LocationID))[1])]">
                            <xsl:value-of select="@LocationID"/>
                            <xsl:text> </xsl:text>
                        </xsl:for-each>
                    </td>
                </tr>
            </xsl:for-each>
        </tbody>
    </table>
</xsl:template>

</xsl:stylesheet>

【讨论】:

以上是关于仅针对 XSL 1.0 中的特定条件列出组内属性的唯一值的主要内容,如果未能解决你的问题,请参考以下文章

使用匹配特定选择器的 XSL 对 XML 进行排序并保持 XML 相同

需要我的XSLT仅从XML输出特定作者(如果可用)

如何仅在该特定组内按结果查找组中未排序的一组行?

R条件求和(组内条件)

在特定条件下计算组内的百分比

如何执行条件“executeFetchRequest”以仅获取特定属性为零的实体? [复制]