1

ソース XML があります

<Cars>
  <Car>
    <Make>Fiat</Make>
    <Colors>
      <Color>RED</Color>
      <Color>BLUE</Color>
    </Colors>
  </Car>
  <Car>
    <Make>Volvo</Make>
    <Colors>
      <Color>RED</Color>
      <Color>WHITE</Color>
    </Colors>
  </Car>
  <Car>
    <Make>Renault</Make>
    <Colors>
      <Color>BLUE</Color>
      <Color>BLACK</Color>
    </Colors>
  </Car>
</Cars>

次のようなものに変換したい

<Cars>
  <Detail>
    <Name>MakeName</Name>
    <Entry>Fiat</Entry>
    <Entry>Volvo</Entry>
    <Entry>Renault</Entry>
  </Detail>
  <Detail>
    <Name>AvailableColors</Name>
    <Entry>RED</Entry>
    <Entry>BLUE</Entry>
    <Entry>WHITE</Entry>
    <Entry>BLACK</Entry>
  </Detail>
<Cars>

私は XSL を初めて使用し、半分の処理を行うために XSL を作成しましたが、色をターゲットの個別の要素として取得することに行き詰まっています

<xsl:template match="/">
  <Cars>
    <xsl:apply-templates />
  </Cars>
</xsl:template>

<xsl:template match="Cars">
  <xsl:apply-templates select="Car" />
</xsl:template>

<xsl:template match="Car">
  <Detail>
    <Name>MakeName</Name>
    <xsl:apply-templates select="Make" />
  </Detail>
</xsl:template>

<xsl:template match="Make">
  <Entry><xsl:value-of select"text()"/></Entry>
</xsl:template>

用の XSL を作成できません。XSL を初めて使用<Name>AvailableColors</Name>するので、どんな助けも大歓迎です

4

2 に答える 2

2

これは、 Muenchianグループ化を使用して重複する色を排除する方法を示すXSLT1.0スタイルシートです。

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

<xsl:output indent="yes"/>

<xsl:key name="k1" match="Car/Colors/Color" use="."/>

<xsl:template match="Cars">
  <xsl:copy>
    <Detail>
      <Name>MakeName</Name>
      <xsl:apply-templates select="Car/Make"/>
    </Detail>
    <Detail>
      <Name>AvailableColors</Name>
      <xsl:apply-templates select="Car/Colors/Color[generate-id() = generate-id(key('k1', .)[1])]"/>
    </Detail>
  </xsl:copy>
</xsl:template>

<xsl:template match="Car/Make | Colors/Color">
  <Entry>
    <xsl:value-of select="."/>
  </Entry>
</xsl:template>

</xsl:stylesheet>
于 2012-06-29T11:34:19.883 に答える
0

この回答にある一般的な「シュレッディング」ソリューションを参照してください

https://stackoverflow.com/a/8597577/36305

于 2012-06-29T12:49:59.427 に答える