Araneo
Araneo

Reputation: 497

XSLT 2.0 skip all elements except one

I have a request with some elements with structure like below:

<Request>
      <Other>
      ...
      </Other>
      <Qualifiers>
         <Options>
            <Segment Number="1"/>
         </Options> 
         <Offer>
            <Column ColumnNumber="2.1"/>
            <Record Number="2"/>
         </Offer>
         <Offer>
            <Column ColumnNumber="1.1"/>
            <Column EndColumnNumber="4.1" ColumnNumber="3.1"/>
            <Record Number="1"/>
         </Offer>
         <Offer>
            <Column ColumnNumber="5.1"/>
            <Record Number="3"/>
         </Offer>
         <Fare>
            <Basis>ABCDE</Basis>
         </Fare>
      </Qualifiers>
      <Other>
      ...
      </Other>
</Request>

From this payload I need to do 3 separate requests with single Offer element in each call. The rest of request should be copied without changes, so for first Offer my request should looks like that:

<Request>
      <Other>
      ...
      </Other>
      <Qualifiers>
         <Options>
            <Segment Number="1"/>
         </Options>
         <Offer>
            <Column ColumnNumber="2.1"/>
            <Record Number="2"/>
         </Offer>
         <Fare>
            <Basis>ABCDE</Basis>
         </Fare>
      </Qualifiers>
      <Other>
      ...
      </Other>
</Request>

Second and third analogical with second and third Offer elements.

I use Camel, so I tried to make it in loop providing to XSLT parameter which is index of the element which should be left. So, I tried:

<xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()" />
            </xsl:copy>
</xsl:template>

<xsl:template match="/Request/Qualifiers/Offer[not($MY_IDX)]"/>

Where $MY_IDX is provided index. After some searchings I realised that it won't worked, because not() function takes and returns only boolean value. Is there any other way to archive my goal?

Upvotes: 1

Views: 258

Answers (1)

kjhughes
kjhughes

Reputation: 111621

Instead of

<xsl:template match="/Request/Qualifiers/Offer[not($MY_IDX)]"/>

use

<xsl:template match="/Request/Qualifiers/Offer[position() != $MY_IDX]" />

which suppresses all but the Offer whose position is given by $MY_IDX.

Upvotes: 1

Related Questions