0

一致するノードがない場合に (a) ノードの値を出力したい 2 つのノード セットがあります。ロジックは次のようになります。

ノード セット #1 の属性「用語」の値のいずれかがノード セット #2 の「キー」属性の値のいずれとも一致しない場合、ノード セット #1 から「用語」の値を出力します。どうすればいいですか?

ノードセット #1

    <stuff term="foo" />
    <stuff term="bar" />
    <stuff term="test" />

ノードセット #2

    <other key="time" />
    <other key="rack" />
    <other key="foo" />
    <other key="fast" />
4

1 に答える 1

1

XML が次のようになっているとします。

<record>
   <stuff>
      <stuff term="foo"/>
      <stuff term="bar"/>
      <stuff term="test"/>
   </stuff>
   <other>
      <other key="time"/>
      <other key="rack"/>
      <other key="foo"/>
      <other key="fast"/>
   </other>
</record>

キー属性に基づいて他の要素を検索するキーを設定できます

<xsl:key name="other" match="other/*" use="@key"/>

次に、一致する他の要素がないもの要素を選択するには、次のようにキーを使用します

<xsl:apply-templates select="stuff/*[not(key('other', @term))]"/>

次の XSLT を試してください

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>
   <xsl:key name="other" match="other/*" use="@key"/>

   <xsl:template match="/*">
      <xsl:apply-templates select="stuff/*[not(key('other', @term))]"/>
   </xsl:template>

   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>
</xsl:stylesheet>

上記の XML に適用すると、次のように出力されます。

<stuff term="bar"></stuff>
<stuff term="test"></stuff>
于 2013-02-01T17:16:47.763 に答える