1

次のようなドキュメントがあります。

<?xml version="1.0" encoding="UTF-8"?>
<document>
    <data attribute1="12" attribute2="1" attribute3="1">Director</data>
    <data attribute1="12" attribute2="1" attribute3="5">James</data>
    <data attribute1="12" attribute2="1" attribute3="8">Male</data>
    <data attribute1="12" attribute2="1" attribute3="9">10-Dec-1965</data>
    <data attribute1="12" attribute2="2" attribute3="18">James@gmail.com</data>
    <data attribute1="12" attribute2="2" attribute3="1">Chief Account</data>
    <data attribute1="12" attribute2="2" attribute3="5">Anna</data>
    <data attribute1="12" attribute2="2" attribute3="8">Female</data>
    <data attribute1="12" attribute2="1" attribute3="9">5-Aug-1980</data>
    <data attribute1="12" attribute2="2" attribute3="18">Anna@gmail.com</data>
</document>

これを次のように変換したい:

<Person>
    <Title>Director</Title>
    <FullName>James</FullName>
    <Gender>Male</Gender>
    <DateOfBirth>10-Dec-1965</DateOfBirth>
    <EmailAddress>James@gmail.com</EmailAddress>
</Person>
<Person>
    <Title>Chief Account</Title>
    <FullName>Anna</FullName>
    <Gender>Female</Gender>
    <DateOfBirth>5-Aug-1980</DateOfBirth>
    <EmailAddress>Anna@gmail.com</EmailAddress>
</Person>

私はこのxsltを使用しています:

<xsl:for-each select="document/data[@attribute1=12]">
    <Person>
        <xsl:choose>
            <xsl:when test="boolean(./@attribute3 = '1')">
                <Title>
                    <xsl:value-of select="./."/>
                </Title>
            </xsl:when>
            <xsl:when test="boolean(./@attribute3 = '5')">
                <FullName>
                    <xsl:value-of select="./."/>
                </FullName>
            </xsl:when>
            <xsl:when test="boolean(./@attribute3 = '8')">
                <Gender>
                    <xsl:value-of select="./."/>
                </Gender>
            </xsl:when>
            <xsl:when test="boolean(./@attribute3 = '9')">
                <DateOfBirth>
                    <xsl:value-of select="./."/>
                </DateOfBirth>
            </xsl:when>
            <xsl:when test="boolean(./@attribute3 = '18')">
                <EmailAddress>
                    <xsl:value-of select="./."/>
                </EmailAddress>
            </xsl:when>
        </xsl:choose>
    </Person>
</xsl:for-each>

<Person>問題は、タグが重複している次の出力が得られることです。

<Person>
    <Title>Director</Title>
</Person>    
<Person>    
    <FullName>James</FullName>
</Person>    
<Person>  
    <Gender>Male</Gender>
</Person>
<Person>    
    <DateOfBirth>10-Dec-1965</DateOfBirth>
</Person>    
<Person>    
    <EmailAddress>James@gmail.com</EmailAddress>
</Person>
<Person>
    <Title>Chief Account</Title>
</Person>
<Person>
    <FullName>Anna</FullName>
</Person>
<Person>
    <Gender>Female</Gender>
</Person>
<Person>
    <DateOfBirth>5-Aug-1980</DateOfBirth>
</Person>
<Person>
    <EmailAddress>Anna@gmail.com</EmailAddress>
</Person>

この問題を解決するのを手伝ってくれる人はいますか? ありがとうございました!

4

2 に答える 2

0

attribute3の値が 1 のデータ要素ごとに新しいPerson要素を作成したいようです。現在行っているようにすべてのデータ要素を反復するのではなく、関連する属性を持つ要素を選択するだけです。

<xsl:apply-templates select="data[@attribute3='1']"/>

次に、これらのデータ要素に対してのみPerson要素を出力するテンプレートを作成します。

<xsl:template match="data[@attribute3='1']">
   <Person>
      <Title><xsl:value-of select="." /></Title>
      <!-- Select other elements here -->
   </Person>
</xsl:template>

さて、他の要素を取得するために、これを達成する別の方法はキーを利用することです。実際には、attribute3値が「1」である最初の最も前のデータ要素によってデータ要素をグループ化します。

<xsl:key name="data" 
         match="data[@attribute3 != '1']" 
         use="generate-id(preceding-sibling::data[@attribute3 = '1'][1])" />

次に、他の要素を選択するために、現在のデータ要素の一意の ID をルックアップとして使用して、このキーを使用できます。

<xsl:apply-templates select="key('data', generate-id())" />

これにより、その特定の人物要素を構成するデータ要素のみが選択されます。次に、可能な属性値ごとに他のデータ要素と一致するテンプレートを作成します。

この XSLT を試してください:

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

   <xsl:key name="data" match="data[@attribute3 != '1']" use="generate-id(preceding-sibling::data[@attribute3 = '1'][1])" />

   <xsl:template match="document">
         <xsl:apply-templates select="data[@attribute3='1']"/>
   </xsl:template>

   <xsl:template match="data[@attribute3='1']">
      <Person>
         <Title><xsl:value-of select="." /></Title>
         <xsl:apply-templates select="key('data', generate-id())" />
      </Person>
   </xsl:template>

   <xsl:template match="data[@attribute3='5']">
      <FullName><xsl:value-of select="." /></FullName>
   </xsl:template>

    <xsl:template match="data[@attribute3='8']">
      <Gender><xsl:value-of select="." /></Gender>
   </xsl:template>

    <xsl:template match="data[@attribute3='9']">
      <DateOfBirth><xsl:value-of select="." /></DateOfBirth>
   </xsl:template>

    <xsl:template match="data[@attribute3='18']">
      <EmailAddress><xsl:value-of select="." /></EmailAddress>
   </xsl:template>
</xsl:stylesheet>
于 2013-08-24T11:24:05.860 に答える