2

同様の質問に対する多くの回答を見ることができますが、それらをうまく機能させることができないようです。同じタグ名を持ついくつかの兄弟要素ノードを持ついくつかのxmlファイルがあります。XSLT を使用してこれらのノードをマージしたいと考えています。どんな助けでも大歓迎です。

入力:

<?xml version="1.0"?>
<Screen>
  <Shapes>
    <Triangle id="tri1">
    <color>red</color>
    <size>large</size>
    </Triangle>
  </Shapes>
  <Shapes>
    <Rectangle id="rec1">
      <color>blue</color>
      <size>medium</size>
    </Rectangle>
  </Shapes>
  <Shapes>
    <Circle id="cir1">
      <color>green</color>
      <size>small</size>
    </Circle>
  </Shapes>
  <Shapes>
    <Square id="sqr1">
      <color>yellow</color>
      <size>large</size>
    </Square>
  </Shapes>
  <Device>
    <Name>peg</Name>
    <type>X11</type>
  </Device>
  <Utilities>
    <Software>QT</Software>
    <Platform>Linux</Platform>
  </Utilities>
</Screen>

すべての「形状」ノードをマージしたい。 必要な出力

<?xml version="1.0"?>
<Screen>
  <Shapes>
    <Triangle id="tri1">
      <color>red</color>
      <size>large</size>
    </Triangle>

    <Rectangle id="rec1">
      <color>blue</color>
      <size>medium</size>
    </Rectangle>

    <Circle id="cir1">
      <color>green</color>
      <size>small</size>
    </Circle>

    <Square id="sqr1">
      <color>yellow</color>
      <size>large</size>
    </Square>
  </Shapes>
  <Device>
    <Name>peg</Name>
    <type>X11</type>
  </Device>
  <Utilities>
    <Software>QT</Software>
    <Platform>Linux</Platform>
  </Utilities>
</Screen>

私が試したXSLTは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
  <xsl:output indent="yes" />
  <xsl:template match="Shapes">
    <xsl:if test="not(preceding-sibling::*[local-name() = 'Shapes'])">

      <Shapes>
        <xsl:apply-templates select="node() | @*" />
        <xsl:apply-templates select="following-sibling::*[local-name() = 'Shapes']" />
      </Shapes>
    </xsl:if>
    <xsl:if test="preceding-sibling::*[local-name() = 'Shapes']">
      <xsl:apply-templates select="node() | @*" />
    </xsl:if>
  </xsl:template>
  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

しかし、私が得た出力は ( :( )

<Screen>
<Shapes>
    <Triangle id="tri1">
      <color>red</color>
      <size>large</size>
    </Triangle>

    <Rectangle id="rec1">
      <color>blue</color>
      <size>medium</size>
    </Rectangle>

    <Circle id="cir1">
      <color>green</color>
      <size>small</size>
    </Circle>

    <Square id="sqr1">
      <color>yellow</color>
      <size>large</size>
    </Square>
  </Shapes>

    <Rectangle id="rec1">
      <color>blue</color>
      <size>medium</size>
    </Rectangle>
    <Circle id="cir1">
      <color>green</color>
      <size>small</size>
    </Circle>
    <Square id="sqr1">
      <color>yellow</color>
      <size>large</size>
    </Square>

  <Device>
    <Name>peg</Name>
    <type>X11</type>
  </Device>
  <Utilities>
    <Software>QT</Software>
    <Platform>Linux</Platform>
  </Utilities>
</Screen>

使用できる単純な XSLT コードはありますか、または出力を取得するために適用できる xslt の変更はありますか?

4

1 に答える 1

9

これは仕事をするはずです:

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

  <xsl:output method="xml" indent="yes"/>

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

  <xsl:template match="/*">
    <xsl:copy>
      <Shapes>
        <xsl:copy-of select="Shapes/*"/>
      </Shapes>
      <xsl:apply-templates select="*[name()!='Shapes']"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

アイデアは、 のすべてのサブ要素をShapes一度に別々に処理してから、残りをコピーすることです。

于 2013-08-29T15:58:00.510 に答える