Complex XPaths using EclipseLink MOXy and JAXB - xpath

I am currently using EclipseLink MOXy 2.4.1 and trying to update an element's value via XPaths.
The input used is :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<expenseReport>
<user>
<userName>Sanaulla</userName>
</user>
<items>
<item>
<itemName>Seagate External HDD</itemName>
<purchasedOn>August 24, 2010</purchasedOn>
<amount>6776.5</amount>
</item>
<item>
<itemName>External HDD</itemName>
<purchasedOn>August 24, 2010</purchasedOn>
<amount>677336.5</amount>
</item>
</items>
</expenseReport>
The XSD is
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="expenseReport" type="ExpenseT"/>
<xs:complexType name="ExpenseT">
<xs:sequence>
<xs:element name="user" type="UserT"/>
<xs:element name="items" type="ItemListT"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="UserT">
<xs:sequence>
<xs:element name="userName" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ItemListT">
<xs:sequence>
<xs:element name="item" type="ItemT" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ItemT">
<xs:sequence>
<xs:element name="itemName" type="xs:string"/>
<xs:element name="purchasedOn" type="xs:string"/>
<xs:element name="amount" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
And my Java code is :
JAXBContext context = JAXBContext.newInstance("sample.jaxb.xsd");
JAXBElement<ExpenseT> element = (JAXBElement<ExpenseT>)unmarshaller.unmarshal(new File("resources/sample_before.xml"));
ExpenseT expenseReport = element.getValue();
context.setValueByXPath(expenseReport, "items/item[1]/amount/text()", null, "100.11");
/** Marshalling code **/
I have my java classes created from the XSD using Moxy's tool "jaxb-compiler.cmd".
As you can see from the above code, I am trying to set the amount of 1st item to "100.11". But it doesn't set the value, nor does it throw any exception.
I tried setting the username above using XPath like this
context.setValueByXPath(expenseReport, "user/userName/text()", null, "ABC");
And, it does set/update the value properly.
Can you please advise on how can I set values for complex XPaths with Predicates? Something like this :
items/item[itemName='Seagate External HDD']/amount

We recently fixed a bug in our EclipseLink 2.4.2 and 2.5.0 streams related to setting values in which the XPath contains a postional indicator (ie "foo[1]/bar[2]/text()"). You can download a nightly build from the following link:
http://www.eclipse.org/eclipselink/downloads/nightly.php
You can use predicates in mappings with #XmlPath (see: http://blog.bdoughan.com/2011/03/map-to-element-based-on-attribute-value.html), but we currently don't supports this with our setValueByXPath call. Could you open an enhancement

Related

What is the meaning of '/' in following TIBCO expression

I have following code in Tibco business works module
$ES_GetInfo/root/pfx4:GetInformationAndPropertyDetailsResponse/pfx4:LicenseInfo/pfx4:CoreEnt/pfx4:Ent
[pfx4:Ent/pfx4:EntOfferingCode = $Read_DB_Data/group/ROW/EOC]
/pfx4:EntState = "Disabled"
I can understand it is comparing "EntOfferingCode" with "EOC", but could not get the expression "/pfx4:EntState = 'Disabled'"?
As per the TIBCO, the whole expression returns a boolean value.
What is the meaning of "/pfx4:entState='Disabled'". Is logical or conditional or something else?
The whole expression is logical condition. The '/' is just xml schema elements separator in XPath (XML Path Language) syntax. You can start learning tibco xpath from here https://docs.tibco.com/pub/activematrix_businessworks/6.3.0/doc/html/GUID-D319018B-AA74-428D-A034-E477778AD2B6.html
The expression first filtering all the "Ent" nodes that have EntOfferingCode = $Read_DB_Data/group/ROW/EOC
then check if exists EntState = "Disabled" in filtered result
the expression can be replaced by
not (empty($Start/pfx:GetInformationAndPropertyDetailsResponse/pfx:LicenseInfo/pfx:CoreEnt/pfx:Ent[ pfx:EntOfferingCode= "EOC" and pfx:EntState = "Disabled"]))
For example
if the schema is like
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.tibco.com/schemas/TestProcess/Schema/Schema.xsd"
targetNamespace="http://www.tibco.com/schemas/TestProcess/Schema/Schema.xsd"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="GetInformationAndPropertyDetailsResponse">
<xs:complexType>
<xs:sequence>
<xs:element ref="LicenseInfo" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="LicenseInfo">
<xs:complexType>
<xs:sequence>
<xs:element ref="CoreEnt" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="CoreEnt">
<xs:complexType>
<xs:sequence>
<xs:element ref="Ent" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Ent">
<xs:complexType>
<xs:sequence>
<xs:element name="EntOfferingCode" type="xs:string"/>
<xs:element name="EntState" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Expression return true for:
<?xml version = "1.0" encoding = "UTF-8"?>
<GetInformationAndPropertyDetailsResponse xmlns = "http://www.tibco.com/schemas/TestProcess/Schema/Schema.xsd">
<LicenseInfo>
<CoreEnt>
<Ent>
<EntOfferingCode>EOC</EntOfferingCode>
<EntState>Disabled</EntState>
</Ent>
</CoreEnt>
</LicenseInfo>
</GetInformationAndPropertyDetailsResponse>
Expression return false for:
<?xml version = "1.0" encoding = "UTF-8"?>
<GetInformationAndPropertyDetailsResponse xmlns = "http://www.tibco.com/schemas/TestProcess/Schema/Schema.xsd">
<LicenseInfo>
<CoreEnt>
<Ent>
<EntOfferingCode>EOC</EntOfferingCode>
<EntState>Enabled</EntState>
</Ent>
<Ent>
<EntOfferingCode>EOC1</EntOfferingCode>
<EntState>Disabled</EntState>
</Ent>
</CoreEnt>
</LicenseInfo>
</GetInformationAndPropertyDetailsResponse>
if you just use
$ES_GetInfo/root/pfx4:GetInformationAndPropertyDetailsResponse/pfx4:LicenseInfo/pfx4:CoreEnt/pfx4:Ent/pfx4:EntState = "Disabled"
it will return true for both examples
If your question is what does the xslt expression mean, it is basically checking the following:
For all LicenseInfo/CoreEnt/Ent,
where Ent/EntOfferingCode = EOC value from the DB
check if the corresponding Enstate is Disabled or not
if Disabled, output true, else output false.

XSD for auto closing tag in complexType

I have this in my XML file:
<rooms>
<room id="1" beds="1" windows="0"/>
<room id="2" beds="2" windows="0"/>
</rooms>
And this in my XSD file:
<xs:complexType name="Rooms">
<xs:sequence>
<xs:element name="room" type="Room"/>
</xs:sequence>
<xs:attribute name="count" type="xs:integer"/>
</xs:complexType>
<xs:complexType name="Room">
<xs:attribute name="id" type="xs:integer"/>
<xs:attribute name="beds" type="xs:integer"/>
<xs:attribute name="windows" type="xs:integer"/>
</xs:complexType>
But I got this error : Element 'room': This element is not expected.
When I put <room id="1" beds="1" windows="0"></room> I don't have the error anymore and if I have only one room I don't have the error, so it's basically because of the auto closing tag.
How can I fix this?
Try specifying values for minOccurs and maxOccurs to your room element inside the sequence. I think the default expected number of occurrences is 1. Something like:
<xs:sequence>
<xs:element name="room" type="Room" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>

case-insensitive key in xsd

I have an XSD embedded into an XML like this:
<Replacements>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Replacements">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" ref="Replace" />
</xs:sequence>
</xs:complexType>
<xs:key name="ReplaceKey">
<xs:selector xpath="./Replace"/>
<xs:field xpath="#old"/>
</xs:key>
</xs:element>
<xs:element name="Replace">
<xs:complexType>
<xs:attribute name="old" type="AnythingButLowerCase" use="required" />
<xs:attribute name="new" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
<xs:simpleType name="AnythingButLowerCase">
<xs:restriction base="xs:string">
<xs:pattern value="[^a-z]+"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
<Replace old="A1020____9" new="A1020"/>
<Replace old="a1020____9" new="A1020"/>
</Replacements>
I've used xs:key to define a unique-key on "old" attribute of Replace elements.
my problem is I want this key to be CASE-INSENSITIVE.
I've read so many documents indicating I can use xsd functions like upper-case or translate to solve this, but if I write something like
<xs:field xpath="upper-case(#old)"/>
VS2010 gives me a warning like this:
'upper-case(#old)' is an invalid XPath for selector or field.
What is it I'm doing wrong?
Thanks :)
The correct XPath function is "upper-case", not "upper case". Just add in the hyphen.
OK!
I got that!
Using functions in an xpath for a xs:field is not allowed.
The workaround for what I was seeking is to define a simpleType and put a restriction there not allowing lower-case letters.
Something like this:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<Replacements>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Replacements">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" ref="Replace" />
</xs:sequence>
</xs:complexType>
<xs:key name="ReplaceKey">
<xs:selector xpath="./Replace"/>
<xs:field xpath="#old"/>
</xs:key>
</xs:element>
<xs:element name="Replace">
<xs:complexType>
<xs:attribute name="old" type="AnythingButLowerCase" use="required" />
<xs:attribute name="new" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
<xs:simpleType name="AnythingButLowerCase">
<xs:restriction base="xs:string">
<xs:pattern value="[^a-z]+"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
<Replace old="A1020____9" new="A1020"/>
<Replace old="a1020____9" new="A1020"/>
</Replacements>
Now this serves as what I want it to.

Which one to use:Dataset or Datatable or Datareader

I have read articles on dataset, datatable and datareader, but still I am in confuse when to use what? Can anyone help me with examples to understand which one is proper in which context?
A DataTable is an object used to store column and row data for a single table.
Dim dt As New Data.DataTable
dt.Columns.Add("ColumnA", GetType(String))
dt.Columns.Add("ColumnB", GetType(Int32))
dt.Rows.Add("TestData1", 1)
dt.Rows.Add("TestData2", 2)
For Each dr As Data.DataRow In dt.Rows
Response.Write(String.Format("{0}, {1}", dr.Item(0), dr.Item(0)))
Next
A Datareader is an object used to read one row at a time from a database.
Using oConn As New Data.SqlClient.SqlConnection
Dim oCmd = oConn.CreateCommand
Dim oRead = oCmd.ExecuteReader
While oRead.Read
Response.Write(oRead.Item(0).ToString)
End While
End Using
A Dataset is a collection of DataTables. With a Dataset you can also store relationships and constraints between parent tables and child tables. You can essentially create an entire relational database in memory with a Dataset. Datasets can be either created with code or created using the dataset editor in Visual Studio. If you make it using Visual Studio (XSD file) the dataset becomes "typed", so you can refer to columns in compiled code by name instead of by index or literal.
Dim ds As New dsMain
Dim drParent = ds.ParentTable.AddParentTableRow("1")
Dim drChild = ds.ChildTable.AddChildTableRow(drParent, "Somedata")
Response.Write(drChild.ChildData & drChild.ParentTableRow.ParentId.ToString)
And the code for dsMain.XSD...
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="dsMain" targetNamespace="http://tempuri.org/dsMain.xsd" xmlns:mstns="http://tempuri.org/dsMain.xsd" xmlns="http://tempuri.org/dsMain.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop" attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:annotation>
<xs:appinfo source="urn:schemas-microsoft-com:xml-msdatasource">
<DataSource DefaultConnectionIndex="0" FunctionsComponentName="QueriesTableAdapter" Modifier="AutoLayout, AnsiClass, Class, Public" SchemaSerializationMode="IncludeSchema" xmlns="urn:schemas-microsoft-com:xml-msdatasource">
<Connections />
<Tables />
<Sources />
</DataSource>
</xs:appinfo>
</xs:annotation>
<xs:element name="dsMain" msdata:IsDataSet="true" msdata:UseCurrentLocale="true" msprop:Generator_UserDSName="dsMain" msprop:Generator_DataSetName="dsMain">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="ParentTable" msprop:Generator_TableClassName="ParentTableDataTable" msprop:Generator_TableVarName="tableParentTable" msprop:Generator_TablePropName="ParentTable" msprop:Generator_RowDeletingName="ParentTableRowDeleting" msprop:Generator_UserTableName="ParentTable" msprop:Generator_RowChangingName="ParentTableRowChanging" msprop:Generator_RowEvHandlerName="ParentTableRowChangeEventHandler" msprop:Generator_RowDeletedName="ParentTableRowDeleted" msprop:Generator_RowEvArgName="ParentTableRowChangeEvent" msprop:Generator_RowChangedName="ParentTableRowChanged" msprop:Generator_RowClassName="ParentTableRow">
<xs:complexType>
<xs:sequence>
<xs:element name="ParentId" msprop:Generator_ColumnVarNameInTable="columnParentId" msprop:Generator_ColumnPropNameInRow="ParentId" msprop:Generator_ColumnPropNameInTable="ParentIdColumn" msprop:Generator_UserColumnName="ParentId" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="ChildTable" msprop:Generator_TableClassName="ChildTableDataTable" msprop:Generator_TableVarName="tableChildTable" msprop:Generator_TablePropName="ChildTable" msprop:Generator_RowDeletingName="ChildTableRowDeleting" msprop:Generator_UserTableName="ChildTable" msprop:Generator_RowChangingName="ChildTableRowChanging" msprop:Generator_RowEvHandlerName="ChildTableRowChangeEventHandler" msprop:Generator_RowDeletedName="ChildTableRowDeleted" msprop:Generator_RowEvArgName="ChildTableRowChangeEvent" msprop:Generator_RowChangedName="ChildTableRowChanged" msprop:Generator_RowClassName="ChildTableRow">
<xs:complexType>
<xs:sequence>
<xs:element name="ParentId" msprop:Generator_ColumnVarNameInTable="columnParentId" msprop:Generator_ColumnPropNameInRow="ParentId" msprop:Generator_ColumnPropNameInTable="ParentIdColumn" msprop:Generator_UserColumnName="ParentId" type="xs:string" />
<xs:element name="ChildData" msprop:Generator_ColumnVarNameInTable="columnChildData" msprop:Generator_ColumnPropNameInRow="ChildData" msprop:Generator_ColumnPropNameInTable="ChildDataColumn" msprop:Generator_UserColumnName="ChildData" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:unique name="Constraint1" msdata:PrimaryKey="true">
<xs:selector xpath=".//mstns:ParentTable" />
<xs:field xpath="mstns:ParentId" />
</xs:unique>
<xs:unique name="ChildTable_Constraint1" msdata:ConstraintName="Constraint1" msdata:PrimaryKey="true">
<xs:selector xpath=".//mstns:ChildTable" />
<xs:field xpath="mstns:ParentId" />
</xs:unique>
<xs:keyref name="FK_ParentTable_ChildTable" refer="Constraint1" msprop:rel_Generator_UserChildTable="ChildTable" msprop:rel_Generator_ChildPropName="GetChildTableRows" msprop:rel_Generator_ParentPropName="ParentTableRow" msprop:rel_Generator_UserRelationName="FK_ParentTable_ChildTable" msprop:rel_Generator_RelationVarName="relationFK_ParentTable_ChildTable" msprop:rel_Generator_UserParentTable="ParentTable">
<xs:selector xpath=".//mstns:ChildTable" />
<xs:field xpath="mstns:ParentId" />
</xs:keyref>
</xs:element>
</xs:schema>
Hope this helps.

BizTalk generated XSD fails to validate the XML its derived from

I've got an XML response from another system, but no XSD, so I used the Create Schema option to generate one.
I then added the XSD to my BizTalk 2006 R2 project and set its "Input Instance Filename" property to the original XML message.
Tried the "Validate Instance" option and it FAILS ???, with a couple of errors like so ...
error BEC2004: The xsi:type attribute value 'http://www.w3.org/2001/XMLSchema:int' is not valid for the element 'http://www.aniteps.com/xml/schemas/awm/images4:NumberOfErrors', either because it is not a type validly derived from the type in the schema, or because it has xsi:type derivation blocked.
How the heck can the XML fail that was used to generate the XSD ?
The XML example I have is ...
<?xml version="1.0" encoding="utf-8"?>
<ImportIndexDocumentResponse
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.aniteps.com/xml/schemas/awm/images4">
<HasErrors>false</HasErrors>
<NumberOfErrors xsi:type="xsd:int">0</NumberOfErrors>
<ErrorDescription xsi:type="xsd:string">No exception ocurred.</ErrorDescription>
<ErrorNumber xsi:type="xsd:int">0</ErrorNumber>
<FailedItems>
<Item>
<OriginalDataString xsi:type="xsd:string" />
<ErrorDescription xsi:type="xsd:string" />
</Item>
</FailedItems>
<UniqueDocumentReferences>
<UniqueDocumentReference>FA40FE</UniqueDocumentReference>
<UniqueDocumentReference>U55922</UniqueDocumentReference>
</UniqueDocumentReferences>
</ImportIndexDocumentResponse>
And BizTalk / Visual Studio 2005 generates ...
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="unqualified"
elementFormDefault="qualified"
targetNamespace="http://www.aniteps.com/xml/schemas/awm/images4">
<xs:element name="ImportIndexDocumentResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="HasErrors" type="xs:boolean" />
<xs:element name="NumberOfErrors" type="xs:unsignedByte" />
<xs:element name="ErrorDescription" type="xs:string" />
<xs:element name="ErrorNumber" type="xs:unsignedByte" />
<xs:element name="FailedItems">
<xs:complexType>
<xs:sequence>
<xs:element name="Item">
<xs:complexType>
<xs:sequence>
<xs:element name="OriginalDataString" />
<xs:element name="ErrorDescription" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="UniqueDocumentReferences">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="UniqueDocumentReference" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xsd:schema>
The XSD generator is reasonably dumb, and didn't notice that the sample instance document contained type annotations (xsi:type attributes). So it decided that <ErrorDescription> should contain unsigned bytes and not signed integers, as the type annotation said.
What happens then is that the validator becomes confused because there are two distinct type definitions for the element, so who should it believe? You could try changing the schema so that the type definitions match the xsi:type overrides, that may help a bit.

Resources