Search code examples
xmlxpathjasper-reports

Jasperreports field with dynamic value from xml node


I have some jrxml template with xml datasource and I have myField visible below, however I need to refactor it to make it conditional because new xml formats are introduced and car owner is not always in the same place (/owner/name vs ownerName):

<field name="myField" class="java.lang.String">
        <property name="net.sf.jasperreports.xpath.field.expression"  value="string(./car/owner/name)"/>
    </field>

and this field should be created somehow to take /owner/name OR ownerName, depending on what is available. How to achieve that ?


Solution

  • If your question is to have a XPath that can handle both ./car/owner/name and ./car/ownerName, you could use:

    string(./car/owner/name|./car/ownerName)
    

    Depending on if you can use XPath 2.0 you could also use:

    string(./car/(owner/name|ownerName))