我正在尝试处理以下XML,以使用XSLT显示每个MerchandiseItem的每个Discount记录:
注意:每个MerchandiseItem后面总是0个或多个Discount元素
示例XML:
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
tasks.withType(Test) {
testLogging {
events TestLogEvent.FAILED,
TestLogEvent.SKIPPED,
TestLogEvent.STANDARD_ERROR,
TestLogEvent.STANDARD_OUT
exceptionFormat TestExceptionFormat.FULL
showCauses true
showExceptions true
showStackTraces true
showStandardStreams true
}
}
考虑到<IBMGSA xmlns="http://www.omnicogroup.com/FPF/namespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<transaction>
<MerchandiseItem Record="2">
<Identifier>
<SalePrc>6.14</SalePrc>
<Quantity>1.00</Quantity>
<NetQuantity>1.00</NetQuantity>
<SaleQuantity>1</SaleQuantity>
<SalePrice>6.14</SalePrice>
<Penny>.01</Penny>
<Zero>0.00</Zero>
<IsVoided>0</IsVoided>
<VoidedQuantity>0.00</VoidedQuantity>
06</Identifier>
<ItemCode>
<ItemCodeGTIN14>00000000000083</ItemCodeGTIN14>
<ItemCodeGTIN12>000000000008</ItemCodeGTIN12>
<Sku>000000008</Sku>
<SkuType>3</SkuType>
<SkuByte>1</SkuByte>
<SuppressRecord>NO</SuppressRecord>
<ItemDescription>ZIL PLAY A-</ItemDescription>
<ItemDescTrimmed>ZIL PLAYA-SYS</ItemDescTrimmed>
1000000000000000000083</ItemCode>
<Department>
<DeptSuppress>YES</DeptSuppress>
182</Department>
<ItemAmount>
<NegativeTransaction>0</NegativeTransaction>
<ItemAmount>7.68</ItemAmount>
<ItemDiscountCode>2</ItemDiscountCode>
<ItemDiscountAmount>1.54</ItemDiscountAmount>
<ItemDiscountPercent>20.0</ItemDiscountPercent>
<ItemAmt>7.68</ItemAmt>
<ItemDisAmt>6.14</ItemDisAmt>
</ItemAmount> <!-- Added by edit -->
</MerchandiseItem>
<Discount Record="3">
<Identifier>10</Identifier>
<DiscountCode>2</DiscountCode>
<DiscountAmount>
<DiscountAmountType>PE</DiscountAmountType>
<DiscountQuantity>1</DiscountQuantity>
<NegativeTransaction>0</NegativeTransaction>
<DiscountAmount>1.54</DiscountAmount>
<TaxExemptAmount>0.00</TaxExemptAmount>
<ABSDiscountAmt>1.54</ABSDiscountAmt>
154</DiscountAmount>
<DiscountPercent>
<DiscountPercent>20.0</DiscountPercent>
200</DiscountPercent>
</Discount>
<Discount Record="4">
<Identifier>10</Identifier>
<DiscountCode>25</DiscountCode>
</Discount>
...
...
...
</transaction>
</IBMGSA> <!-- Added by edit -->
元素始终位于DiscountItem
元素之后并且仅需要与该{关联/显示,因此,在遍历每个MerchandiseItem元素时如何遍历DiscountItem
元素{1}}?
我需要输出如下(引用此示例XML)-感谢您重新格式化! 谢谢-正确的HTML输出(第5版)应为:
MerchandiseItem
与上述输出等效的XML:
MerchandiseItem
我使用@ michael.hor257k的注释中的建议,尝试使用以下XSL显示MerchandiseItem和Discount信息(已更新为更清晰-希望如此)-我希望如何分组和显示列出的DiscountItem和MerchandiseItems以上:
<html>
<body>
<table>
<tr>
ZIL PLAY A-
00000000000083 1 @ 7.68 7.68
10 (20 %) OFF 00000000000083 1.54-
25 (5 %) OFF 00000000000083 .31-
</tr>
</table>
</body>
</html>
,但是使用相同的XML示例呈现任何内容。我的for-each循环是否未正确引用MerchandiseItem / Discount分组?
答案 0 :(得分:0)
这是示例XSLT 3(我想我没有使用XSLT 3功能,因此也应该与XSLT 2处理器一起使用)样式表,显示了如何在某些情况下应用for-each-group group-starting-with
来分组和输出数据:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
xpath-default-namespace="http://www.omnicogroup.com/FPF/namespace"
version="3.0">
<xsl:output method="html" indent="yes" html-version="5"/>
<xsl:template match="/">
<html>
<head>
<title>.NET XSLT Fiddle Example</title>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="transaction">
<table>
<thead>
<tr>
<th/>
</tr>
</thead>
<xsl:for-each-group select="*" group-starting-with="MerchandiseItem">
<tbody>
<xsl:apply-templates select="current-group()[self::Discount]">
<xsl:with-param name="merch-item" select="."/>
</xsl:apply-templates>
</tbody>
</xsl:for-each-group>
</table>
</xsl:template>
<xsl:template match="Discount">
<xsl:param name="merch-item"/>
<tr>
<td valign="top" align="left" style="font-size:14px;line-height:18px;">
<xsl:value-of select="$merch-item/ItemCode/ItemDescription"/>
<br />
<xsl:value-of select="$merch-item/ItemCode/ItemCodeGTIN14"/> 
<xsl:value-of select="floor($merch-item/Identifier/Quantity)"/> @ <xsl:value-of
select="$merch-item/ItemAmount/ItemAmount div $merch-item/Identifier/Quantity"/>
<xsl:value-of select="Identifier, DiscountPercent/DiscountPercent"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/ej9EGd3
我意识到输出格式不是您显示的格式,但是我无法真正从两个不同的片段中准确地确定您想要的格式,该示例说明了如何使用分组构造并处理每个{{1} }各组中的}元素可输出一些值,同时允许访问Discount
开始每组,因此希望它足以使您自己将其塑造为正确的格式。