What can I do to select an attribute value using XPATH query? - java

I am pretty new in XPATH and I have the following problem:
I have this XML content:
<root><status>
<id>0</id>
<message>MY MESSAGE</message>
</status>
<drivers>
<drive id="my ID">
<property1>0</property1>
<property2>104857600</property2>
<property3 />
</drive></drivers>
</root>
What have I to do to select the attribute value named id (I wanto select the "my ID value)
Can you help me?
Tnx
Andrea

To get the attribute node, you can do:
/root/drivers/drive/#id
or
/root/drivers/drive/attribute::id
In most cases this will automatically be converted to a string ('atomized') for you, but in a case where you need to ensure that you are getting back a string value, you can also do:
/root/drivers/drive/#id/string()

This should work:
/root/drivers/drive[1]/#id/text()
equivalent to:
//drive[1]/#id/text()
See there for more XPATH syntax explanation: http://www.w3schools.com/xpath/xpath_intro.asp

Related

How get attribute content by another attribute value with XPath?

I have XML like:
<?xml version='1.0' encoding='UTF-8'?>
<ClinicalDocument xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='urn:hl7-org:v3'
xmlns:ext='urn:hl7-RU-EHR:v1'
xsi:schemaLocation='urn:hl7-org:v3'>
<author>
<time value='20160809000000+0300'/>
<assignedAuthor>
<id root='1.2.643.5.1.13.3.25.1.1.100.1.1.70' extension='1'/>
<id root='1.2.643.100.3' extension='03480134121'/>
<id nullFlavor='NI'/>
</assignedAuthor>
</author>
</ClinicalDocument>
I have to get extension in id with root's value = 1.2.643.100.3.
I must use XPath 2.0.
I have tried:
*[name()='ClinicalDocument']/*[name()='author']/*[name()='assignedAuthor']/*[name()='id' and #id='1.2.643.100.3']/#extension. Not working
/*[name()='ClinicalDocument']/*[name()='author']/*[name()='assignedAuthor']/*[name()='id'][2]/#extension, but order of ids can
mixed. So that, I should retrieve by id's value
It's needed to me for retrieving value by Java's XPathExpression
First, bind namespace prefix, u: to urn:hl7-org:v3.
Then, this XPath,
//u:id[#root='1.2.643.100.3']/#extension
will return 03480134121, as requested.
If you are unable to bind a namespace prefix, you can instead use this XPath,
//*[local-name() ='id' and #root='1.2.643.100.3']/#extension
which will also return 03480134121, as requested.
Correct XPath: /*[name()='ClinicalDocument']/*[name()='author']/*[name()='assignedAuthor']/*[local-name()='id' and #root='1.2.643.100.3']/#extension

Why is DOM doing this? (Wrong nodeName XML)

I have this XML (just a little part.. the complete xml is big)
<Root>
<Products>
<Product ID="307488">
<ClassificationReference ClassificationID="AR" Type="AgencyLink"/>
<ClassificationReference ClassificationID="AM" Type="AgencyLink">
<MetaData>
<Value AttributeID="tipoDeCompra" ID="C">Compra Centralizada</Value>
</MetaData>
</ClassificationReference>
</Product>
</Products>
</Root>
Well... I want to get the data from the line
<Value AttributeID="tipoDeCompra" ID="C">Compra Centralizada</Value>
I'm using DOM and when I use nodoValue.getTextContent() I got "Compra Centralizada" and that is ok...
But when I use nodoValue.getNodeName() I got "MetaData" but I was expecting "Value"
What is the explanations for this behaviour?
Thanks!
Your nodeValuevariable most likely points to the MetaData node, so the returned name is correct.
Note that for an element node Node.getTextContent() returns the concatenation of the text content of all child nodes. Therefore in your example the text content of the MetaData element is equal to the text content of the Value element, namely Compra Centralizada.
I guess your are getting the Node object using getElementsByTagName("MetaData"). In this case nodoValue.getTextContent() will return the text content correctly but to get the node name you need to get the child node.
Your current node must be MetaData and getTextContent() will give all the text within its opening and closing tags. This is because you are getting
Compra Centralizada
as the value. You should get the first child using getChildNodes() and then can get the Value tag.

Solr query only returns Id's

I have wanting to import data from a table and index it using solr..
I am using solr-tomcat admin panel.
But whenever I query it returns to me only the id's and value.
I have also tried adding FIELDS to fl , but that also does not help.
here is my data-config.xml file:
<dataConfig>
<dataSource type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/{DB_NAME}"
user="{DB_USER}"
password="{DB_PASSS}"
/>
<document>
<entity name="id" query="select s3_location,file_name from video">
<field column="s3_location" name="s3_location"/>
<field column="file_name" name="file_name"/>
</entity>
</document>
</dataConfig>
Is there any way to get the above s3_location and file_name fields also.
You need to specify the actual field names in the fl parameter or use * to indicate all fields. Also, please note that the fields must have been defined with stored=true in your schema.xml file for them to be returned/visible during a query.
fl=id,s3_location,file_name
fl=*
Are you sure you are importing the data at all? If you start with empty index, do you get anything?
The reason I ask is because you are not mapping the id field explicitly. Now, I believe there is implicit mapping of the fields by Jdbc data source based on names, but relying on it is risky when you are just starting.
Otherwise, like Paige said, make sure you defined those fields in your schema and that they are actually stored.

How to retrieve value of an attribute which contains colon in the attribute name using xpath

I have a simple requirement where in I need to fetch the value of attribute xml:id i.e af1.
I am making use of a SAXParser and here is my xpath:a/aff/#xml:id on the contrary I was able to fetch value of using the xpath:a/aff/#value.
But i was unable to retrieve the value could you please help me?
<?xml version="1.0" encoding="UTF-8" ?>
<a>
<aff xml:id="af1" value="a">
<uAff>
Hello
</uAff>
</aff>
<aff xml:id="corr1">
<uAff>
Hello1
</uAff>
</aff>
</a>
Thanks in advance.
To get the value of the attributes you could use:
/a/aff/#*[name()='xml:id']
/a/aff/#xml:id works just fine in getting the values...
Are you trying to get both values?
If you are trying to get just the first value you could use /a/aff[1]/#xml:id

query to get element that has particular value using xpath

I doubt there is a way to get the element which has a particular value (text) from xml document using xpath.
Example doc:
<domain log-root="/logs" application-root="/applications"><resources>
<jdbc-resource pool-name="SamplePool" jndi-name="jdbc/sample" />
<jdbc-resource pool-name="TimerPool" jndi-name="abc">text1</jdbc-resource>
<jdbc-resource pool-name="TimerPool" jndi-name="def">text2</jdbc-resource>
<jdbc-resource pool-name="TimerPool" jndi-name="ghi">text3</jdbc-resource></resources</domain>
Example xPath Query:
/domain//jdbc-resource[#pool-name='TimerPool']/text()='text2'
Please post your ideas if there is any.
Use:
/domain/*/jdbc-resource[#pool-name='TimerPool' and .='text2']
or you may use:
/domain/*/jdbc-resource[#pool-name='TimerPool'][.='text2']
Both expressions above select all jdbc-resource elements the string value of whose pool-name attribute is "TimerPoool" and whose string value (of the jdbc-resource element) is "text2" and that are grand-children of the top element of the XML document.
Well, text() should do. http://www.w3schools.com/xpath/xpath_examples.asp
Have you tried it already? Also, check the path, it could be
//jdbc-resource[#pool-name='TimerPool']/text()='text2'
or
/domain/resource/jdbc-resource[#pool-name='TimerPool']/text()='text2'
or
//resource/jdbc-resource[#pool-name='TimerPool']/text()='text2'

Categories