0

i am beginner in XSLT. I am using XSLT to transform XML to XML through JDK 6.

Below is the Source XML which i received from the web service.

Source XML:

<Response>
    <data>
        <route type="OUTBOUND">
            <Include>AA</Include>
            <Include>BB</Include>
            <Include>CC</Include>
        </route>
        <route type="INBOUND">
            <Include>XX</Include>
            <Include>YY</Include>
            <Include>ZZ</Include>
        </route>
    </data>
    <result>
        <Flights type="OUTBOUND">
            <Flight>
                <Airline>AA</Airline>
            </Flight>
            <Flight>
                <Airline>BB</Airline>
            </Flight>
            <Flight>
                <Airline>CC</Airline>
            </Flight>
            <Flight>
                <Airline>XX</Airline>
            </Flight>
            <Flight>
                <Airline>YY</Airline>
            </Flight>           
            <Flight>
                <Airline>ZZ</Airline>
            </Flight>
        </Flights>
        <Flights type="INBOUND">
            <Flight>
                <Airline>AA</Airline>
            </Flight>
            <Flight>
                <Airline>BB</Airline>
            </Flight>
            <Flight>
                <Airline>CC</Airline>
            </Flight>
            <Flight>
                <Airline>XX</Airline>
            </Flight>
            <Flight>
                <Airline>YY</Airline>
            </Flight>           
            <Flight>
                <Airline>ZZ</Airline>
            </Flight>
        </Flights>
    </result>
</Response>

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="1.0" xmlns:exslt="http://exslt.org/common" exclude-result-prefixes="exslt">

    <xsl:output method="xml" indent="yes" />

    <xsl:template match="Response">
        <xsl:element name="Result">
            <xsl:apply-templates select="result/Flights"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="Flights">
        <xsl:variable name="PrefAirlines">
            <xsl:choose>
                <xsl:when test="current()/@type = 'OUTBOUND'">
                    <xsl:value-of select="//route[@type='OUTBOUND']/Include"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="//route[@type='INBOUND']/Include"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:apply-templates select="Flight[Airline=$PrefAirlines]"/>
    </xsl:template>

    <xsl:template match="Flight">
        <xsl:element name="FilterFlight">
            <xsl:value-of select="Airline"/>
        </xsl:element>
    </xsl:tempalte>

</xsl:stylesheet>

When i apply above XSLT on the source XML i get following output XML.

OutputXML:

<Result>
  <FilterFlight>AA</FilterFlight>
  <FilterFlight>XX</FilterFlight>
</Result>

Expected output:

<Result>
  <FilterFlight>AA</FilterFlight>
  <FilterFlight>BB</FilterFlight>
  <FilterFlight>CC</FilterFlight>
  <FilterFlight>XX</FilterFlight>
  <FilterFlight>YY</FilterFlight>
  <FilterFlight>ZZ</FilterFlight>
</Result>

The problem is within the logic of creating variable PrefAirlines.

I have tried below approach as well but didn't get the expected output.

<xsl:variable name="PrefAirlines">
    <xsl:choose>
        <xsl:when test="current()/@type = 'OUTBOUND'">
            <xsl:value-of select="exslt:node-set(//route[@type='OUTBOUND']/Include)"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="exslt:node-set(//route[@type='INBOUND']/Include)"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>

Is there any function available using which i can get the node set instead of single value? Please help.

Ankur Raiyani
  • 1,509
  • 5
  • 21
  • 49

1 Answers1

2

If you're using Java, then XSLT 2.0 is available to you (in the form of Saxon), so there's really very little point in struggling with the restrictions and limitations of XSLT 1.0, and the extensions like exslt:node-set() that were designed to get around these limitations.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Thanks for the suggestion. However, here we have to use XSLT 1.0 because it's not our call to change the technology. So, please suggest some solution for XSLT 1.0 – Ankur Raiyani Jan 01 '13 at 06:57
  • 1
    Sorry, if you want to reach the North Pole by hopping on one leg, you might find that an interesting challenge, but I don't. – Michael Kay Jan 03 '13 at 19:45