I have an xml file containing a lists of nodes with a "date" attribute, storing a date:
<root>
<entry date="2011-12-04" />
<entry date="2011-11-29" />
</root>
Is it possible using a xPath query to retrive all the nodes that have the month of the "date" attribute set to november?
I read on the web (for example here) that this kind of functions exists in the xPath language, but I am not able to figure how to use them.
I should using this in a java application.
The following XPath works for me:
//entry[month-from-date(#date)=11]
(Replace entry with * to select all the nodes with a November date attribute, regardless of the node name.)
I haven't used the date functions, but you could use substring to extract the month and test that value, for example:
'//entry[substring(#date,6,2)="11"]'
returns just
<entry date="2011-11-29" />
Related
It is easy to parse XML in which tags name are fixed. In XStream, we can simply use #XStreamAlias("tagname") annotation. But how to parse XML in which tag name is not fixed. Suppose I have following XML :
<result>
<result1>
<fixed1> ... </fixed1>
<fixed2> ... </fixed2>
</result1>
<result2>
<item>
<America>
<name> America </name>
<language> English </language>
</America>
</item>
<item>
<Spain>
<name> Spain </name>
<language> Spanish </language>
</Spain>
</item>
</result2>
</result>
Tag names America and Spain are not fixed and sometimes I may get other tag names like Germany, India, etc.
How to define pojo for tag result2 in such case? Is there a way to tell XStream to accept anything as alias name if tag name is not known before-hand?
if it is ok for you to get the tag from inside the tag itself (field 'name'), using Xpath, you can do:
//result2/*/name/text()
another option could be to use the whole element, like:
//result2/*
or also:
//result2/*/name()
Some technologies (specifically, data binding approaches) are optimized for handling XML whose structure is known at compile time. Others (like DOM and other DOM-like tree models - JDOM, XOM etc) are designed for handling XML whose structure is not known in advance. Use the tool for the job.
XSLT and XQuery try to blend both. In their schema-aware form, they can take advantage of static structure information when it is available. But more usually they are run in "untyped" mode, where there is no a-priori knowledge of element names or structure, and everything is handled as it comes. The XSLT rule-based processing paradigm is particularly well suited to "semi-structured" XML whose content is unpredictable or variable.
My XML file
<classifications>
<classification sequence="1">
<classification-scheme office="" scheme="CS" />
<section>G</section>
<class>01</class>
<subclass>R</subclass>
<main-group>33</main-group>
<subgroup>365</subgroup>
<classification-value>I</classification-value>
</classification>
<classification sequence="2">
<classification-scheme office="" scheme="CS" />
<section>G</section>
<class>01</class>
<subclass>R</subclass>
<main-group>33</main-group>
<subgroup>3415</subgroup>
<classification-value>A</classification-value>
</classification>
<classification sequence="1">
<classification-scheme office="US" scheme="UC" />
<classification-symbol>324/300</classification-symbol>
</classification>
<classification sequence="2">
<classification-scheme office="US" scheme="UC" />
<classification-symbol>324/307</classification-symbol>
</classification>
</classifications>
I want to parse the value with following condition
required all the classification-symbol element value along with condition office="US"
I tried with below XPath,
NodeList usClassification = (NodeList)xPath.compile("//classifications//classification//classification-scheme[#office=\"US\"]//classification-symbol//text()").evaluate(xmlDocument, XPathConstants.NODESET);
but I'm getting an empty result set,
System.out.println(usClassification.getLength()); //its becomes zero
This XPath (written on two lines to ease readability),
/classifications/classification[classification-scheme/#office='US']
/classification-symbol/text()
will select the classification-symbol text of the classification elements with classification-scheme #office attribute value equal to US:
324/300
324/307
as requested.
classification-symbol is not a child of classification-scheme - they are siblings. Use following-sibling axis to get from "scheme" to "symbol" instead:
//classifications/classification/classification-scheme[#office=\"US\"]/following-sibling::classification-symbol/text()
I want to try parse xml with XPath in Android Application.
My XML file looks like this.
<expenses>
<entry type="fixed">
<amount>200</amount>
<recurring>true</recurring>
<category>Home/Rent</category>
<payee>Ahmet Necati</payee>
<account>1</account>
<startDate>2013-01-01</startDate>
<endDate>2013-01-01</endDate>
</entry>
<entry type="variable">
<amount>150</amount>
<category>Departmental</category>
<payee>Ahmet Necati</payee>
<recurring>true</recurring>
<startDate>2013-01-01</startDate>
<endDate>2013-01-01</endDate>
<account>1</account>
</entry>
</expenses>
and I want to try parse xml with xPath like that
String expression = "/expenses/entry[xs:date(endDate) < xs:date('2013-10-10')]";
NodeList widgetNode = (NodeList) xpath.evaluate(expression, document,
XPathConstants.NODESET);
But I couldnt deal with it. It returns 0 node.
Edit: I want to get all nodes "endDate" less than spesific date for example: I want to get nodes which end Date less than "2013-10-10"
The "XML schema constructor functions" are part of XPath 2.0, but Android only supports XPath 1.0: http://developer.android.com/reference/javax/xml/xpath/package-summary.html
One solution is to register your own function to do the conversion (see XPathFunctionResolver). Another is to look into libraries that support XPath 2.0.
Is there a way to get the IBM Websphere Commerce Foundation framework (WCF) sort data?
E.g. this snippet from a Websphere Commerce JSP file:
<wcf:getData type="com.ibm.commerce.store.facade.datatypes.GeoNodeType[]"
var="geoNodes" varException="geoNodeException" expressionBuilder="findChildGeoNodesByGeoNodeUniqueID">
<wcf:param name="accessProfile" value="IBM_Store_All" />
<wcf:param name="parentUniqueId" value="${provinceId}" />
</wcf:getData>
How would I get this to sort the data by a given data field in GeoNodeType? Can I add something like <wcf:param name="sortBy" value="Description" /> ?
ExpressionBuilder "findChildGeoNodesByGeoNodeUniqueID" from your example is declared in /Stores/WebContent/WEB_INF/config/com.ibm.commerce.store/get-data-config.xml as follows:
<expression-builder>
<name>findChildGeoNodesByGeoNodeUniqueID</name>
<data-type-name>GeoNode</data-type-name>
<expression-template>{_wcf.ap=$accessProfile$}/GeoNode[ParentGeoNodeIdentifier[UniqueID='$parentUniqueId$']]</expression-template>
<param>
<name>accessProfile</name>
<value>IBM_Store_All</value>
</param>
<param>
<name>parentUniqueId</name>
<value></value>
</param>
</expression-builder>
According to expression-builder tag docs, if expression-language is not specified inside expression-builder tag - XPath language is used by default. Unfortunately XPath does not support ordering.
I imagine that you can still implement your own ExpressionBuilder class (I haven't done that), implement any kind of sorting inside this new class, and then specify it in get-data-config.xml
<playingTestCodeDetails classCode="ENT" determinerCode="INSTANCE" >
<realmCode code="QD" />
<id assigningAuthorityName="PRMORDCODE" extension="16494" />
<id assigningAuthorityName="TESTNUMINBOOK" extension="16494" />
<code code="16494" codeSystemName="QTIM" displayName="SureSwab Candidiasis" />
<name use=""></name>
<asSeeAlsoCode classCode="ROL" > <!-- Have repeated Seealsocode section for multiple see also codes and stripped names -->
<realmCode code="QD" />
<code code="7600" displayName="Sample See Also Name" ></code>
</asSeeAlsoCode>
<asSeeAlsoCode classCode="ROL" >
<realmCode code="QD" />
<code code="6496" displayName="Sample See Also Name" ></code>
</asSeeAlsoCode>
</playingTestCodeDetails>
<subjectOf typeCode="SBJ">
<realmCode code="QD" />
<order classCode="OBS" moodCode="EVN" >
<realmCode code="QD" />
<performer nullFlavor="" typeCode="PRF"><!-- Have added this to accomodate the UnitCode-->
<performingLocatedEntity classCode="LOCE" nullFlavor="">
<locatedPerformingSite classCode="ORG" determinerCode="INSTANCE">
<id assigningAuthorityName="ASORDERED" extension="16494" />
</locatedPerformingSite>
</performingLocatedEntity>
</performer>
<origin nullFlavor="" typeCode="ORG"> <!-- Have added this to accomodate the Ordering Lab Code-->
<orderingLocatedEntity classCode="LOCE" >
<locatedOrderingSite classCode="ORG" determinerCode="INSTANCE">
<id assigningAuthorityName="PRMORDCODE" extension="16494"/>
<code code="SJC" codeSystemName="QTIM" codeSystem="ORDERINGLABCODE"/>
</locatedOrderingSite>
</orderingLocatedEntity>
</origin>
<pertinentInformation1 typeCode="PERT">
<realmCode code="QD" />
<clinicalInfo classCode="ACT" moodCode="EVN">
<realmCode code="QD" />
<title>Specialitysample1</title>
<text>Conditionsample1</text>
</clinicalInfo>
</pertinentInformation1>
<subjectOf typeCode="SUBJ">
<realmCode code="QD" />
<annotation classCode="ACT" moodCode="EVN" >
<realmCode code="QD" />
<code code="DOSCATNAME"></code>
<text><![CDATA[SureSwab<sup>®</sup>, <em>Candidiasis</em>, PCR]]></text>
</annotation>
</subjectOf>
</subjectOf>
I have a xml looking like above. I want to parse it; what is the best way to parse it?? DOM, SAX ( i have heard of JAXB, XSLT,.... not sure of this two); Can we have a combination of DOM & SAX to parse a XML??
A simple scenario to attain a tag value using attribute access as "code"
like when code=DOSCATNAME in tag then we need to take up data for corresponding tag.
Other scenario is to access tag and get the hierarchy and access extension attribute of when assigningAuthorityName attribute has value PRMORDCODE.
Can the above two scenarios can be achievable using a Parser??
I am a newbie, please understand what i need to parse & suggest me a thought... thanks in advance...
Use JAXB. Create class model and annotate your classes appropriately. The environment will do the rest.
For example you should create class PlayingTestCodeDetails with properties classCode, determinerCode etc.
I will tell you more: you can kindly ask JAXB to generate the classes for you. Start learning from this article: http://www.roseindia.net/jaxb/r/jaxb.shtml
It will take a couple of hours to start but than you will be done in 15 minutes. If you are using DOM you can start in 15 minutes of learning and the coding a couple of days to parse your XML.
It depends on your need which to use.
Both SAX and DOM are used to parse the XML document. Both has advantages and disadvantages and can be used in our programming depending on the situation.
SAX
• Parses node by node
• Doesn’t store the XML in memory
• We cant insert or delete a node
• SAX is an event based parser
• SAX is a Simple API for XML
• doesn’t preserve comments
• SAX generally runs a little faster than DOM
DOM
• Stores the entire XML document into memory before processing
• Occupies more memory
• We can insert or delete nodes
• Traverse in any direction.
• DOM is a tree model parser
• Document Object Model (DOM) API
• Preserves comments
• SAX generally runs a little faster than DOM
If we need to find a node and doesn’t need to insert or delete we can go with SAX itself otherwise DOM provided we have more memory.
These are few parsers:-
woodstox
dom4j
In addition to SAX and DOM there is STaX parsing available using XMLStreamReader which is an xml pull parser.