Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using If Else in XSLT

Tags:

xml

xslt

I need to select a node if a condition is matched. I'm totally new in XSLT and tried to search for an answer but can't find a solution that works for me. I want to select an adres when there is an AddressType = 'Personal', if there's not an AddressType = Personal, check for 'Second', if not check for 'Office'. A normal If Else. I tried with and and this could work if only one type of AddressType would exist in the XML file. In my case there can be up too 11 addressTypes (in random order) in the XML file and I can only pass one through.

This looks like my source XML:

<Person>
<address>
<street></street
<number></number>
<City></City>
<AddressType>Personal</AddressType>
</address>
<address>
<street></street
<number></number>
<City></City>
<AddressType>Second</AddressType>
</address>
<address>
<street></street
<number></number>
<City></City>
<AddressType>Office</AddressType>
</address>
</Person>

Any advice? thank you

like image 597
user1292411 Avatar asked Dec 13 '22 03:12

user1292411


2 Answers

You need to use <xsl:choose>

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <xsl:choose>
            <xsl:when test="/Person/address/AddressType='Personal'">
                <!-- DO Stuff -->
            </xsl:when>
            <xsl:when test="/Person/address/AddressType='Second'">
                <!-- DO Stuff -->
            </xsl:when>
            <xsl:when test="/Person/address/AddressType='Office'">
                <!-- DO Stuff -->
            </xsl:when>
            <xsl:otherwise>
                <!-- Do your else stuff -->
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>
like image 85
Dan675 Avatar answered Dec 21 '22 04:12

Dan675


Another option, rather than using nesting everything in an xsl:choose, is to use template matching to get the case you want. Try this XSLT as an example

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>

   <xsl:template match="Person[address/AddressType='Personal']" priority="3">
      Match Personal
   </xsl:template>

   <xsl:template match="Person[address/AddressType='Second']" priority="2">
      Match Second
   </xsl:template>

   <xsl:template match="Person[address/AddressType='Office']" priority="1">
      Match Office
   </xsl:template>

   <xsl:template match="Person">
      Match None
   </xsl:template>
</xsl:stylesheet>

Do note that templates with more specific patterns take precedence over ones with no pattern (which is why the last "Person" template won't be called unless any of the other templates don't match. For the other templates, the higher the priority attribute will take precedence if all three templates match. Thus "Personal" will always be chosen even when the other attributes are present.

(If none of the templates had a priority attribute, the last matching one would be chosen in this case).

like image 40
Tim C Avatar answered Dec 21 '22 05:12

Tim C