【问题标题】:XSLT sibling from grandparent level祖父母级别的 XSLT 兄弟姐妹
【发布时间】:2013-03-11 16:20:20
【问题描述】:

我有一个如下所示的 xml。我需要找到所有不同的货币。使用以下

 <xsl:for-each select="$itemPrices/Relationships/Relationship/Target
                       /Properties/PropertyItem[cs:Key='Currency']/cs:Value">

我已经能够得到所有的货币类型,但是有重复。我需要使用 XSLT 1.0 找到不同的值。我遇到了使用前后兄弟姐妹的解决方案,但我能够获得相同级别的兄弟姐妹。我无法构建一个 XPath,它可以升级四级中的三级,然后查看可比较的下一个兄弟。

  <Relationship>
    <ModelName>Entities.Relationship</ModelName>
    <Properties />
    <Target>
      <ModelName>ItemPrice</ModelName>
      <Properties>
        <PropertyItem>
          <Key>Currency</Key>
          <Value i:type="a:string" xmlns:a="http://www.w3.org/2001/XMLSchema">US</Value>
        </PropertyItem>
        <PropertyItem>
          <Key>PriceValue</Key>
          <Value i:type="a:decimal" xmlns:a="http://www.w3.org/2001/XMLSchema">13.51</Value>
        </PropertyItem>
        <PropertyItem>
          <Key>ProductId</Key>
          <Value i:type="a:string" xmlns:a="http://www.w3.org/2001/XMLSchema">0600</Value>
        </PropertyItem>
      </Properties>
    </Target>
  </Relationship>
  <Relationship>
    <ModelName>Entities.Relationship</ModelName>
    <Properties />
    <Target>
      <ModelName>ItemPrice</ModelName>
      <Properties>
        <PropertyItem>
          <Key>Currency</Key>
          <Value i:type="a:string" xmlns:a="http://www.w3.org/2001/XMLSchema">US</Value>
        </PropertyItem>
        <PropertyItem>
          <Key>PriceValue</Key>
          <Value i:type="a:decimal" xmlns:a="http://www.w3.org/2001/XMLSchema">11.82</Value>
        </PropertyItem>
        <PropertyItem>
          <Key>ProductId</Key>
          <Value i:type="a:string" xmlns:a="http://www.w3.org/2001/XMLSchema">0600</Value>
        </PropertyItem>
      </Properties>
    </Target>
  </Relationship>
  <Relationship>
    <ModelName>Entities.Relationship</ModelName>
    <Properties />
    <Target>
      <ModelName>ItemPrice</ModelName>
      <Properties>
        <PropertyItem>
          <Key>Currency</Key>
          <Value i:type="a:string" xmlns:a="http://www.w3.org/2001/XMLSchema">Canadian</Value>
        </PropertyItem>
        <PropertyItem>
          <Key>PriceValue</Key>
          <Value i:type="a:decimal" xmlns:a="http://www.w3.org/2001/XMLSchema">10.95</Value>
        </PropertyItem>
        <PropertyItem>
          <Key>ProductId</Key>
          <Value i:type="a:string" xmlns:a="http://www.w3.org/2001/XMLSchema">0600</Value>
        </PropertyItem>
      </Properties>
    </Target>
  </Relationship>

所以在上面的 XML 中,我应该只得到一次美国和加拿大,而不是美国两次和加拿大一次。我怎样才能做到这一点?

【问题讨论】:

    标签: xslt xpath xslt-grouping


    【解决方案1】:

    虽然您可以使用 preceding:: 而不是 preceding-sibling::,但在 XSLT 1.0 中选择不同值的有效方法是使用 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="kCurrency" match="PropertyItem[Key = 'Currency']/Value"
               use="."/>
    
      <xsl:template match="/">
        <xsl:variable name="allCurrencies"
                      select="Relationships/Relationship/Target/Properties
                              /PropertyItem[Key = 'Currency']/Value" />
        <xsl:for-each select="$allCurrencies[generate-id() = 
                      generate-id(key('kCurrency', .)[1])]">
          <currency>
            <xsl:value-of select="."/>
          </currency>
        </xsl:for-each>
      </xsl:template>
    </xsl:stylesheet>
    

    &lt;Relationships&gt; 元素包裹在您的示例 XML 中并输入到此 XSLT 中时,结果是:

    <currency>US</currency>
    <currency>Canadian</currency>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-31
      • 2021-09-23
      • 2019-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多