Reputation: 173
I've an address list in XML format, with multiple 'EstablishmentDetails' Using XSL I wish to transform it into a geojson file.
<EstablishmentCollection>
<EstablishmentDetail>
<BusinessName>Company Name</BusinessName>
<BusinessType>Retail</BusinessType>
<AddressLine1>14 Somerset Place</AddressLine1>
<AddressLine2>London</AddressLine2>
<AddressLine3>...</AddressLine3>
<AddressLine4>...</AddressLine4>
<PostCode>SW11 1AP</PostCode>
</EstablishmentDetail>
</EstablishmentCollection>
I want to return all occurances of Business type 'Retail' in London. If London is in 'AddressLine2' It works using this line:
<xsl:apply-templates select="//EstablishmentDetail[BusinessType = 'Retail' and AddressLine2 = 'London']">
However, depending on the format of the address, London can occur in either AddressLine2, 3 or 4 (but never in multiple lines). How do I search those three nodes? Is it pssible using with wildcards? Upon a Stackoverflow search I came upon:
*[starts-with(name(), 'London')]
But I'm unsure how to combine it with the working line above.
I'm using Saxon,XPath 2 if that helps.
Thanks
Upvotes: 0
Views: 223
Reputation: 167571
You can use //EstablishmentDetail[BusinessType = 'Retail' and 'London' = (AddressLine2, AddressLine3, AddressLine4)]
to select those EstablishmentDetail
elements with the Retail
BusinessType
where at least one of the listed AddressLineX
elements is London
.
As an alternative you can use //EstablishmentDetail[BusinessType = 'Retail' and *[matches(local-name(), 'AddressLine[234]')] = 'London']
.
Upvotes: 2
Reputation: 3357
The following XPath 1.0 expression:
//EstablishmentDetail[node()[starts-with(name(), 'AddressLine') and starts-with(., 'London')] and BusinessType = 'Retail']
will select all nodes EstablishmentDetail
where any address line starts with 'London' and the business type is 'Retail'.
If you want only to check AddressLine2
to AddressLine4
only you need to limit the check to these fields, e.g. by using
//EstablishmentDetail[node()[(name() = 'AddressLine2' or name() = 'AddressLine3' or name() = 'AddressLine4') and starts-with(., 'London')] and BusinessType = 'Retail']
I don't known if XPath 2.0 will make life easier or not.
Upvotes: 0