0

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

user982124
  • 4,416
  • 16
  • 65
  • 140
  • This works fine for me: `count(/Lenders/ns:FinancialOrganization/ns:BusinessTypeCode)`, as does `count(//ns:FinancialOrganization/ns:BusinessTypeCode)`. Both return `3`, which seems to be correct. – Ken White May 06 '14 at 23:55
  • I'm using this online tool and it's returning a 0 for the count: http://www.utilities-online.info/xpath/?save=7f748259-a214-4f1e-8872-6f6306199331-xpath – user982124 May 07 '14 at 00:14
  • I can't help you with that online tool, because I know nothing about it. I can only say that I tested the XPath expressions I posted, and they worked. (I used an XML editing tool that unfortunately is no longer available, WMHelp XMLPad, but it's still fully functional. It uses MSXML as the XML implementation.) – Ken White May 07 '14 at 00:19
  • The page you cite calls it an "W3C XPath evalutation online". It's not a W3C tool, I think their use of the name W3C is purely to qualify "XPath" (or to attract google hits). They would do better if they could spell "evaluation". Since they say nothing about how to bind namespace prefixes, I should assume that their knowledge of XPath is probably as weak as their English spelling. – Michael Kay May 07 '14 at 07:02

1 Answers1

1

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.

Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137