我有以下输入 XML:
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person id="1" type="parent">
<name>father</name>
<person-reference type="child">3</person-reference>
<person-reference type="child">4</person-reference>
</person>
<person id="2" type="parent">
<name>mother</name>
<person-reference type="child">3</person-reference>
<person-reference type="child">4</person-reference>
</person>
<person id="3">
<name>brother</name>
</person>
<person id="4">
<name>sister</name>
</person>
</persons>
使用以下 XSLT 转换:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/persons">
<relations>
<xsl:apply-templates select="person[@type = 'parent']"/>
</relations>
</xsl:template>
<xsl:template match="person">
<parent>
<name><xsl:value-of select="name"/></name>
</parent>
<xsl:apply-templates select="person-reference[@type = 'child']"/>
</xsl:template>
<xsl:template match="person-reference">
<child>
<name><xsl:value-of select="//person[@id = current()]/name"/></name>
</child>
</xsl:template>
</xsl:stylesheet>
我得到这个 XML(结果):
<?xml version="1.0" encoding="UTF-8"?>
<relations xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<parent>
<name>father</name>
</parent>
<child>
<name>brother</name>
</child>
<child>
<name>sister</name>
</child>
<parent>
<name>mother</name>
</parent>
<child>
<name>brother</name>
</child>
<child>
<name>sister</name>
</child>
</relations>
但我想要的是这个 XML(预期结果):
<?xml version="1.0" encoding="UTF-8"?>
<relations xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<parent>
<name>father</name>
</parent>
<child>
<name>brother</name>
</child>
<child>
<name>sister</name>
</child>
<parent>
<name>mother</name>
</parent>
</relations>
或者这个(可选的预期结果):
<?xml version="1.0" encoding="UTF-8"?>
<relations xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<parent>
<name>father</name>
</parent>
<parent>
<name>mother</name>
</parent>
<child>
<name>brother</name>
</child>
<child>
<name>sister</name>
</child>
</relations>
XSLT 有没有办法像我一样使用引用来防止双重输出?