1

私はこのxmlファイルを持っています:

<A>
<B>
    <elt>Add</elt>
    ...there are some element here
    <bId>2</bId>
</B>
<B>
    <elt>Add</elt>
    ...there are some element here
    <bId>2</bId>
</B>
<B>
    <elt>Add</elt>
    ...there are some element here
    <bId>2</bId>
</B>
<B>
    <elt>can</elt>
    ...there are some element here
    <bId>3</bId>
</B>
<B>
    <elt>can</elt>
    ...there are some element here
    <bId>3</bId>
</B>

各bId要素の値を確認したいと思います。この値が前後のbId要素と同じである場合、変換後に拒否される要素bIdを除いて、ブロックBの他の要素を別のブロックに配置します。私の質問をあなたに理解させるために、ここに期待される出力があります:

<CA>
  <cplx>
    <spRule>
            <elt>Add</elt>
             ...
    </spRule>
    <spRule>
            <elt>Add</elt>
             ...
    </spRule>
    <spRule>
            <elt>Add</elt>
            ...
    </spRule>
  </cplx>
  <cplx>
    <spRule>
            <elt>can</elt>
            ...
    </spRule>
    <spRule>
            <elt>can</elt>
            ...
    </spRule>
  </cplx>
</CA>

xmlファイルの要素がbIdの値でソートされていない場合でも、同じ期待される出力を取得したいと思います。私はこのxslコードを使おうとしています:

<xsl:for-each select="bId"
  <CA>
    <cplx>
    <xsl:choose>
      <xsl:when test="node()[preceding::bId]">
         <xsl:copy>
          <xsl:copy-of select="@*"/>
          <xsl:apply-templates/>
         </xsl:copy>
      </xsl:when>
    </cplx>
  </CA>
</xsl:for-each>

しかし、それは歩きません。誰かが私を助けてくれませんか?ありがとう

4

1 に答える 1

2

同じ値で隣接する要素をグループ化する最初の説明がbId、XSLT1.0の方法で必要なものであると仮定すると次のようになります。

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

  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>

  <xsl:key name="k1"
    match="B[bId = preceding-sibling::B[1]/bId]"
    use="generate-id(preceding-sibling::B[not(bId = preceding-sibling::B[1]/bId)][1])"/>

  <xsl:template match="A">
    <CA>
      <xsl:apply-templates select="B[not(preceding-sibling::B[1]) or not(bId = preceding-sibling::B[1]/bId)]"/>
    </CA>
  </xsl:template>

  <xsl:template match="B">
    <cplx>
      <xsl:apply-templates select=". | key('k1', generate-id())" mode="sp"/>
    </cplx>
  </xsl:template>

  <xsl:template match="B" mode="sp">
    <spRule>
      <xsl:copy-of select="node()[not(self::bId)]"/>
    </spRule>
  </xsl:template>

</xsl:stylesheet>

すべてのB要素を同じbId値でグループ化するだけの場合は、

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

  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>

  <xsl:key name="k1"
    match="B"
    use="bId"/>

  <xsl:template match="A">
    <CA>
      <xsl:apply-templates select="B[generate-id() = generate-id(key('k1', bId)[1])]"/>
    </CA>
  </xsl:template>

  <xsl:template match="B">
    <cplx>
      <xsl:apply-templates select="key('k1', bId)" mode="sp"/>
    </cplx>
  </xsl:template>

  <xsl:template match="B" mode="sp">
    <spRule>
      <xsl:copy-of select="node()[not(self::bId)]"/>
    </spRule>
  </xsl:template>

</xsl:stylesheet>
于 2012-10-09T09:47:23.163 に答える