我有下面的XML访问分组节点使用XPath建立一个恒定的输出
<toPay>
<Pay>
<amount>1111</amount>
<accountNumber>223128987</accountNumber>
<payType>PAYROLL</payType>
</Pay>
<Pay>
<amount>2222</amount>
<accountNumber>123128987</accountNumber>
<payType>PAYROLL</payType>
</Pay>
<Pay>
<amount>333</amount>
<accountNumber>645032</accountNumber>
<payType>MAIN</payType>
</Pay>
我需要按此格式输出:
<root>
<element>
<amount>1111</amount>
<accountNumber>223128987</accountNumber>
</element>
<element>
<amount>2222</amount>
<accountNumber>223128987</accountNumber>
</element>
<element>
<amount></amount>
<accountNumber></accountNumber>
</element>
<element>
<amount></amount>
<accountNumber></accountNumber>
</element>
我写了如下代码:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="groups" match="payType" use="." />
<xsl:template match="/toPay/Pay">
<root>
<xsl:apply-templates select="payType[generate-id() = generate-id(key('groups', .)[1])]" />
</root>
</xsl:template>
<xsl:template match="payType">
<xsl:variable name="currentGroup" select="." />
<xsl:if test="$currentGroup = 'PAYROLL'">
<xsl:for-each select="key('groups', $currentGroup)">
<element>
<xsl:if test="position() = 1">
<amount>
<xsl:value-of select="../amount" />
</amount>
<accountNumber>
<xsl:value-of select="../accountNumber" />
</accountNumber>
</xsl:if>
<xsl:if test="position() = 2">
<amount>
<xsl:value-of select="../amount" />
</amount>
<accountNumber>
<xsl:value-of select="../accountNumber" />
</accountNumber>
</xsl:if>
</element>
</xsl:for-each>
</xsl:if>
</xsl:template>
但我面临的问题是 - 我不能访问分组节点为paytype -PAYROLL。我知道的唯一方法是使用位置,但是我的要求始终在输出中始终输入4个元素节点,而无论源xml中带有PAYtype -PAYROLL的节点是否存在。 如果存在,那么将填充相同eeds的值,如果不存在,那么xml标签将不带值。
任何帮助非常感谢 - 无论是xslt 1.0或2.0。
请用单词解释所需的逻辑。我根本不理解你的输出。 –
您的模板会创建一个“组”结果元素,而您所需的输出不会显示任何此类元素。所以你需要更详细地解释你想要实现的内容。在完整结果中,每个“组”或“元素”结果元素中是否需要四个'element'元素? –
在输出中应该总是有4个元素节点。每个元素节点都应该使用来自源xml节点的值来填充payType,其中payType是PAYROLL。如果Pay支付节点的PayType为PayPoll,那么输出应该有前2个元素节点,其值来自xml源,其余2个元素节点的x值和账户数量为空值。 – Vicky