Please Help with the Xpath...

Discussion in 'XML' started by supergreat, Aug 30, 2004.

  1. supergreat

    supergreat Guest

    Could someone help me with the XPath to select LastName from the below
    xml without duplicating the name.
    Note: There are two cities in the above tree that have the same
    information, I just want to pick one, not the two,
    and I want my select to check the two cities and just pick one... Here
    is the one I wrote, It pick up two, I just
    want one

    /Residence/*/City[@id='16']/LastName
    'Brown'
    'Brown'


    <Residence>
    <California>
    <City active="true" id="16">
    <FirstName>James</FirstName>
    <LastName>Brown</LastName>
    <Phone>(800) 445-4545</Phone>
    <Fax>(800) 445-4500</Fax>
    </City>
    <City active="true" id="17">
    <FirstName>James</FirstName>
    <LastName>Brown</LastName>
    <Phone>(800) 445-4545</Phone>
    <Fax>(800) 445-4500</Fax>
    </City>
    </California>
    <Colorado>
    <City active="true" id="16">
    <FirstName>James</FirstName>
    <LastName>Brown</LastName>
    <Phone>(800) 445-4545</Phone>
    <Fax>(800) 445-4500</Fax>
    </City>
    <City active="true" id="17">
    <FirstName>Mary</FirstName>
    <LastName>Bob</LastName>
    <Phone>(800) 445-2245</Phone>
    <Fax>(800) 445-2200</Fax>
    </City>
    </Colorado>
    </Residence>



    --
    supergreat
    ------------------------------------------------------------------------
    Posted via http://www.forum4designers.com
    ------------------------------------------------------------------------
    View this thread: http://www.forum4designers.com/message113822.html
     
    supergreat, Aug 30, 2004
    #1
    1. Advertising

  2. In article <4designers.com>,
    supergreat <4designers.com> wrote:

    [...]
    % and I want my select to check the two cities and just pick one... Here
    % is the one I wrote, It pick up two, I just
    % want one
    %
    % /Residence/*/City[@id='16']/LastName
    % 'Brown'
    % 'Brown'

    How about

    /Residence/*/City[@id='16'][1]/LastName

    ? That's untested, but it should pick up the first city in the list.
    --

    Patrick TJ McPhee
    East York Canada
     
    Patrick TJ McPhee, Aug 31, 2004
    #2
    1. Advertising

  3. supergreat

    Marrow Guest

    Hi,

    As a pure XPath expression try something like...

    /Residence/*/City[@id='16']/LastName[not(. =
    .../../preceding-sibling::*/City[@id = '16']/LastName)]

    However, if you are doing this within XSLT then using XPath (and axes) to
    obtain distinct values is rarely the most efficient method.

    HTH
    Marrow
    http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
    http://www.topxml.com/Xselerator



    "supergreat" <4designers.com> wrote in message
    news:4designers.com...
    >
    > Could someone help me with the XPath to select LastName from the below
    > xml without duplicating the name.
    > Note: There are two cities in the above tree that have the same
    > information, I just want to pick one, not the two,
    > and I want my select to check the two cities and just pick one... Here
    > is the one I wrote, It pick up two, I just
    > want one
    >
    > /Residence/*/City[@id='16']/LastName
    > 'Brown'
    > 'Brown'
    >
    >
    > <Residence>
    > <California>
    > <City active="true" id="16">
    > <FirstName>James</FirstName>
    > <LastName>Brown</LastName>
    > <Phone>(800) 445-4545</Phone>
    > <Fax>(800) 445-4500</Fax>
    > </City>
    > <City active="true" id="17">
    > <FirstName>James</FirstName>
    > <LastName>Brown</LastName>
    > <Phone>(800) 445-4545</Phone>
    > <Fax>(800) 445-4500</Fax>
    > </City>
    > </California>
    > <Colorado>
    > <City active="true" id="16">
    > <FirstName>James</FirstName>
    > <LastName>Brown</LastName>
    > <Phone>(800) 445-4545</Phone>
    > <Fax>(800) 445-4500</Fax>
    > </City>
    > <City active="true" id="17">
    > <FirstName>Mary</FirstName>
    > <LastName>Bob</LastName>
    > <Phone>(800) 445-2245</Phone>
    > <Fax>(800) 445-2200</Fax>
    > </City>
    > </Colorado>
    > </Residence>
    >
    >
    >
    > --
    > supergreat
    > ------------------------------------------------------------------------
    > Posted via http://www.forum4designers.com
    > ------------------------------------------------------------------------
    > View this thread: http://www.forum4designers.com/message113822.html
    >
     
    Marrow, Aug 31, 2004
    #3
    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,976
    jan V
    Jul 29, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,417
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Anna
    Replies:
    0
    Views:
    529
  4. goog
    Replies:
    0
    Views:
    510
  5. KK
    Replies:
    2
    Views:
    596
    Big Brian
    Oct 14, 2003
Loading...

Share This Page