Problem understanding what XPATH to write

Discussion in 'XML' started by Clarence, Oct 6, 2004.

  1. Clarence

    Clarence Guest

    Hi - I have a problem and here is the verbose version of what I am trying to
    do (better too much info than not enough).

    I am searching through about 4,700 XML files containing company contact
    details. In the company details are phone numbers. The phone numbers with
    the formats I need have the following structure.

    <xs:complexType name="PHONENO">
    <xs:sequence maxOccurs="unbounded">
    <xs:element name="COUNTRY" type="xs:string"/>
    <xs:element name="NUMBER" type="xs:string"/>
    <xs:element name="AREACODE" type="xs:string"/>
    </xs:sequence>
    </xs:complexType>

    I have determined this structure and I don't have a schema for the XML files
    I am searching. Many of these files have many different XML structures but
    the structure of the phone numbers is always the same.

    My question is how do I tell if a bit of XML contains this PHONENO structure
    (above)? What is a typical XPATH expression I could use to search my XML to
    return a result saying Yes, this is a valid bit of XML containing the phone
    number or no it isn't?

    For example, for the following bit of XML I want a result (using XPATH?) to
    say that the complex type PHONENO exists

    <COMPANY>
    <NAME>ACME Manufacturing</NAME>
    <PHONENO>
    <COUNTRY>UK</COUNTRY>
    <NUMBER>887323</NUMBER>
    <AREACODE>01865</AREACODE>
    </PHONENO>
    </COMPANY>



    I want a result using XPATH to say that the complex type PHONENO exists too.

    <COMPANY>
    <NAME>ACME Manufacturing</NAME>
    <PHONENO>
    <COUNTRY>UK</COUNTRY>
    <AREACODE>01865</AREACODE>
    <NUMBER>887323</NUMBER>
    </PHONENO>
    </COMPANY>


    However for the following THREE bits of XML, representative of the XML in my
    files, I want the result using XPATH to say that the complex type PHONENO
    does NOT exist.


    This bit of XML has no PHONENO at all.

    <COMPANY>
    <NAME>ACME Manufacturing</NAME>
    <ADDRESS>18 St Giles Street</ADDRESS>
    <ADDRESS>Carfax</ADDRESS>
    <ADDRESS>Oxford</ADDRESS>
    </COMPANY>



    This bit is missing the <NUMBER> element in PHONENO and therefore since
    every element in type PHONENO isn't present, it doesn't contain the
    structure I want.

    <COMPANY>
    <NAME>ACME Manufacturing</NAME>
    <PHONENO>
    <COUNTRY>UK</COUNTRY>
    <AREACODE>01865</AREACODE>
    </PHONENO>
    </COMPANY>



    This bit of XML doesn't have the structure of PHONENO in it that I want
    either, the PHONENO in this XML has an extra element EXCHANGE and therefore
    isn't the same as the complex type phone number defined above.


    <COMPANY>
    <NAME>ACME Manufacturing</NAME>
    <PHONENO>
    <COUNTRY>UK</COUNTRY>
    <AREACODE>01865</AREACODE>
    <NUMBER>887323</NUMBER>
    <EXCHANGE>Banbury 1183</EXCHANGE>
    </PHONENO>
    </COMPANY>


    I'm not sure whether this is difficult of easy but I am certain that it has
    taken me a long time thinking about it before making this post.

    Thanks for everyone thinking about this.

    Clarence (this is for home use, not for a job)
     
    Clarence, Oct 6, 2004
    #1
    1. Advertising

  2. In article <>,
    Clarence <> wrote:

    ><xs:complexType name="PHONENO">
    > <xs:sequence maxOccurs="unbounded">
    > <xs:element name="COUNTRY" type="xs:string"/>
    > <xs:element name="NUMBER" type="xs:string"/>
    > <xs:element name="AREACODE" type="xs:string"/>
    > </xs:sequence>
    ></xs:complexType>


    Do you really want that maxOccurs="unbounded"? And from what you say
    lower down, you don't seem to care about the order.

    You seem to be wanting to find elements called PHONENO that contain a
    COUNTRY, a NUMBER, an AREACODE and nothing else. If so, this will
    match such an element:

    PHONENO[count(*) = 3 and COUNTRY and NUMBER and AREACODE]

    If you want to discover whether there is one of those within the current
    element,

    .//PHONENO[count(*) = 3 and COUNTRY and NUMBER and AREACODE]

    -- Richard
     
    Richard Tobin, Oct 6, 2004
    #2
    1. Advertising

  3. Clarence

    Joris Gillis Guest

    On 6 Oct 2004 14:21:40 GMT, Richard Tobin <> wrote:

    > In article <>,
    > Clarence <> wrote:
    >
    >> <xs:complexType name="PHONENO">
    >> <xs:sequence maxOccurs="unbounded">
    >> <xs:element name="COUNTRY" type="xs:string"/>
    >> <xs:element name="NUMBER" type="xs:string"/>
    >> <xs:element name="AREACODE" type="xs:string"/>
    >> </xs:sequence>
    >> </xs:complexType>

    >
    > Do you really want that maxOccurs="unbounded"? And from what you say
    > lower down, you don't seem to care about the order.


    If "maxOccurs" should really be "unbounded", you could use:

    count(*) div (4*count(COUNTRY)*count(AREACODE)*count(NUMBER)) &lt; 1


    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Ceterum censeo XML omnibus esse utendum
     
    Joris Gillis, Oct 6, 2004
    #3
  4. Clarence

    Clarence Guest

    WOAAAAAH, THANK YOU!!!!

    >
    > Do you really want that maxOccurs="unbounded"? And from what you say
    > lower down, you don't seem to care about the order.


    I don't care about the order you are right. Your observation above just
    reflects my limited understanding of what I wrote :( I put this there
    because some companies have more than one PHONENO and I thought I was making
    things easier for people to help me out.

    > If you want to discover whether there is one of those within the current
    > element,
    >
    > .//PHONENO[count(*) = 3 and COUNTRY and NUMBER and AREACODE]
    >



    Exactly what I was looking for. I was missing the count(*) bit in my code.

    Thank you again Richard. Very clear answer.
    Clarence



    "Richard Tobin" <> wrote in message
    news:ck0v1k$2p3j$...
    > In article <>,
    > Clarence <> wrote:
    >
    >><xs:complexType name="PHONENO">
    >> <xs:sequence maxOccurs="unbounded">
    >> <xs:element name="COUNTRY" type="xs:string"/>
    >> <xs:element name="NUMBER" type="xs:string"/>
    >> <xs:element name="AREACODE" type="xs:string"/>
    >> </xs:sequence>
    >></xs:complexType>

    >
    > Do you really want that maxOccurs="unbounded"? And from what you say
    > lower down, you don't seem to care about the order.
    >
    > You seem to be wanting to find elements called PHONENO that contain a
    > COUNTRY, a NUMBER, an AREACODE and nothing else. If so, this will
    > match such an element:
    >
    > PHONENO[count(*) = 3 and COUNTRY and NUMBER and AREACODE]
    >
    > If you want to discover whether there is one of those within the current
    > element,
    >
    > .//PHONENO[count(*) = 3 and COUNTRY and NUMBER and AREACODE]
    >
    > -- Richard
     
    Clarence, Oct 6, 2004
    #4
  5. Clarence

    Clarence Guest

    You guys have got to stop this, there will be nothing left for me to do.
    Thank you too Joris.


    "Joris Gillis" <> wrote in message
    news:eek:...
    > On 6 Oct 2004 14:21:40 GMT, Richard Tobin <> wrote:
    >
    >> In article <>,
    >> Clarence <> wrote:
    >>
    >>> <xs:complexType name="PHONENO">
    >>> <xs:sequence maxOccurs="unbounded">
    >>> <xs:element name="COUNTRY" type="xs:string"/>
    >>> <xs:element name="NUMBER" type="xs:string"/>
    >>> <xs:element name="AREACODE" type="xs:string"/>
    >>> </xs:sequence>
    >>> </xs:complexType>

    >>
    >> Do you really want that maxOccurs="unbounded"? And from what you say
    >> lower down, you don't seem to care about the order.

    >
    > If "maxOccurs" should really be "unbounded", you could use:
    >
    > count(*) div (4*count(COUNTRY)*count(AREACODE)*count(NUMBER)) &lt; 1
    >
    >
    > --
    > Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    > Ceterum censeo XML omnibus esse utendum
     
    Clarence, Oct 6, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    1,996
    jan V
    Jul 29, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,453
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Anna
    Replies:
    0
    Views:
    542
  4. tfsquare

    Understanding XPath in XSLT

    tfsquare, Dec 2, 2003, in forum: XML
    Replies:
    13
    Views:
    800
    Patrick TJ McPhee
    Dec 5, 2003
  5. goog
    Replies:
    0
    Views:
    519
Loading...

Share This Page