以下是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>
低于xslt我试试。这里我applay exclude-result-prefixes =&#34; ms xsi&#34;
<?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>
答案 0 :(得分:0)
我看不出那些转义字符串中的任何字符串是否会编译。你试图在密钥中使用的父项不存在,所以我认为你想要一个祖先:
<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>