XSLT输出不同的值

时间:2018-09-23 00:25:42

标签: xml xslt

我有这个XML:

<LocatorInfo>
 <LocationAddr>
            <StreetNum>182</StreetNum>
            <StreetName>Eastern Avenue</StreetName>
            <City>Fall River</City>
            <StateProv>MA</StateProv>
            <PostalCode>02723</PostalCode>
            <Country>USA</Country>
            <GEOCode>
                <Latitude>41.694600119635915</Latitude>
                <Longitude>-71.13038416718094</Longitude>
            </GEOCode>
        </LocationAddr>
        <LocationTimestamp>4/6/2018 4:17:37 PM</LocationTimestamp>
        <LocationDistance>4.338 miles</LocationDistance>
        <Accuracy>1.985 miles</Accuracy>
    </LocatorInfo>
<LocatorInfo>
    <LocationAddr>
      <StreetNum>285</StreetNum>
      <StreetName>Wastern Avenue</StreetName>
      <City>Ball River</City>
      <StateProv>MA</StateProv>
      <PostalCode>02723</PostalCode>
      <Country>USA</Country>
      <GEOCode>
        <Latitude>41.694600119635915</Latitude>
        <Longitude>-71.13038416718094</Longitude>
      </GEOCode>
    </LocationAddr>
    <LocationTimestamp>4/7/2018 7:00:03 AM</LocationTimestamp>
    <LocationDistance>4.338 miles</LocationDistance>
    <Accuracy>1.985 miles</Accuracy>
  </LocatorInfo>
<LocatorInfo>
    <LocationAddr>
      <StreetNum>182</StreetNum>
      <StreetName>Eastern Avenue</StreetName>
      <City>Fall River</City>
      <StateProv>MA</StateProv>
      <PostalCode>02723</PostalCode>
      <Country>USA</Country>
      <GEOCode>
        <Latitude>41.694600119635915</Latitude>
        <Longitude>-71.13038416718094</Longitude>
      </GEOCode>
    </LocationAddr>
    <LocationTimestamp>4/7/2018 7:00:03 AM</LocationTimestamp>
    <LocationDistance>4.338 miles</LocationDistance>
    <Accuracy>1.985 miles</Accuracy>
  </LocatorInfo>

我需要找到一种方法来在一个段落(StreetNum,StreetName,City,StateProv)中输入每个不同的地址,基本上只是不输出重复项。并计算每个地址出现的次数。

我不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

我相信xml spy支持XSLT 2.0及更高版本,因此您应该能够使用xsl:for-each-group来获得不同的分组。

尝试使用此XSLT

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" />

    <xsl:template match="/*">
      <Addresses>
        <xsl:for-each-group select="LocatorInfo/LocationAddr" group-by="string-join((StreetNum, StreetName, City, StateProv), '!')">
          <Address>
            <xsl:copy-of select="StreetNum, StreetName, City, StateProv" />
            <Count>
              <xsl:value-of select="count(current-group())" />
            </Count>
        </Address>
        </xsl:for-each-group>
      </Addresses>
    </xsl:template>
</xsl:stylesheet>