EclipseLink MOXy #XmlPath is returning the incorrect data for contains - eclipselink

I am attempting to unmarshal an XML data stream with EclipseLink MOXy with an XPath using the contains function.
When I apply the sample XPath directly to the sample XML data stream I get the correct value returned (Ref_number_1). However, when I unmarshal this using MOXy, the value that is set for refNumber1 is "Ref_number_2".
Does MOXy not support this type of XPath? It would appear that it at least is understanding it because it's not throwing an error, just setting the wrong value.
Anyone have any experience with this sort of thing? Know of a better approach?
Thanks for any help.
Marshal code:
String s = //xml stream from restful service (see xml example below);
StringReader sr = new StringReader(s);
ReferenceNumber refNum = (ReferenceNumber)marshaller.unmarshal(
new StreamSource(sr));
Member annotation:
#XmlPath("Header/ReferenceNumbers/ReferenceNumber[contains(ReferenceNumberType, \"REF_NUMBER_TYPE_1\")]/ReferenceNumber/text()")
private String refNumber1;
XML data:
<?xml version="1.0" encoding="UTF-8"?>
<Document>
<Header>
<ReferenceNumbers>
<ReferenceNumber>
<ReferenceNumber>Ref_number_1</ReferenceNumber>
<ReferenceNumberType>REF_NUMBER_TYPE_1</ReferenceNumberType>
</ReferenceNumber>
<ReferenceNumber>
<ReferenceNumber>Ref_number_2</ReferenceNumber>
<ReferenceNumberType>REF_NUMBER_TYPE_2</ReferenceNumberType>
</ReferenceNumber>
</ReferenceNumbers>
</Header>
</Document>

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
Currently MOXy does not support XPaths of that form. Currently a conditional check must be on an attribute contained within the element such as (see: http://blog.bdoughan.com/2011/03/map-to-element-based-on-attribute-value.html).
#XmlPath("personal-info/name[#type='first']/text()")
String firstName;
I have entered the following bug so that we improve the validation that we do on our #XmlPath annotation:
http://bugs.eclipse.org/397101
I am also interested in the use case described in your question. Would you mind entering an enhancement requires against the MOXy component for this:
https://bugs.eclipse.org/bugs/enter_bug.cgi?product=EclipseLink

Related

Exception cx_st_match_element when deserializing XML?

I'm having trouble getting a simple transformation for XML to work in ABAP. I keep getting the exception cx_st_match_element when I try to execute it on a test XML document inside of a report.
I have the following XML that I want to transform into an ABAP internal table:
<?xml version="1.0" encoding="UTF-8"?>
<studenten xmlns="http://www.foo.be/bar/preinschrijvingsflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.foo.be/bar/preinschrijvingsflow bar_studenten.xsd">
<student>
<barGuid>1</barGuid>
<familienaam>Doe</familienaam>
<voornaam>John</voornaam>
<geslacht>1</geslacht>
<nationaliteit>BE</nationaliteit>
<geboortedatum>1995-11-18</geboortedatum>
<geboorteplaats>Antwerpen</geboorteplaats>
<email>John.Doe#gmail.com</email>
<straatNummer>Grote Markt 1 bus 0102</straatNummer>
<postcode>1000</postcode>
<gemeente>Brussel</gemeente>
<land>BE</land>
<telefoonnummer>+32123456789</telefoonnummer>
<academiejaar>2021</academiejaar>
</student>
</studenten>
To this end I defined the following simple transformation I called zc_tr_student:
<?sap.transform simple?>
<tt:transform
xmlns="http://www.foo.be/bar/preinschrijvingsflow"
xmlns:tt="http://www.sap.com/transformation-templates"
xmlns:ddic=" http://www.sap.com/abapxml/types/dictionary">
<tt:root name="studenten" type="ddic:ZCTT_bar_STUDENT"/>
<tt:template>
<studenten>
<tt:loop ref=".studenten" name="studenten">
<student>
<barGuid tt:value-ref="$studenten.bar_guid"/>
<familienaam tt:value-ref="$studenten.familienaam"/>
<voornaam tt:value-ref="$studenten.voornaam"/>
<geslacht tt:value-ref="$studenten.geslacht"/>
<nationaliteit tt:value-ref="$studenten.nationaliteit"/>
<geboortedatum tt:value-ref="$studenten.geboortedatum"/>
<geboorteplaats tt:value-ref="$studenten.geboorteplaats"/>
<email tt:value-ref="$studenten.email"/>
<straat_nummer tt:value-ref="$studenten.straat_nummer"/>
<postcode tt:value-ref="$studenten.postcode"/>
<gemeente tt:value-ref="$studenten.gemeente"/>
<land tt:value-ref="$studenten.land"/>
<telefoonnummer tt:value-ref="$studenten.telefoonnummer"/>
<academiejaar tt:value-ref="$studenten.academiejaar"/>
</student>
</tt:loop>
</studenten>
</tt:template>
</tt:transform>
In the tt:value-refattributes I refer to the field in the DDIC line type of the ABAP internal table corresponding to the tag in the XML.
If I call this simple transformation from an ABAP report like this:
call transformation zc_tr_student
source xml lv_bxml
result studenten = p_student.
The cx_st_match_element is thrown.
I validated both the syntax of the file and its adherence to the schema. The XML file and the XSD file are present in the same directory on the application server. I have no idea why the ST fails as the cx_st_match_element instance does not have any useful information except that it expected a studenten element. That element is clearly present in the XML file as the root element.
I'm inexperienced with defining simple transformations and I can't spot my error myself. Thank you in advance for your help,
Joshua

Allow custom attribute to a script tag element?

I can't validate script tag with attribute nomodule.
I am using odoo framework which is a python backend. It is using lxml to validate xml views or pages. I am building a view with a script tag like:
<script src="src.js" nomodule></script>
It returns an error
lxml.etree.XMLSyntaxError: Specification mandate value for attribute nomodule
However this should be valid according to https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script
Is there a way so that I can make the parser ignore this new attribute or I can bypass such as special data or character.
That's possibly because XML != HTML. And as you can see in the error, it's an XML error.
Is an xml attribute without a value, valid? --> your attribute isn't valid.
You need to specify the attribute value always in xml. Odoo uses xml to produce html, so you need to comply with xml rules. You can do it in this case by specifying an empty value for xml attribute like this:
<script src="src.js" nomodule=””></script>

Edit XML tag by attribute

I have a XML document which looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<Data key="dailyKey">19283</Data>
</Configuration>
And in my vb.net program I want to change the value from "<Data>" by the attribute "dailyKey"
I have tried to understand myself on this but cannot figure out how to edit TAG by ATTRIBUTE
Please help, Richard
You could use XPath expressions and the SelectSingleNode method like this:
Dim node = xmlDoc.SelectSingleNode("//data[#key=""dailyKey""]")
Then you can modify the value of node as you wish to. You can find more XPath examples at MSDN.
The workflow in its entirety would be:
1. Load the XML Document for manipulation
You can use the XmlDocument class to load (and subsequently save) your XML Document like this:
Dim xmlDoc As New XmlDocument()
xmlDoc.Load("<Here goes your url // You can also feed in a stream to this method>")
2. Locate the node you want to modify
As mentioned earlier, use the SelectSingleNode function to locate the node you are trying to modify the value of. It takes an XPath expression.
Dim node = xmlDoc.SelectSingleNode("//data[#key=""dailyKey""]")
3. Modify the node
You can now edit the node (tag) in whatever way you wish. It seems you want to edit the contained value. Do it by changing the Value property of the XmlNode:
node.Value = 224062 'Random value. Change to suit your needs.
4. Save the XML Document (Obviously :P)
xmlDoc.Save()

How to query database using saxon sql:query where db.id='value of xml attribute'

I have a requirement where i need to query database using saxon sql;query by applying where clause, where database_table.ProductID should match with incoming xml input productId
Here is what i tried so far:
<sql:query connection="$sql.conn" table="table_name" column="Product_ID" row-tag="row" column-tag="col" where="Product_ID="<xsl:value-of select="ProductItem/ProductItemId/text()"/>"" />
I am getting following Exception:
SXXP0003: Error reported by XML parser: Element type "sql:query" must be followed by either attribute specifications, ">" or "/>".
I am finding it difficult to format the where clause in XPath, can any one suggest what would be correct format. Thanks in Advance.
Try to use to use an attribute value template where you put the XPath expression in curly braces {...}, as in
<sql:query connection="$sql.conn" table="table_name" column="Product_ID" row-tag="row" column-tag="col" where="Product_ID="{ProductItem/ProductItemId}""/>

Mule datamapper: Mapping a value to an attribute of right hand side

I have an xml like:
<name>Mule</name>
<company>mulesoft</company>
How can I map using datamapper to get result like:
<details name="Mule" company="mulesoft" />
I tried but datamapper never shows the attributes to map.
I am using Mule 3.5.1
you need to either supply the schema or provide an example for both sides.
For example use the following as the sample source. DataMapper will generate the XSD based on the example file:
<enity>
<name>Mule</name>
<company>mulesoft</company>
</enity>
Right side do the same but use
<company>
<details name="Mule" company="mulesoft" />
</company>
Then click Create Mappings and you should see the mappings to your attributes.