0

私の入力xmlは次のようになります。

<items>
    <item>
        <geoDetails>
            <street>xxx</street>
            <city>yyy</city>
            <state>zzz</state>
        </geoDetails>
        <otherDetails>
            <desc>abcd111</desc>
            <comments>good item</comments>
        </otherDetails>
        <key>
            <name>item1</name>
            <id>123</id>
            <color>red</color>
        </key>
        <misc>
            <available>false</available>
            <details>geo</details>
        </misc>
    </item>
    <item>
        <otherDetails>
            <desc>efgh222</desc>
            <comments>good item</comments>
        </otherDetails>
        <key>
            <name>item2</name>
            <id>123</id>
            <color>red</color>
        </key>
        <misc>
            <available>false</available>
            <details>other</details>
        </misc>
    </item>
    <item>
        <geoDetails>
            <street>ppp</street>
            <city>qqq</city>
            <state>rrr</state>
        </geoDetails>
        <otherDetails>
            <desc>ijkl333</desc>
            <comments>best item</comments>
        </otherDetails>
        <key>
            <name>item3</name>
            <id>456</id>
            <color>blue</color>
        </key>
        <misc>
            <available>false</available>
            <details>other</details>
        </misc>
    </item>
</items>

'item' ノードをグループ化するためのキーは concat(/items/item/key/id,/items/item/key/color) です。特定されたグループごとに、以下に示すロジックでマージを行う必要があります。

a. その他/詳細が「geo」である「item」から「geoDetails」を抽出します。

b. その他/詳細が「その他」である「アイテム」から「その他の詳細」を抽出します。「geo」または「other」のいずれかの値を持つ各「item」には、1 つのその他/詳細のみが存在します。

c. 特定のグループの最初の「アイテム」から「キー」要素を抽出します。

d. 「misc」要素には「available」要素がそのまま含まれている必要があり (これは常に「false」であるため、これを 1 回入力するだけで十分です)、「details」要素には要素「geoDetails」または「otherDetails」に基づいた値が入力されている必要があります。上記の手順 a および b で指定されたロジックを介して。「geoDetails」と「otherDetails」の両方が設定されている場合、2 つの「details」要素が必要です。

e. グループの一部ではない他の単一の「アイテム」要素は、そのまま出力にプッシュする必要があります。

そして、上記のロジックに基づく予想される出力は次のとおりです。

<items>
<item>
    <geoDetails>
        <street>xxx</street>
        <city>yyy</city>
        <state>zzz</state>
    </geoDetails>
    <otherDetails>
        <desc>efgh222</desc>
        <comments>good item</comments>
    </otherDetails>
    <key>
        <name>item1</name>
        <id>123</id>
        <color>red</color>
    </key>
    <misc>
        <available>false</available>
        <details>geo</details>
        <details>other</details>
    </misc>
</item>
<item>
    <geoDetails>
        <street>ppp</street>
        <city>qqq</city>
        <state>rrr</state>
    </geoDetails>
    <otherDetails>
        <desc>ijkl333</desc>
        <comments>best item</comments>
    </otherDetails>
    <key>
        <name>item3</name>
        <id>456</id>
        <color>blue</color>
    </key>
    <misc>
        <available>false</available>
        <details>other</details>
    </misc>
</item>

xsl:key と apply-templates を使用して Muenchian グループ化に基づく変換を試みました。「アイテム」要素をグループ化することはできましたが、上記の一連の条件に基づいてこれらのグループ化された要素をマージする方法についてこれ以上進めることができませんでした。

XSLT 変換:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" version="1.0" exclude-result-prefixes="exsl">
<xsl:key match="item" name="itemKey" use="concat(key/id,key/color)" />
<xsl:template match="/">
    <xsl:variable name="output">
        <xsl:apply-templates />
    </xsl:variable>
    <xsl:copy-of select="exsl:node-set($output)/*" />
</xsl:template>
<!-- default template -->
<xsl:template match="node( ) | @*">
    <xsl:copy>
        <xsl:apply-templates select="@*" />
        <xsl:apply-templates />
    </xsl:copy>
</xsl:template>
<xsl:template match="item[generate-id(.)=generate-id(key('itemKey',concat(key/id,key/color))[1])]">
    <xsl:copy>
        <xsl:apply-templates select="@* | key('itemKey',concat(key/id,key/color))/node()" />
    </xsl:copy>
</xsl:template>
<xsl:template match="item" />

Stackoverflow に関するいくつかの関連する質問を調べましたが、マージ プロセスを現在のシナリオに適応させることができませんでした。どんな助けでも大歓迎です。

4

1 に答える 1

0

AFAICT、これはあなたの条件を満たしています(私が理解できる限り):

XSLT1.0

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

<xsl:key name="itemKey" match="item" use="concat(key/id, key/color)" />

<xsl:template match="/items">
    <xsl:copy>
        <xsl:apply-templates select="item[generate-id()=generate-id(key('itemKey', concat(key/id, key/color))[1])]"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="item">
    <xsl:variable name="curr-group" select="key('itemKey', concat(key/id, key/color))" />
    <xsl:choose>
        <!-- e. Any other single 'item' element that is which is not part of a group should be pushed to output as it is. -->
        <xsl:when test="count($curr-group)=1">
            <xsl:copy-of select="."/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:copy>
                <!-- a. Extract 'geoDetails' from the 'item' where misc/details is 'geo'. --> 
                <xsl:copy-of select="$curr-group[misc/details='geo']/geoDetails"/>
                <!-- b. Extract 'otherDetails' from the 'item' where misc/details is 'other'. --> 
                <xsl:copy-of select="$curr-group[misc/details='other']/otherDetails"/>
                <!-- c. Extract the 'key' element from the first 'item' in a given group. -->
                <xsl:copy-of select="key"/>
                <!-- d. ??? --> 
                <misc>
                    <available>false</available>
                    <xsl:copy-of select="$curr-group/misc/details"/>
                </misc>
            </xsl:copy>
        </xsl:otherwise>
    </xsl:choose>   
</xsl:template>

</xsl:stylesheet>
于 2015-11-27T09:51:35.210 に答える