XPath command to extract the following info?

J

JS

Given the below XML file:

<Top>
<Tree id="doc" lang="en" refid="tree">
<blocks>
<block id="block" order="1.1" refid="Type0" type="table">
<parameters>
<parameter id="Name" value="Bob"/>
<parameter id="Position" value="Developer"/>
</parameters>
</block>
<block id="block" order="1.2" refid="Type1" type="table">
<datatables>
<datatable tableid="Type1">
<rows>
<row order="7">
<datum id="a" value="value00"/>
<datum id="b" value="value11"/>
<datum id="c" value="value22"/>
</row>
</rows>
</datatable>
</datatables>
</block>
</blocks>
<outputs>
<output name="out" type="doc"/>
</outputs>
</Tree>
</Top>

I have defined the following rowmapping:

//block/*

Now I would like to extract all id and values from :

parameters and datum

I have tried:

parameter/@id
parameter/@value
datatable/rows/row/datum/@id
datatable/rows/row/datum/@value

but that only gives me the first entry. Any ideas?

How do I get
 
M

Martin Honnen

JS said:
Given the below XML file:

<Top>
<Tree id="doc" lang="en" refid="tree">
<blocks>
<block id="block" order="1.1" refid="Type0" type="table">
<parameters>
<parameter id="Name" value="Bob"/>
<parameter id="Position" value="Developer"/>
</parameters>
</block>
<block id="block" order="1.2" refid="Type1" type="table">
<datatables>
<datatable tableid="Type1">
<rows>
<row order="7">
<datum id="a" value="value00"/>
<datum id="b" value="value11"/>
<datum id="c" value="value22"/>
</row>
</rows>
</datatable>
</datatables>
</block>
</blocks>
<outputs>
<output name="out" type="doc"/>
</outputs>
</Tree>
</Top>

I have defined the following rowmapping:

//block/*

Now I would like to extract all id and values from :

parameters and datum

I have tried:

parameter/@id
parameter/@value
datatable/rows/row/datum/@id
datatable/rows/row/datum/@value

but that only gives me the first entry. Any ideas?

Well you have not provided any details about the context you use XPath
in other than saying that you "have defined ... rowmapping". If you treat
//block/*
as a "row" then you have two rows in your sample XML data, the
parameters child element of the first block id="block" element and the
datatables child element of the second block id="block" element.

Your other XPath expressions certainly select a node-set of id or value
attributes but I guess the environment in which you evaluate them simply
takes the string value of the first node in that node-set.

So map parameter and datum elements to rows, then you can access the id
and value attribute of each row.
 
J

JS

The problem is that in my application I am restricted to specify only ONE
"rowmapping". But I can specify multiple "columnmappings", like:

parameter/@id
parameter/@value
datatable/rows/row/datum/@id
datatable/rows/row/datum/@value

But your suggestion seems to imply that what I want cannot be done unless I
specify multiple "rowmappings"?
 
M

Martin Honnen

JS said:
The problem is that in my application I am restricted to specify only
ONE "rowmapping". But I can specify multiple "columnmappings", like:

parameter/@id
parameter/@value
datatable/rows/row/datum/@id
datatable/rows/row/datum/@value

But your suggestion seems to imply that what I want cannot be done
unless I specify multiple "rowmappings"?

I don't know your application and how exactly relate row mappings to
XPath but you can certainly use
//parameter | //datum
as an XPath expression to form a node-set of each parameter and datum
element. So try that as your row mapping, then as the column mappings use
@id
@value
 

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

Forum statistics

Threads
474,001
Messages
2,570,255
Members
46,852
Latest member
CarlaDowle

Latest Threads

Top