Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Xquery, XPath help 1

Status
Not open for further replies.

misterlar

Programmer
Apr 19, 2010
1
US
I am trying to get the value of an element that has two attributes and have tried several methods but am not being successful.

This query gets me the closest:
//*/@ID/(name(), data(.))

But it returns the 3 values from the following elements:
<Accession-ID ID="" Type="Clinic-AccID"/>
<Accession-ID ID="7666532" Type="Chart-ID"/>
<Accession-ID ID="B6490491" Type="Lab-AccID"/>

What I really want is the value for the element with a Type of Chart-ID.

Any ideas of how to do that would be greatly appreciated.

Below is more of the xml to give more context:
<?xml version="1.0" encoding="UTF-8"?>
<LabReport>
<Accession Acc-Result-ID="" Lab-ID="" LabClinicExt-ID="" Location-ID="" Order-Status="F">
<AccessionHeader>
<Accession-ID ID="" Type="Clinic-AccID"/>
<Accession-ID ID="7666532" Type="Chart-ID"/>
<Accession-ID ID="B6490491" Type="Lab-AccID"/>
<TimeStamp Type="Order Received DateTime" Value="04/05/2010 11:29 PM"/>
<Pet>
<Name>RACCOON 127</Name>
<Age/>
<Sex>F</Sex>
<Species Ext-ID=" "/>
<Breed Ext-ID=" ">RACCOON</Breed>
<Owner>WILDCARE</Owner>
<Doctor/>
</Pet>
</AccessionHeader>
.
.
.
</Accession>
</LabReport>
 
Don't know what exactly you want.

>the value of an element that has two attributes
What is the value of an element? An "attribute" is not usually called as an "element". So I would think an element would be something like Accession-ID. But what is the value of Accession-ID, for instance?

>//*/@ID/(name(), data(.))
What has this xpath has to do with the need of an element having "two attributes"? It matches an attribute ID and that's all. What has it to do with the need mentioned above?

>What I really want is the value for the element with a Type of Chart-ID.
Then why the closest xpath has @ID rather than anything else, such as @Type?
 
I think you need 2 steps here - first step to the find the Accession-ID node that has the attribute matching 'Chart-ID'. The next step is to take found node and extract the ID attribute from it.

Here's a PHP example using simplexml:
Code:
<?php

$xml = '<?xml version="1.0" encoding="UTF-8"?>
<LabReport>
  <Accession Acc-Result-ID="" Lab-ID="" LabClinicExt-ID="" Location-ID="" Order-Status="F">
    <AccessionHeader>
      <Accession-ID ID="" Type="Clinic-AccID"/>
      <Accession-ID ID="7666532" Type="Chart-ID"/>
      <Accession-ID ID="B6490491" Type="Lab-AccID"/>
      <TimeStamp Type="Order Received DateTime" Value="04/05/2010 11:29 PM"/>
      <Pet>
        <Name>RACCOON 127</Name>
        <Age/>
        <Sex>F</Sex>
        <Species Ext-ID=" "/>
        <Breed Ext-ID=" ">RACCOON</Breed>
        <Owner>WILDCARE</Owner>
        <Doctor/>
      </Pet>
    </AccessionHeader>
  </Accession>
</LabReport>';

$d = simplexml_load_string($xml) ;
$search = $d->xpath("//Accession-ID[@Type='Chart-ID']") ;
foreach($search as $node)
  echo "{$node['ID']} - {$node['Type']}<br/>" ;
?>

Greg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top