Jump to content
Changes to the Jaspersoft community edition download ×

How to read attribute value in tags by XPath?


2004 IR Help

Recommended Posts

By: zoran_k - zoran_k

How to read attribute value in tags by XPath?

2005-08-10 00:35

For example:

< A my_attribute1 = "x" my_attribute2="y">.

Question:

Could I define XPath in any part of *.jrxml to read values 'x' or 'y'?

I do not need attribute in use to find tag node. I need to read values of attributes. Am I wrong or in JRXmlDataSource no option to tecognize what is root tag for procesing a Document or what is a attribute in tag?

 

 

 

 

By: Lucian Chirita - lucianc

RE: How to read attribute value in tags by XP

2005-08-10 01:34

Hi

 

Have you tried setting the field description as @my_attribute1? I mean

<field name="my_field1" class="java.lang.String">

<fieldDescription>

<![CDATA[@my_attribute1]]>

</fieldDescription>

</field>

 

HTH,

Lucian

 

 

 

 

By: zoran_k - zoran_k

RE: How to read attribute value in tags by XP

2005-08-10 02:39

Thanks Lucianc for respond, but not work!

I got 'null' in report.

 

I think that field name 'my_field1' in jrxml file after procesing source *.xml file is reference to one tag element with name 'my_field1' and read element value but no attribute value. FieldDescription can not manages with own description : CDATA[@my_attribute1].

Or some triks lay in Exspression in text field in detail band?

I have had debugged thru Eclipse3.1 and JR0.6.8 in Class 'JRXmlDataSource' method 'xpathAPI.selectSingleNode(currentNode, expression)' and expression had value '@my_attribute1', bat that was parsing reference for node wich has not existed. That was a reasone to get 'null' value. Problems probably lay in 'xpathAPI.selectSingleNode(...,...)' method wich not regognize what is a attribute and what is a element in source .xml fiel. May be I am wrong, if You have any idea or tested examples post it.

Once again, thanks for respond.

Zoran

 

 

 

 

By: Lucian Chirita - lucianc

RE: How to read attribute value in tags by XP

2005-08-10 02:58

You're right, Zoran, my suggestion doesn't work in 0.6.8 (I tested it with the CVS version).

 

JRXmlDataSource has changed since the 0.6.8 release and it now uses xpathAPI.eval instead of xpathAPI.selectSingleNode. You could upgrade to 1.0.0 or get JRXmlDataSource.java from CVS and rebuild 0.6.8.

 

Regards,

Lucian

 

 

 

 

By: zoran_k - zoran_k

RE: How to read attribute value in tags by XP

2005-08-10 05:33

I have took the last jasperreports-1.0.0 source, but do not work! I have compiled complite source, my Classes an so on....

 

I suposed that Fields like 'my_field1' in *.jrxml have to

reference in tag of source *.xml fiel <my_field1 my_attribute = " this is a value of attr.">, and Description of same field 'my_field1' in *.jrxml have to catch ''this is a value of attr.' value of my_attribute in tag of data source. But it does not work. I have saw the new method in JRXmlDataSource, named 'xpathAPI.eval(...,...)'.

 

Please Lucian, could You be so kind and tell me in few words dependence between *.jrxml , source *.xml

in a sense to finding attribute value or give me Yours examples.

 

Thanks in advance, Zoran.

 

 

 

 

By: Lucian Chirita - lucianc

RE: How to read attribute value in tags by XP

2005-08-10 06:11

The JRXmlDataSource works like this:

- you provide in the constructor (as the second argument) an XPath string which is used to retrieve a list of nodes

- each node in this list corresponds to one record/row in the report. The value of a field is computed by evaluating the field description as an XPath using the node from the list as context node.

 

E.g. you have a data xml like

 

<list>

<elem att1="bla" att2="bla2">

<tag3>bla3</tag3>

</elem>

<elem att1="blabla" att2="blabla2">

<tag3>blabla3</tag3>

</elem>

</listt>

 

you should declare the fields as

 

<field name="field1" type="java.lang.String">

<fieldDescription>

<![CDATA[@att1]]>

</fieldDescription>

</field>

<field name="field2" type="java.lang.String">

<fieldDescription>

<![CDATA[@att2]]>

</fieldDescription>

</field>

<field name="field3" type="java.lang.String">

<fieldDescription>

<![CDATA[tag3]]>

</fieldDescription>

</field>

 

and you create the datasource like this:

 

new new JRXmlDataSource("data.xml", "/list/elem"));

 

The field descriptions are evaluated as XPath using the /list/elem nodes as context nodes.

 

HTH,

Lucian

 

 

 

 

By: zoran_k - zoran_k

RE: How to read attribute value in tags by XP

2005-08-10 07:00

That is what I have found, OK. The attributes have to be in a root ,end of XPath. In Yours examples XPAth is "/list/elem" and attributes is in:

<list>

<elem att1="bla" att2="bla2">

<tag3>bla3</tag3>

</elem>

<elem att1="blabla" att2="blabla2">

<tag3>blabla3</tag3>

</elem>

</listt>

,just at the end of XPath.

 

What could be if I need a attributes exactly in a :

 

<tag3 att="I need just this value">bla3</tag3> (above).

 

That is a deeply from end of XPath. I have to point with once more XPath in a same source (useing "or" word "|").

I think during parsing a *.xml data source, xpathAPI.eval(currentNode, expression) have to recognise what is a current node name a what is a attributes in it. My idea was, according a xml file like tramsport form "customer-to-customer" of data source, to provide every single information of data and description of data "attributes". it could be that I am so far from core of JasperReports. Excuse me, taht's are my wishes.

In any case THANK YOU VERY MUCH for answers.

Zoran

 

 

 

 

 

By: Lucian Chirita - lucianc

RE: How to read attribute value in tags by XP

2005-08-10 07:12

For what you need you should simply select the attribute of the "tag3" child of the context node, i.e. set the field description as

<![CDATA[tag3/@att]]>

 

The attribute doesn't have to be in the root/end of XPath, it can be an attribute of one of the children of the root/end node.

 

HTH,

Lucian

 

 

 

 

By: zoran_k - zoran_k

RE: How to read attribute value in tags by XP

2005-08-10 07:34

That is a exelent answer, thanks. I can prolong-extend

XPath in any data field of *,jrxml. That is a point.

 

Thank You very much Zoran

 

 

 

 

By: Lucian Chirita - lucianc

RE: How to read attribute value in tags by XP

2005-08-10 07:29

Also, I was wrong about xpathAPI.eval(), selectSingleNode() works fine in this case. Therefore, you can keep using 0.6.8.

 

Regards,

Lucian

 

 

 

 

By: zoran_k - zoran_k

RE: How to read attribute value in tags by XP

2005-08-10 07:38

Never mind, version *-1.0.0 is in my source now.

You last ansver with <![CDATA[tag3/@att]]> give possibilities to exten a XPath for any deeply fields in *.xml data source.

 

Now ALL WORKS FINE, Thank You

 

 

 

 

By: HSL - lhshyong

RE: How to read attribute value in tags by XP

2005-08-15 19:19

Hi, some question here..

 

what if there is zero or more tag3 inside there and i need to select them, this the example

 

 

<list>

<elem att1="bla" att2="bla2">

<tag3>bla3</tag3>

<tag3>bla3</tag3>

<tag3>bla3</tag3>

</elem>

<elem att1="blabla" att2="blabla2">

<tag3>blabla3</tag3>

</elem>

</listt>

 

<![CDATA[tag3]]> this only can access first record.

How to make it loop through all..?

 

Thanks..

 

 

 

 

 

 

 

By: Lucian Chirita - lucianc

RE: How to read attribute value in tags by XP

2005-08-15 23:58

Hi

 

You need to create a subreport for the <tag3> elements and use

((net.sf.jasperreports.engine.data.JRXmlDataSource) $P{REPORT_DATA_SOURCE}).dataSource("tag3")

as data source for the subreport.

 

Check the "xmldatasource" sample.

 

Regards,

Lucian

 

 

 

 

By: Lucian Chirita - lucianc

RE: How to read attribute value in tags by XP

2005-08-16 00:04

Correction, you have to use subDataSource("tag3") instead of dataSource("tag3").

 

HTH,

Lucian

 

 

 

 

By: HSL - lhshyong

RE: How to read attribute value in tags by XP

2005-08-16 00:46

Hi:

 

thanks for your response, I read the example before, but still havent get it.

 

((net.sf.jasperreports.engine.data.JRXmlDataSource) $P{REPORT_DATA_SOURCE}).subDataSource("tag3")

 

Have u success with this before.? because no matter what parameter i pass in , it throw me the exception.

 

net.sf.jasperreports.engine.JRException: No node available. Iterate or rewind the data source.

 

 

i can send it with this expression

((net.sf.jasperreports.engine.data.JRXmlDataSource) $P{REPORT_DATA_SOURCE}).dataSource("/list/elem/tag3")

 

but it return all the <tag3> node within every <elem>, what i need is <tag3> node within the current <elem>

 

Any Comments..?

 

 

 

 

 

 

By: Lucian Chirita - lucianc

RE: How to read attribute value in tags by XP

2005-08-16 01:33

Actually you have to use subDataSource("elem/tag3").

 

If you want to see an working example, change in the xmldatasource example the datasource expression to

((net.sf.jasperreports.engine.data.JRXmlDataSource) $P{REPORT_DATA_SOURCE}).subDataSource("Customers/Orders")

and use this data xml

<?xml version="1.0" encoding="UTF-8"?>

<Northwind>

<Customers>

<CustomerID>ALFKI</CustomerID>

<CompanyName>Alfreds Futterkiste</CompanyName>

<Orders>

<OrderID>10643</OrderID>

<OrderDate>1997-08-25</OrderDate>

<Freight>29.46</Freight>

<ShipCity>Berlin</ShipCity>

</Orders>

<Orders>

<OrderID>10692</OrderID>

<OrderDate>1997-10-03</OrderDate>

<Freight>61.02</Freight>

<ShipCity>Berlin</ShipCity>

</Orders>

<Orders>

<OrderID>10702</OrderID>

<Freight>23.94</Freight>

<ShipCity>Berlin</ShipCity>

</Orders>

</Customers>

<Customers>

<CustomerID>ANATR</CustomerID>

<CompanyName>Ana Trujillo Emparedados y helados</CompanyName>

<Orders>

<OrderID>10308</OrderID>

<OrderDate>1996-09-18</OrderDate>

<Freight>1.61</Freight>

<ShipCity>M鸩co D.F.</ShipCity>

</Orders>

<Orders>

<OrderID>10625</OrderID>

<OrderDate>1997-08-08</OrderDate>

<Freight>43.9</Freight>

<ShipCity>M鸩co D.F.</ShipCity>

</Orders>

</Customers>

</Northwind>

 

The parent data source needs to be iterated before creating the sub datasource.

 

HTH,

Lucian

 

 

 

 

By: HSL - lhshyong

RE: How to read attribute value in tags by XP

2005-08-16 02:26

Thanks a lot

 

 

 

 

By: zoran_k - zoran_k

RE: How to read attribute value in tags by XP

2005-08-10 05:50

I think that I have found trick.

 

Attributes in tags HAVE TO BE IN ROOT OF Xpath, isn't it? Or for all attributes wich I need in report fields, relativ Xpath have to references it?

Simply , I need to take all attribute values wich belongs to any tag under the root tag of XPath?

Could You confirm me this statements!

By this attribute in root of XPath all works, but only in root!

 

cheers and thanks Lucian, Zoran

 

 

 

 

By: Lucian Chirita - lucianc

RE: How to read attribute value in tags by XP

2005-08-10 06:18

I don't understand what you mean by the attributes being in the root of XPath. I think it would more clear if you could give an example of your data xml structure and an attribute that you're trying to read and it's not working.

 

Regards,

Lucian

 

 

 

 

By: zoran_k - zoran_k

RE: How to read attribute value in tags by XPath?

2005-08-10 07:27

Restriction exists for any attributes wich belongs under

the end of XPath. Look once more (I have wrote above, time 2005-08-10 16:00) :

 

XPAth is "/list/elem"

Attributes must be only and only in "<elem ...>" tags(that is restriction) :

 

<list>

<elem att1="bla" att2="bla2">

<tag3 att9="red_blue">bla3</tag3>

</elem>

<elem att1="blabla" att2="blabla2">

<tag3>blabla3</tag3>

</elem>

</listt>

I can take all attributes from "<elem ..." tags BUT

(Please look my request)

I need attribute from "<tag3", "att9" and value "red_blue" , with a same XPath "/list/elem",(look above where is a "<tag3", under "<elem" tag. That is impossible. "<Elem" tag is root for parsing xml source and is a end of XPath string ).

My examples have many code, I need a time to prepare for easy reading.

 

Thank Lucian and do You understand, what I have saw

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...