Please Help with the Xpath...

S

supergreat

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>
 
P

Patrick TJ McPhee

[...]
% 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.
 
M

Marrow

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top