0

次のようなxmlがあります。私はすべての異なる通貨を見つける必要があります。以下を使用する

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

すべての通貨タイプを取得できましたが、重複しています。XSLT1.0を使用して個別の値を見つける必要があります。前後の兄弟を使用するソリューションに出くわしましたが、同じレベルの兄弟を取得することができました。4つのレベルのうち3つを上に移動し、同等の次の兄弟を調べる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では、米国とカナダを1回だけ取得する必要があります。米国を2回、カナダを1回取得する必要はありません。どうやってやるの?

4

1 に答える 1

2

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>

<Relationships>要素がサンプルXMLにラップされ、このXSLTにフィードされると、結果は次のようになります。

<currency>US</currency>
<currency>Canadian</currency>
于 2013-03-11T16:38:03.330 に答える