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



当一个属性具有特定值时,我需要列出组中某个属性的唯一值。在 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 设置为基于位置标识节点中属性 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>

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

期望的输出

<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>

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

你需要做另一轮的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>

最新更新