Reputation: 4600
I have the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<Lenders>
<ns:FinancialOrganization xmlns:ns="http://www.starstandard.org/STAR/5">
<ns:BusinessTypeCode>F</ns:BusinessTypeCode>
<ns:CompanyName>FIRST NIAGARA BANK</ns:CompanyName>
<ns:CompanyCode>1ST NIAGARA BANK</ns:CompanyCode>
<ns:PostalAddress>
<ns:LineOne>PO BOX 76</ns:LineOne>
<ns:LineTwo>HUDSON, NY 12534</ns:LineTwo>
<ns:CountryID>US</ns:CountryID>
<ns:Privacy>
<ns:PrivacyIndicator>false</ns:PrivacyIndicator>
</ns:Privacy>
</ns:PostalAddress>
<ns:LegalClassificationCode>NIAG</ns:LegalClassificationCode>
</ns:FinancialOrganization>
<ns:FinancialOrganization xmlns:ns="http://www.starstandard.org/STAR/5">
<ns:BusinessTypeCode>F</ns:BusinessTypeCode>
<ns:CompanyName>BALLSTON SPA NATIONAL BANK</ns:CompanyName>
<ns:CompanyCode>BALLSTON SPA FIN</ns:CompanyCode>
<ns:PostalAddress>
<ns:LineOne>87 FRONT STREET</ns:LineOne>
<ns:LineTwo>BALLSTON SPA, NY 12020</ns:LineTwo>
<ns:CityName>BALLSTON SPA</ns:CityName>
<ns:CountryID>US</ns:CountryID>
<ns:Postcode>12020</ns:Postcode>
<ns:StateOrProvinceCountrySub-DivisionID>NY</ns:StateOrProvinceCountrySub-DivisionID>
<ns:Privacy>
<ns:PrivacyIndicator>false</ns:PrivacyIndicator>
</ns:Privacy>
</ns:PostalAddress>
<ns:LegalClassificationCode>BSN</ns:LegalClassificationCode>
</ns:FinancialOrganization>
<ns:FinancialOrganization xmlns:ns="http://www.starstandard.org/STAR/5">
<ns:BusinessTypeCode>F</ns:BusinessTypeCode>
<ns:CompanyName>CAPITAL ONE AUTO FINANCE</ns:CompanyName>
<ns:CompanyCode>CAP</ns:CompanyCode>
<ns:PostalAddress>
<ns:LineOne>P.O.BOX 255605</ns:LineOne>
<ns:LineTwo>SACRAMENTO CA 95865-5587</ns:LineTwo>
<ns:CityName>SACRAMENTO</ns:CityName>
<ns:CountryID>US</ns:CountryID>
<ns:Postcode>958655587</ns:Postcode>
<ns:StateOrProvinceCountrySub-DivisionID>CA</ns:StateOrProvinceCountrySub-DivisionID>
<ns:Privacy>
<ns:PrivacyIndicator>false</ns:PrivacyIndicator>
</ns:Privacy>
</ns:PostalAddress>
<ns:LegalClassificationCode>CAP</ns:LegalClassificationCode>
</ns:FinancialOrganization>
</Lenders>
I'm trying to use XPath to get a count of the number of items returned and also then get the individual element. Here's my examples that are not returning the correct result:
/Lenders/ns:FinancialOrganization[1]/ns:BusinessTypeCode
count(/Lenders/ns:FinancialOrganization/ns:BusinessTypeCode)
I'm pretty new to XPath but this particular challenge has me stumped. I'm using the W3c Xpath evaluation online tool:
http://www.utilities-online.info/xpath/?save=7f748259-a214-4f1e-8872-6f6306199331-xpath
Upvotes: 0
Views: 370
Reputation: 89285
Most likely, the online tool you're using doesn't support namespace prefix in the XPath. I tried to remove prefixes from the XML, for example this element :
<ns:FinancialOrganization xmlns:ns="http://www.starstandard.org/STAR/5">
became this unprefixed element :
<FinancialOrganization xmlns:ns="http://www.starstandard.org/STAR/5">
Then this XPath returned 3
as expected :
count(/Lenders/FinancialOrganization/BusinessTypeCode)
In my expereience using several .NET libraries that support XPath, we need to manually declare prefix-to-namespace-URI mapping before we can use that prefix in our XPath query. It seems that the same applies in Java.
Upvotes: 1