【发布时间】:2025-12-06 12:40:01
【问题描述】:
我有一个 XML 和一个 XSLT,可以提供当前输出。它使用“密钥”,但我没有得到预期的输出
源 XML:
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<Receivers>
<ReceiverRule>
<Condition>
<Value>Condition 1</Value>
</Condition>
<Receiver>
<party>party1</party>
<system>SYS1</system>
</Receiver>
</ReceiverRule>
<ReceiverRule>
<Condition>
<Value>Condition 2</Value>
</Condition>
<Receiver>
<party>party2</party>
<system>SYS2</system>
</Receiver>
</ReceiverRule>
</Receivers>
<ReceiverInterfaces>
<Receiver>
<party>party1</party>
<system>SYS1</system>
</Receiver>
<ReceiverInterfaceRule>
<Rule>Rule 1 sytem 1</Rule>
</ReceiverInterfaceRule>
<ReceiverInterfaceRule>
<Rule>Rule 2 system 1</Rule>
</ReceiverInterfaceRule>
</ReceiverInterfaces>
<ReceiverInterfaces>
<Receiver>
<party>party2</party>
<system>SYS2</system>
</Receiver>
<ReceiverInterfaceRule>
<Rule>Rule 1 system 2</Rule>
</ReceiverInterfaceRule>
<ReceiverInterfaceRule>
<Rule>Rule 2 system 2</Rule>
</ReceiverInterfaceRule>
</ReceiverInterfaces>
</Root>
我的 XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes"/>
<xsl:key name="Receiver" match="ReceiverRule/Receiver" use="concat(party,system)"/>
<xsl:template match="Root">
<ReceiverList>
<xsl:apply-templates select="Receivers/ReceiverRule"/>
</ReceiverList>
</xsl:template>
<xsl:template match="ReceiverRule">
<Receiver>
<Name>
<xsl:value-of select="concat(Receiver/party, ' ' ,Receiver/system)"/>
</Name>
<Condition>
<xsl:value-of select="Condition/Value"/>
</Condition>
<xsl:apply-templates select="ancestor::Root/ReceiverInterfaces
[Receiver/child::* = key('Receiver',Root/Receivers/ReceiverRule/Receiver)]"/>
</Receiver>
</xsl:template>
<xsl:template match="ReceiverInterfaces">
<xsl:apply-templates select="ReceiverInterfaceRule"/>
</xsl:template>
<xsl:template match="ReceiverInterfaceRule">
<Rule>
<xsl:value-of select="Rule"/>
</Rule>
</xsl:template>
</xsl:stylesheet>
产生这个 XML 输出:
<?xml version="1.0" encoding="UTF-8"?>
<ReceiverList>
<Receiver>
<Name>party1 SYS1</Name>
<Condition>Condition 1</Condition>
</Receiver>
<Receiver>
<Name>party2 SYS2</Name>
<Condition>Condition 2</Condition>
</Receiver>
</ReceiverList>
但我期待这个输出:
<?xml version="1.0" encoding="UTF-8"?>
<ReceiverList>
<Receiver>
<Name>party1 SYS1</Name>
<Condition>Condition 1</Condition>
<Rule>Rule 1 sytem 1</Rule>
<Rule>Rule 2 system 1</Rule>
</Receiver>
<Receiver>
<Name>party2 SYS2</Name>
<Condition>Condition 2</Condition>
<Rule>Rule 1 system 2</Rule>
<Rule>Rule 2 system 2</Rule>
</Receiver>
</ReceiverList>
不知何故我的钥匙无法识别。
【问题讨论】: