Crobzilla
Crobzilla

Reputation: 5051

How to check if an XML element has a certain attribute with XSLT

I've inherited an XSLT transformation project and this is my first time using this technology. Say I have this XML:

<report>
    <data>
        <group>
            <row>
                <cell email="true">
                    <stuff>[email protected]</stuff>
                </cell>
                <cell>
                    <stuff>Not an email</stuff>
                </cell>
            </row>
        </group>
    </data>
</report>

How can I test in XSLT to see if a cell has an email attribute, and/or if the attribute it set?

Upvotes: 1

Views: 2581

Answers (1)

collapsar
collapsar

Reputation: 17238

try

//cell[@email]

for your xpath expresssion. use it as a pattern in your template which would contain some instructions like ...

<xsl:if test=".[@email]">
    <!-- structures to generate / further processing -->
</xsl:if>

or

<xsl:if test=".[@email = 'true']">
    <!-- structures to generate / further processing -->
</xsl:if>

the test expression may also contain a pattern like the one from user1759572's comment - it all dependes on the context where you are going to perform your test and which outcome of the transformation you wish to obtain.

Upvotes: 2

Related Questions