JohnoAvocado
JohnoAvocado

Reputation: 13

XSLT Conditional Template Match

I am trying to do a conditional match on an XML and to return the value from a sibling node. I would like to match on the records which have the attribute of Gender PreviousValue equal to an empty string. Then I would like to return the value of Number in ID for that slave.

My example:

<SlaveTeam>

    <Slave>
        <ID>
            <Number>202</Number>
        </ID>
        <Personal>
            <Gender>Female</Gender>
        </Personal>
    </Slave>

    <Slave>
        <ID>
            <Number>303</Number>
        </ID>
        <Personal>
                <Gender PreviousValue = "">Male</Gender>
        </Personal>
    </Slave>

</SlaveTeam>

The XSLT that I am trying to use is along the lines of:

<xsl:template match="/SlaveTeam/Slave/Personal/Gender/@PreviousValue = '' ">

    <xsl:value-of select="/SlaveTeam/Slave/ID/Number"/>

</xsl:template>

Because the second slave had a previous value of nothing for Gender, the output I would like to get would be:

303

I think I have a basic misunderstanding with how the matches should be used. Really appreciate any help I can get with this.

Upvotes: 1

Views: 7309

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167641

I think you want

<xsl:template match="/SlaveTeam/Slave[Personal/Gender/@PreviousValue = '' ]">

    <xsl:value-of select="ID/Number"/>

</xsl:template>

Here is a complete example:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/SlaveTeam/Slave[not(Personal/Gender/@PreviousValue = '')]"/>

<xsl:template match="/SlaveTeam/Slave[Personal/Gender/@PreviousValue = '' ]">

    <xsl:value-of select="ID/Number"/>

</xsl:template>

</xsl:stylesheet>

When applied to the input

<SlaveTeam>

    <Slave>
        <ID>
            <Number>202</Number>
        </ID>
        <Personal>
            <Gender>Female</Gender>
        </Personal>
    </Slave>

    <Slave>
        <ID>
            <Number>303</Number>
        </ID>
        <Personal>
                <Gender PreviousValue = "">Male</Gender>
        </Personal>
    </Slave>

    <Slave>
        <ID>
            <Number>404</Number>
        </ID>
        <Personal>
            <Gender>Female</Gender>
        </Personal>
    </Slave>

    <Slave>
        <ID>
            <Number>505</Number>
        </ID>
        <Personal>
                <Gender PreviousValue = "">Male</Gender>
        </Personal>
    </Slave>

</SlaveTeam>

I get the output

303

505

So I think the match is as I understand your description.

Upvotes: 1

Related Questions