0

XLST 1.0 を使用して、「Filter me out」または「And filter me too」でbb要素を持たないaa要素を取得する必要があります。

<data>
    <aa>
        <bb>Filter me out</bb>
        <bb>Some information</bb>
    </aa>
    <aa>
        <bb>And filter me out too</bb>
        <bb>Some more information</bb>
    </aa>
    <aa>
        <bb>But, I need this information</bb>
        <bb>And I need this information</bb>
    </aa>
</data>

正しいaa要素を取得したら、次のように各bb要素を出力します。

<notes>
    <note>But, I need this information</note>
    <note>And I need this information</note>
</notes>

どうもありがとう。

4

1 に答える 1

2

この種の標準的なアプローチは、テンプレートを使用することです

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <!-- copy everything as-is from input to output unless I say otherwise -->
  <xsl:template match="@*|node()">
    <xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
  </xsl:template>

  <!-- rename aa to notes -->
  <xsl:template match="aa">
    <notes><xsl:apply-templates select="@*|node()" /></notes>
  </xsl:template>

  <!-- and bb to note -->
  <xsl:template match="bb">
    <note><xsl:apply-templates select="@*|node()" /></note>
  </xsl:template>

  <!-- and filter out certain aa elements -->
  <xsl:template match="aa[bb = 'Filter me out']" />
  <xsl:template match="aa[bb = 'And filter me out too']" />
</xsl:stylesheet>

これらの最後の 2 つのテンプレートは、必要のない特定の要素に一致し、aaもしません。特定のフィルタリング テンプレートに一致しない要素は、特定性の低いテンプレートに一致し、名前が に変更されます。aa<xsl:template match="aa">notes

特定のテンプレートがないものはすべて、最初の「アイデンティティ」テンプレートによってキャッチされ、変更されずに出力にコピーされます。これには、すべての要素をラップする親要素が含まaaれます (例では提供していませんが、存在する必要があります。そうしないと、入力が整形式の XML になりません)。

于 2013-07-16T14:36:39.767 に答える