找出某个id的所有元素是否具有相同的区域

时间:2014-01-15 14:12:27

标签: xml xslt xslt-grouping

我正在寻找以下问题的解决方案。我想知道具有特定ID的所有对象是否具有相同的区域:

<?xml version="1.0" encoding="UTF-8"?>
<TABLE>
    <ROW>
        <OBJECTID>Z001</OBJECTID>
        <DISTRICT>1234</DISTRICT>
    </ROW>
    <ROW>
        <OBJECTID>Z001</OBJECTID>
        <DISTRICT>1234</DISTRICT>
    </ROW>
    <ROW>
        <OBJECTID>Z001</OBJECTID>
        <DISTRICT>1234</DISTRICT>
    </ROW>
    <ROW>
        <OBJECTID>Z001</OBJECTID>
        <DISTRICT>1234</DISTRICT>
    </ROW>
    <ROW>
        <OBJECTID>Z001</OBJECTID>
        <DISTRICT>1234</DISTRICT>
    </ROW>
    <ROW>
        <OBJECTID>Z002</OBJECTID>
        <DISTRICT>5678</DISTRICT>
    </ROW>
    <ROW>
        <OBJECTID>Z002</OBJECTID>
        <DISTRICT>5678</DISTRICT>
    </ROW>
    <ROW>
        <OBJECTID>Z002</OBJECTID>
        <DISTRICT>3255</DISTRICT>
    </ROW>
    <ROW>
        <OBJECTID>Z002</OBJECTID>
        <DISTRICT>3457</DISTRICT>
    </ROW>
    <ROW>
        <OBJECTID>Z002</OBJECTID>
        <DISTRICT>8765</DISTRICT>
    </ROW>
</TABLE>

类似的东西:看看OBJECTID Z001。所有DISTRICT都相同吗?然后在转换后的xml中选择DISTRICT的值。不留空时。

我不知道如何管理这项任务。感谢您的帮助。

法里德

2 个答案:

答案 0 :(得分:1)

您可以使用Muenchian分组来完成此操作。这是一个例子:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

  <xsl:key name="kObjId" match="DISTRICT" use="../OBJECTID"/>

  <xsl:template match="/*">
    <n>
      <xsl:variable
        name="uniqueDistricts"
        select="ROW/DISTRICT[generate-id() = 
                             generate-id(key('kObjId', ../OBJECTID)[1])]
                            [not(. != key('kObjId', ../OBJECTID))]" />

      <xsl:apply-templates select="$uniqueDistricts" />
    </n>
  </xsl:template>

  <xsl:template match="DISTRICT">
    <district>
      <xsl:value-of select="." />
    </district>
  </xsl:template>
</xsl:stylesheet>

在样本输入上运行此操作时,结果为:

<n>
  <district>1234</district>
</n>

答案 1 :(得分:1)

在XSLT 2.0中,这是for-each-groupdistinct-values

的简单组合
<xsl:for-each-group select="/TABLE/ROW" group-by="OBJECTID">
  <xsl:if test="count(distinct-values(current-group()/DISTRICT)) = 1">
    <!-- all district values are the same for this OBJECTID -->
  </xsl:if>
</xsl:for-each-group>