下面是 xml
<?xml version="1.0" encoding="UTF-8"?>
<hotels>
<hotel>
<rooms>
<room>
<rates>
<rate id="1" adults="1" child="0"></rate>
<rate id="2" adults="2" child="0"></rate>
<rate id="3" adults="1" child="0"></rate>
</rates>
</room>
<room>
<rates>
<rate id="4" adults="1" child="0"></rate>
<rate id="5" adults="2" child="0"></rate>
<rate id="6" adults="2" child="0"></rate>
</rates>
</room>
</rooms>
</hotel>
</hotels>
我在下面尝试xslt。XSLT 工作正常。我根据需要得到结果
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:key name="by-occupancy" match="rooms/room/rates/rate" use="concat(generate-id(parent::rooms),'|',@adults)"/>
<xsl:template match="hotels/hotel">
<hotel>
<xsl:apply-templates select="rooms/room/rates/rate[generate-id() = generate-id(key('by-occupancy', concat(generate-id(parent::rooms),'|',@adults))[1])]" mode="fun_options"/>
</hotel>
</xsl:template>
<xsl:template match="rate" mode="fun_options">
<rates>
<xsl:for-each select="key('by-occupancy', concat(generate-id(parent::rooms),'|',@adults))">
<rate><xsl:value-of select="@id"/>-<xsl:value-of select="@adults"/></rate>
</xsl:for-each>
</rates>
</xsl:template>
</xsl:stylesheet>
但是当我在 xml 中播放 xmlns:xsi 时。
结果变为空白
<?xml version="1.0" encoding="UTF-8"?>
<hotels xmlns="http://www.test.com/schemas/messages" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://www.test.com/schemas/messages">
<hotel >
<rooms>
<room>
<rates>
<rate id="1" adults="1" child="0"></rate>
<rate id="2" adults="2" child="0"></rate>
<rate id="3" adults="1" child="0"></rate>
</rates>
</room>
<room>
<rates>
<rate id="4" adults="1" child="0"></rate>
<rate id="5" adults="2" child="0"></rate>
<rate id="6" adults="2" child="0"></rate>
</rates>
</room>
</rooms>
</hotel>
</hotels>
在这里,我 applay 排除-结果-前缀="ms xsi"
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ms="http://www.test.com/schemas/messages" exclude-result-prefixes="ms xsi">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:key name="by-occupancy" match="ms:rooms/ms:room/ms:rates/ms:rate" use="concat(generate-id(parent::ms:rooms),'|',@adults)"/>
<xsl:template match="ms:hotels/ms:hotel">
<hotel>
<xsl:apply-templates select="ms:rooms/ms:room/ms:rates/ms:rate[generate-id() = generate-id(key('by-occupancy', concat(generate-id(parent::ms:rooms),'|',@adults))[1])]" mode="fun_options"/>
</hotel>
</xsl:template>
<xsl:template match="ms:rate" mode="fun_options">
<rates>
<xsl:for-each select="key('by-occupancy', concat(generate-id(parent::ms:rooms),'|',@adults))">
<rate><xsl:value-of select="@id"/>-<xsl:value-of select="@adults"/></rate>
</xsl:for-each>
</rates>
</xsl:template>
</xsl:stylesheet>
我需要如下结果
<hotel>
<rates>
<rate>1-1</rate>
<rate>3-1</rate>
<rate>4-1</rate>
</rates>
<rates>
<rate>2-2</rate>
<rate>5-2</rate>
<rate>6-2</rate>
</rates>
</hotel>
我根本看不出这些转义的字符串是如何编译的。并且您尝试在密钥中使用的父项不存在,因此我认为您想要一个祖先:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ms="http://www.test.com/schemas/messages" exclude-result-prefixes="ms xsi">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:key name="by-occupancy" match="ms:rooms/ms:room/ms:rates/ms:rate" use="concat(generate-id(ancestor::ms:rooms),'|',@adults)"/>
<xsl:template match="ms:hotels/ms:hotel">
<hotel>
<xsl:apply-templates select="ms:rooms/ms:room/ms:rates/ms:rate[generate-id() = generate-id(key('by-occupancy', concat(generate-id(ancestor::ms:rooms),'|',@adults))[1])]" mode="fun_options"/>
</hotel>
</xsl:template>
<xsl:template match="ms:rate" mode="fun_options">
<rates>
<xsl:for-each select="key('by-occupancy', concat(generate-id(ancestor::ms:rooms),'|',@adults))">
<rate><xsl:value-of select="@id"/>-<xsl:value-of select="@adults"/></rate>
</xsl:for-each>
</rates>
</xsl:template>
</xsl:stylesheet>