xpath xml files within matlab - matlab

Is it possible to read,compare and look for a specefic strings in xml files using xpath from Matlab ?
I don't find any documentation.
could someone give me an example ?
<?xml version="1.0" encoding="UTF-8"?>
<address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation='test.xsd'>
<lists name="myState">
<description name="-temp">-20</description>
<description name="localization">north</description>
<description name="-state">false</description>
</lists>
</address>
<language language="english" name="">
<description name="population">5000</description>
</language>
here to access to description name="localization"> , I did :
docNode = xmlread(myXMLFILE);
factory = XPathFactory.newInstance;
xpath = factory.newXPath;
% compile and evaluate the XPath Expression
expression = xpath.compile(adress/lists/description')
description = expression.evaluate(docNode, XPathConstants.NODE);
descriptionValue = phoneNumberNode.getTextContent % this gives me -20
how can I get the value of ?
thanks

Have you tried Google? One of the first link gave me a good example of using XPath on FileExchange:
Using XPath from MATLAB
An XPath package started shipping as part of Java 5, so we can use it
from MATLAB. This is a simple example.
The Java XPath API tutorial on ibm.com is a good introduction to XPath
in Java.
% Import the XPath classes
import javax.xml.xpath.*
% Construct the DOM.
doc = xmlread(which('demos/demos.xml'));
% Create an XPath expression.
factory = XPathFactory.newInstance;
xpath = factory.newXPath;
expression = xpath.compile('//demosection/label');
% Apply the expression to the DOM.
nodeList = expression.evaluate(doc,XPathConstants.NODESET);
% Iterate through the nodes that are returned.
for i = 1:nodeList.getLength
node = nodeList.item(i-1);
disp(char(node.getFirstChild.getNodeValue))
end
Another good article is in Mike's blog - XML and MATLAB: Navigating a Tree. It has a part specifically on using XPath.

Related

Accessing DOM element with namespace in Freemarker template

I have the following XML
<wmi xmlns="http://www.exmple.com/XMLSchema/fulfillment/v1/order/orderShipment" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/XMLSchema/fulfillment/v1/order/orderShipment OrderShipmentNotification.xsd">
<wmiHeader>
<fileID>693401.20160229.130342.3541254</fileID>
<version>2.0.0</version>
<messageType>FSN</messageType>
<genDate>2016-02-29T13:03:42Z</genDate>
<from>
</from>
</wmiHeader>
<orderShipNotification>
<shipmentHeader dateTimeCreated="2016-02-29T13:03:42Z" requestNumber="2574445351883" />
<shipmentDetails actualShipmentDateTime="2016-02-29T12:18:54Z" carrierCode="XX" carrierMethodCode="XX-01">
<shipmentPackDetails trackingNumber="9361289672090007124848" trackingURL="https://example.com/go/TrackConfirmAction_input?qtc_tLabels1=323434">
<shipmentPackLineDetails requestLineNumber="1" partnerItemID="FXT-CC-LB" itemQtyShipped="1" />
</shipmentPackDetails>
</shipmentDetails>
</orderShipNotification>
</wmi>
I am getting error in Freemarker template when I am trying to access.
${orderShipNotification.shipmentDetails.#actualShipmentDateTime[0]!""}
If I delete the namespaces from the document it is working fine. I deleted the following content from the XML
xmlns="http://www.exmple.com/XMLSchema/fulfillment/v1/order/orderShipment" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/XMLSchema/fulfillment/v1/order/orderShipment OrderShipmentNotification.xsd"
I did some investigation. The is a ftl directive. But it is still not clear how this will solve the problem. Please let me know how I can access the attributes.
http://freemarker.incubator.apache.org/docs/ref_directive_ftl.html#ref.directive.ftl
Start the template with
<#ftl ns_prefixes={"D":"http://www.exmple.com/XMLSchema/fulfillment/v1/order/orderShipment"}>
This sets the namespace as the default (D stands for default). Note that if you will also use XPath queries, there you will have to write out the D: before the element names (this is an XPath restriction).
This is documented here: http://freemarker.org/docs/xgui_imperative_learn.html

generate tag xml in JAVA

How i generate tag XML like this in java to hit HTTP POS SOA ?
Parameter:
<opNameA>
<Header>
<code11>A34</code11>
<code12>POS</code12>
</Header>
<opname>
<code1>1234</code1>
<code2>123456</code2>
<code3>abc123</code3>
<code4>xyz123</code4>
<code5>78689332</code5>
<code6>50000</code6>
<code7>2013-11-11 10:15</code7>
</opname>
</opNameA>
You could use either DOM or SAX parsing for generating xml in java. Please follow the below links for the basics.
http://www.w3schools.com/dom/
http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/

Xforms checkbox - replacing True / False with Y / N

I am writing a form (betterFORMs/Xforms) to be displayed to the user with a selection of checkboxes.
If a checkbox is empty the form should bind an "N" into the element. When ticked, a "Y".
I realise there are answers to this question already but I have tried all answered solutions with no luck.
The first solution I attempted to use is here - stackoverflow link
(the first solution looks good, but I have had more success with solution 2 as I am not using Orbeon)
The answer given is what I am looking for, but I am having trouble implementing this into my form. I am not using Xhtml or Orbeon and so the binding I use seems to be different to that used in the solution.) I have tried tweaking this code to fit into my form but I get a repetitive error from the xml parser every time I load the page - but it doesn't point me to anywhere related to the new code.
The next solution I have tried is found here - stackoverflow link
This answer has had the best results in my code because the checkbox values change to N when not used and when they are deselected. The problem I have with this solution is that the Y set in the form element is contained in braces - [].
output example:
<addressProof>N</addressProof><other>[Y]</other><otherText>_text_</otherText>
Here is a snippet of my form:
model:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns="http://www.w3.org/2002/06/xhtml2" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
<xsl:output method="xml" />
<xsl:template match="/">
<xforms:model id="documentsChecklist">
<xforms:instance>
<actionform xmlns="">
<xforms:model id="documentsChecklist">
<xforms:instance>
<actionform xmlns="">
<detail>
<other></other>
<otherText></otherText>
</detail>
</actionform>
</xforms:instance>
<xforms:bind id="other" nodeset="/actionform/detail/other" calculate="choose(. = 'Y', ., 'N')"/>
<xforms:bind nodeset="/actionform/detail/otherBox" relevant="/actionform/detail/other ='Y'" />
</xforms:model>
form:
<div id="formBody"><br />
<xforms:select bind="other" appearance="full" align="right">
<xforms:item>
<xforms:label>Other</xforms:label>
<xforms:value>Y</xforms:value>
</xforms:item>
</xforms:select>
<xforms:input ref="/actionform/detail/otherText">
<xforms:label>Please specify:*</xforms:label>
</xforms:input>
</div>
</xsl:template>
</xsl:stylesheet>
Why does the checkbox value now get set to "[Y]" instead of "Y"? (Could it be something to do with an Array?) Thanks.
PS. I know I could do this using a boolean for each checkbox, with the checkbox value corresponding to the boolean, which in turn updates the bind value. I would rather not have to have a big block of boolean elements and binds as I have a large amount of check boxes. this solution has an example here - stackoverflow link
A select control allows you to select more than one item and I wonder if it is why the XForms implementation you are using is adding square brackets (according to specifications selected values have to be separated by a space character, which is not always very convenient by the way).
I am afraid that XForms 1.1 and XForms 2.0 require to use extra intermediate nodes and bindings. It would be useful to be able to add 2 XPath expressions for bindings: one to convert node value to control value and the other one back from control value to node value.
As a workaround, I use another extension in XSLTForms: XSLT stylesheets for converting instances.
-Alain

x3d import and export nodes from inline url

I need to get access to nodes from an inline x3d file in a parent x3d file. For example, say we have a room model as an x3d file. We populate this room with several chairs. We use the inline url to populate room.x3d with several chair.x3d files. We've got something like this inside the room.x3d file to place chairs:
<Transform DEF = 'Chair'
translation = '0 0 0'
scale = '1 1 1'
rotation='-0.600546 0.600546 90 0'>
<Inline DEF ='chr' url = 'Chair.x3d' />
</Transform>
Now, I need to get access to a few nodes within Chair.x3d to manipulate the scene. I have read about IMPORT and EXPORT and how they are used to get nodes from an inline url, however I have not found a good working example yet. I've looked at this:
http://www.web3d.org/x3d/content/examples/Basic/development/_pages/page27.html
But it does not seem to work.
I've also read this:
http://www.web3d.org/files/specifications/19775-1/V3.2/Part01/components/networking.html
But the syntax is VRML as opposed to x3d.
If anyone could give me a quick example of how to use IMPORT and EXPORT and how to route the nodes from the inline url so I can send it events, etc. it would be greatly appreciated. Let me know if I'm not being clear enough.
This works for me:
Source FILE of X3D object ( a brown cylinder):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 3.0//EN" .stuff..>
<X3D profile='Interchange' version='3.0'
xmlns:xsd='http://www.w3.org/2001/XMLSchema-instance' ..stuff..>
<Scene>
<Transform DEF='XFER_OBJECT'>
<Shape>
<Cylinder radius='1' height='1'/>
<Appearance>
<Material diffuseColor='.9 .3 .3'/>
</Appearance>
</Shape>
</Transform>
<Export localDEF='XFER_OBJECT' as='Cyl'/>
</Scene>
</X3D>
and the url/import code in my xhtml file:
<Transform translation='2 0 0' >
<Inline DEF='objectBrnCyl' url='"test33.x3d"' />
</Transform>
<IMPORT InlineDEF='objectBrnCyl' exportDEF='Cyl' as='brnCyl'/>
Haven't finished case testing the commands yet -- the documentation is a little spooky.

XML based template engine in Java?

Can somebody suggest me a template engine (preferably written in Java) that could generate any text that I like from given XML input?
StringTemplate, FreeMarker
How about XSLt? You may use JAXP to do the processing.
You can use XSLT, it is not restricted to generating only XML output. It is restricted to XML input. Use the xsl:output tag do define the type of output you will be generating.
E.g. to generate text output
<xsl:output method="text" encoding="UTF-8"/>
To generate XML output with indentation
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>