在有關通過 XSLT 決議 XML 代碼的查詢之一中需要幫助。下面是我的 XML 代碼示例。
<?xml version="1.0" encoding="UTF-8"?>
<Entity>
<Shipment>
<shipFromLocation>06AC001</shipFromLocation>
<shipToLocation>07BCDAZ</shipToLocation>
<container>
<key1>101</key1>
<key1>102</key1>
</container>
<container>
<key1>103</key1>
<key1>104</key1>
</container>
</Shipment>
<Shipment>
<shipFromLocation>06AC001</shipFromLocation>
<shipToLocation>07BCDAZ</shipToLocation>
<container>
<key1>105</key1>
<key1>106</key1>
</container>
<container>
<key1>107</key1>
<key1>108</key1>
</container>
</Shipment>
<Shipment>
<shipFromLocation>06AC002</shipFromLocation>
<shipToLocation>08BCDAZ</shipToLocation>
<container>
<key1>200</key1>
<key1>201</key1>
</container>
</Shipment>
<Shipment>
<shipFromLocation>06AC002</shipFromLocation>
<shipToLocation>08BCDAZ</shipToLocation>
<container>
<key1>202</key1>
<key1>203</key1>
</container>
</Shipment>
</Entity>
如您所見,有 4 個標簽,在您擁有的每個標簽中<shipFromLocation>,<shipToLocation>
- 前 2 個標簽包含相同的
<Shipment>值<shipFromLocation><shipToLocation> - 最后 2 個標簽包含相同的
<Shipment>值<shipFromLocation><shipToLocation>
現在每個<Shipment>標簽內都有多個<container>標簽。我試圖得到如下結果
<?xml version="1.0" encoding="UTF-8"?>
<Entity>
<Shipment>
<shipFromLocation>06AC001</shipFromLocation>
<shipToLocation>07BCDAZ</shipToLocation>
<container>
<key1>101</key1>
<key1>102</key1>
</container>
<container>
<key1>103</key1>
<key1>104</key1>
</container>
<container>
<key1>105</key1>
<key1>106</key1>
</container>
<container>
<key1>107</key1>
<key1>108</key1>
</container>
</Shipment>
<Shipment>
<shipFromLocation>06AC002</shipFromLocation>
<shipToLocation>08BCDAZ</shipToLocation>
<container>
<key1>200</key1>
<key1>201</key1>
</container>
<container>
<key1>202</key1>
<key1>203</key1>
</container>
</Shipment>
</Entity>
也就是說,您正在根據<shipFromLocation>并<shipToLocation>聚合其中的所有詳細資訊進行分組。我無法為其生成 XSLT 代碼。我擁有的是一個破碎的
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="/Entity">
<xsl:copy>
<xsl:for-each-group select="Shipment" group-by="shipFromLocation">
<Shipment>
<xsl:for-each select="current-group()">
<shipFromLocation>
<xsl:value-of select="shipFromLocation"/>
</shipFromLocation>
<container>
<xsl:value-of select="container"/>
</container>
</xsl:for-each>
</Shipment>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
有人可以幫我嗎....
uj5u.com熱心網友回復:
AFAICT,你想做:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Entity">
<xsl:copy>
<xsl:for-each-group select="Shipment" group-by="concat(shipFromLocation, '|', shipToLocation)">
<Shipment>
<xsl:copy-of select="shipFromLocation, shipToLocation"/>
<xsl:copy-of select="current-group()/container"/>
</Shipment>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/494813.html
標籤:xml xslt xml 解析 xslt-1.0 xslt-2.0
下一篇:即使在發送回應后仍執行中間件
