[英]XSL/XPath - search differently named elements (wildcard?)
我有一個XML格式的地址列表,其中包含多個“ FoundationDetails”。使用XSL,我希望將其轉換為geojson文件。
<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>
我想返回倫敦所有類型為“零售”的業務。 如果倫敦位於“ AddressLine2”中,則可以使用以下行:
<xsl:apply-templates select="//EstablishmentDetail[BusinessType = 'Retail' and AddressLine2 = 'London']">
但是,根據地址的格式,倫敦可以出現在AddressLine2、3或4中(但不能出現在多行中)。 如何搜索這三個節點? 可與通配符一起使用嗎? 通過Stackoverflow搜索,我發現:
*[starts-with(name(), 'London')]
但是我不確定如何將其與上面的工作線結合使用。
如果有幫助,我正在使用Saxon,XPath 2。
謝謝
您可以使用//EstablishmentDetail[BusinessType = 'Retail' and 'London' = (AddressLine2, AddressLine3, AddressLine4)]
選擇Retail
BusinessType
EstablishmentDetail
元素,其中至少列出的AddressLineX
元素之一是London
。
作為替代方案,您可以使用//EstablishmentDetail[BusinessType = 'Retail' and *[matches(local-name(), 'AddressLine[234]')] = 'London']
。
以下XPath 1.0表達式:
//EstablishmentDetail[node()[starts-with(name(), 'AddressLine') and starts-with(., 'London')] and BusinessType = 'Retail']
將選擇所有地址行以“倫敦”開頭且業務類型為“零售”的所有節點EstablishmentDetail
。
如果只想檢查AddressLine2
到AddressLine4
,則需要將檢查范圍限制為這些字段,例如使用
//EstablishmentDetail[node()[(name() = 'AddressLine2' or name() = 'AddressLine3' or name() = 'AddressLine4') and starts-with(., 'London')] and BusinessType = 'Retail']
我不知道XPath 2.0是否會讓生活更輕松。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.