下面是我的xml
<?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>
<hotel>
<rooms>
<room name="Standard">
<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 name="Deluxe">
<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 来获取我的输出
<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="../../../ms:room/@name"/>-<xsl:value-of select="@id"/>-<xsl:value-of select="@adults"/></rate>
</xsl:for-each>
</rates>
</xsl:template>
</xsl:stylesheet>
我需要像下面这样放
<hotel>
<rates>
<rate>Standard-1-1</rate>
<rate>Standard-3-1</rate>
<rate>Deluxe-4-1</rate>
</rates>
<rates>
<rate>Standard-2-2</rate>
<rate>Deluxe-5-2</rate>
<rate>Deluxe-6-2</rate>
</rates>
</hotel>
但是这里的房间名称不能正常工作..它只从xml中获取第一个房间节点。如何根据 id 放置当前房间名称
我认为您只需要ancestor::ms:room/@name
而不是../../../ms:room/@name
.请注意,您发布的 XML 示例不使用命名空间,而样式表使用命名空间,您必须更正其中一个以使它们协同工作。