请帮助 XSLT,我有这个 XML 示例
<?xml version="1.0" encoding="utf-8"?>
<Document>
<TopLevel>
<Header>
<!-- Header Information-->
</Header>
<!--Payments is a one to many-->
<Payments>
<PaymentID>PID1</PaymentID>
<!--More Info-->
<!--Transactrion can be one to many for each payment-->
<Transaction>
<TranssID>TR1 - PID1</TranssID>
<TranssID>TR2 - PID1</TranssID>
<!--More Info-->
</Transaction>
</Payments>
<Payments>
<PaymentID>PID2</PaymentID>
<!--More Info-->
<!--Transactrion can be one to many for each payment-->
<Transaction>
<TranssID>TR1 - PID2</TranssID>
<TranssID>TR2 - PID2</TranssID>
<!--More Info-->
</Transaction>
</Payments>
</TopLevel>
</Document>
我需要 XSLT 中的循环机制,它将为我提供每次付款的交易。
这是我尝试使用模板方法 OUTPUT 时的输出:
<?xml version="1.0" encoding="utf-8"?>
<TopLevel>
<Payments>
<PaymentID>PID1</PaymentID>
<transIDs>TR1 - PID1</transIDs>
<transIDs>TR2 - PID1</transIDs>
<transIDs>TR1 - PID2</transIDs>
<transIDs>TR2 - PID2</transIDs>
<transIDs>TR1 - PID1</transIDs>
<transIDs>TR2 - PID1</transIDs>
<transIDs>TR1 - PID2</transIDs>
<transIDs>TR2 - PID2</transIDs>
</Payments>
<Payments>
<PaymentID>PID1</PaymentID>
<transIDs>TR1 - PID1</transIDs>
<transIDs>TR2 - PID1</transIDs>
<transIDs>TR1 - PID2</transIDs>
<transIDs>TR2 - PID2</transIDs>
<transIDs>TR1 - PID1</transIDs>
<transIDs>TR2 - PID1</transIDs>
<transIDs>TR1 - PID2</transIDs>
<transIDs>TR2 - PID2</transIDs>
</Payments>
</TopLevel>
我希望它按交易和付款分开
这是我的 XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<TopLevel>
<xsl:apply-templates select="//TopLevel/Payments"/>
</TopLevel>
</xsl:template>
<xsl:template match="//TopLevel/Payments">
<Payments>
<PaymentID>
<xsl:value-of select="//TopLevel/Payments/PaymentID"/>
</PaymentID>
<xsl:apply-templates select="//TopLevel/Payments/Transaction"/>
</Payments>
</xsl:template>
<xsl:template match="//TopLevel/Payments/Transaction">
<xsl:apply-templates select="//TopLevel/Payments/Transaction/TranssID"/>
</xsl:template>
<xsl:template match="//TopLevel/Payments/Transaction/TranssID">
<transIDs>
<xsl:value-of select="."/>
</transIDs>
</xsl:template>
</xsl:stylesheet>