2

次の XML ドキュメントがあります。

<?xml version="1.0" encoding="UTF-8"?>
<cars>
    <car>
        <entrydata columnnumber="4" name="Colour">
            <text>Red</text>
        </entrydata>
    </car>
    <car>
        <entrydata columnnumber="4" name="Colour">
            <textlist>
                <text>Yellow</text>
                <text>Blue</text>
            </textlist>
        </entrydata>
    </car>
</cars>

そして、次の XSLT スタイルシート:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com: xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/">
    <records>
      <xsl:apply-templates select="//cars"/>
    </records>
  </xsl:template>
  <!--Top level template -->
  <xsl:template match="cars">
    <!-- Loop through each document (viewentry) and apply create the rows for each one-->
    <xsl:for-each select="car">
      <record>
        <xsl:attribute name="Colour">
          <xsl:value-of select="entrydata[@name='Colour']"/>
        </xsl:attribute>
       </record>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

これにより、次の出力が生成されます。

<?xml version="1.0" encoding="UTF-8"?>
<records>
    <record Colour="Red"/>
    <record Colour="YellowBlue"/>
</records>

出力が次のようになるように XSLT ファイルを変更するにはどうすればよいですか ( のコンマ区切りに注意してください<textlist>)。

<?xml version="1.0" encoding="UTF-8"?>
<records>
    <record Colour="Red"/>
    <record Colour="Yellow, Blue"/>
</records>
4

3 に答える 3

2

XSLT 2.0 を使用すると、

<record Colour="{string-join(entrydata[@name='Colour']/textlist/text, ', ')}"/>

XSLT 1.0 を使用する場合

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com: xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/">
    <records>
      <xsl:apply-templates select="//cars"/>
    </records>
  </xsl:template>
  <!--Top level template -->
  <xsl:template match="cars/car">
    <record>
        <xsl:attribute name="Colour">
          <xsl:apply-templates select="entrydata[@name='Colour']textlist/text"/>
        </xsl:attribute>
       </record>
  </xsl:template>
  <xsl:template match="textlist/text">
    <xsl:if test="position() > 1">
      <xsl:text>, </xsl:text>
    </xsl:if>
    <xsl:value-of select="."/>
  </xsl:template>
</xsl:stylesheet>
于 2012-07-10T10:19:28.143 に答える
2

生成された属性の名前にハードコードされた文字列を使用しない、それほど冗長ではない純粋な「プッシュ スタイル」の XSLT 1.0 ソリューション:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="cars">
     <records>
       <xsl:apply-templates/>
     </records>
 </xsl:template>
 <xsl:template match="car">
  <record>
   <xsl:apply-templates/>
  </record>
 </xsl:template>
 <xsl:template match="entrydata">
  <xsl:attribute name="{@name}">
   <xsl:apply-templates/>
  </xsl:attribute>
 </xsl:template>
 <xsl:template match="text">
  <xsl:if test="position() >1">, </xsl:if>
  <xsl:value-of select="."/>
 </xsl:template>
</xsl:stylesheet>

この変換が提供された XML ドキュメントに適用されると、次のようになります。

<cars>
    <car>
        <entrydata columnnumber="4" name="Colour">
            <text>Red</text>
        </entrydata>
    </car>
    <car>
        <entrydata columnnumber="4" name="Colour">
            <textlist>
                <text>Yellow</text>
                <text>Blue</text>
            </textlist>
        </entrydata>
    </car>
</cars>

必要な正しい結果が生成されます。

<records>
   <record Colour="Red"/>
   <record Colour="Yellow, Blue"/>
</records>

説明:

テンプレート、パターン マッチング、AVT、position()関数の適切な使用。


Ⅱ.よりシンプルな XSLT 2.0 ソリューション

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="cars">
     <records>
       <xsl:apply-templates/>
     </records>
 </xsl:template>
 <xsl:template match="car">
  <record>
   <xsl:apply-templates/>
  </record>
 </xsl:template>
 <xsl:template match="entrydata">
  <xsl:attribute name="{@name}">
   <xsl:value-of select=".//text" separator=", "/>
  </xsl:attribute>
 </xsl:template>
</xsl:stylesheet>

この変換を同じ XML ドキュメント (上記) に適用すると、同じ正しい結果が生成されます。

<records>
   <record Colour="Red"/>
   <record Colour="Yellow, Blue"/>
</records>

説明:

テンプレート、パターン マッチング、AVT、separator属性の適切な使用xsl:value-of

于 2012-07-10T12:10:28.963 に答える
1

この XSLT 1.0 スタイルシートは、そのトリックを行います ...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <records>
      <xsl:apply-templates select="*/*"/>
    </records>
  </xsl:template>

  <xsl:template match="car">
   <xsl:variable name="colour-list">
     <xsl:for-each select="entrydata[@name='Colour']/text |
                           entrydata[@name='Colour']/textlist/text">
       <xsl:value-of select="concat(.,', ')" />
     </xsl:for-each>  
   </xsl:variable>  
      <record Colour="{substring($colour-list,1,string-length($colour-list)-2)}"/>
  </xsl:template>
</xsl:stylesheet>

しかし、最も単純なソリューションの勝者は、この XSLT 2.0 スタイルシートです ...

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
 <records>
  <xsl:apply-templates select="*/*"/>
 </records>
</xsl:template>

<xsl:template match="car">
 <record Colour="{string-join(entrydata[@name='Colour']/(text | textlist/text),', ')}"/>
</xsl:template>
</xsl:stylesheet>
于 2012-07-10T10:21:39.750 に答える