【发布时间】:2019-04-12 16:32:59
【问题描述】:
我正在尝试根据出现的次数将节点移动到其前一个节点下的同一级别。用 XSLT 代码试试这个。
Pay,Remit,Trailer 可以在 xml 中重复。但是每次付款之后都会有n个汇款节点。 在哪里输出。 pay 应该包含它下面的所有汇款节点。
我尝试过使用 XSLT 代码,但不知何故我没有得到预期的结果。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding='UTF-8'/>
<!--Identity template,
provides default behavior that copies all content into the output -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:for-each select="pay">
<xsl:template match="Remit">
<xsl:value-of select="."/>
</xsl:template>
</xsl:for-each>
</xsl:stylesheet>
输入 -
<?xml version="1.0" encoding="utf-8"?>
<Message>
<Record>
<Header>
<H></H>
</Header>
<Trailer>
<AA>1</AA>
</Trailer>
<Pay>
<BB>1</BB>
<amount>11</amount>
</Pay>
<Remit>
<Type>30</Type>
<Transaction>I</Transaction>
</Remit>
<Trailer>
<AA>1</AA>
</Trailer>
<Pay>
<BB>1</BB>
<amount>78</amount>
</Pay>
<Remit>
<Type>30</Type>
<Transaction>I</Transaction>
</Remit>
<Remit>
<Type>30</Type>
<Transaction>I</Transaction>
</Remit>
<Trailer>
<AA>1</AA>
</Trailer>
</Record>
</Message>
预期输出:
<?xml version="1.0" encoding="utf-8"?>
<Message>
<Record>
<Header>
<H>1</H>
</Header>`enter code here`
<Trailer>
<AA>1</AA>
</Trailer>
<Pay>
<BB>1</BB>
<amount>11</amount>
<Remit>
<Type>30</Type>
<Transaction>I</Transaction>
</Remit>
</Pay>
<Trailer>
<AA>1</AA>
</Trailer>
<Pay>
<BB>1</BB>
<amount>78</amount>
<Remit>
<Type>30</Type>
<Transaction>I</Transaction>
</Remit>
<Remit>
<Type>30</Type>
<Transaction>I</Transaction>
</Remit>
</Pay>
<Trailer>
<AA>1</AA>
</Trailer>
</Record>
</Message>
【问题讨论】:
标签: xml xslt xslt-1.0 xslt-2.0