[英]XSLT 1.0 conditional merge after grouping
我的輸入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)對於每個已標識的組,合並應使用以下給出的邏輯進行:
一種。 從其他/細節為“ geo”的“項目”中提取“ geoDetails”。
b。 從其他/細節為“其他”的“項目”中提取“其他細節”。 每個“項目”中只有一個雜項/詳細信息,其值為“ geo”或“ other”。
C。 從給定組的第一個“項目”中提取“關鍵”元素。
d。 “ misc”元素應按原樣包含“ available”元素(始終為“ false”,因此填充一次就足夠了),並且“ details”元素應具有基於填充的元素“ geoDetails”或“ otherDetails”的值通過上面步驟a和b中給出的邏輯。 如果同時填充了“ geoDetails”和“ otherDetails”,則應有2個“ details”元素。
e。 不屬於組的任何其他單個“ item”元素應按原樣輸出。
基於上述邏輯的預期輸出如下所示:
<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分組的轉換。 我能夠對'item'元素進行分組,但是無法根據上述條件進一步討論如何合並這些分組的元素。
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上經歷了幾個相關的問題,但是無法使合並過程適應當前的情況。 任何幫助是極大的贊賞。
AFAICT,這可以滿足您的條件(據我所知):
XSLT 1.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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.