0
votes

Below is my 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>

I using below xslt to get my output

<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>

I need out put like below

<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>

But here room name is not working fine.. its takes only the first room node from the xml. How i place current room name based on id

1

1 Answers

0
votes

I think instead of ../../../ms:room/@name you simply want ancestor::ms:room/@name. Note that your posted XML sample does not use namespaces while your stylesheet does, you will have to correct one or the other to have them work together.