Extract list of objects from WSDL XML with XPath - java

I have the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<DataSet xmlns="http://MRCS/">
<xs:schema id="Estacoes" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Estacoes" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="NomeEstacao" type="xs:string" minOccurs="0"/>
<xs:element name="CodEstacao" type="xs:string" minOccurs="0"/>
<xs:element name="Bacia" type="xs:string" minOccurs="0"/>
<xs:element name="SubBacia" type="xs:decimal" minOccurs="0"/>
<xs:element name="Operadora" type="xs:string" minOccurs="0"/>
<xs:element name="Responsavel" type="xs:string" minOccurs="0"/>
<xs:element name="Municipio-UF" type="xs:string" minOccurs="0"/>
<xs:element name="Latitude" type="xs:decimal" minOccurs="0"/>
<xs:element name="Longitude" type="xs:decimal" minOccurs="0"/>
<xs:element name="Altitude" type="xs:decimal" minOccurs="0"/>
<xs:element name="CodRio" type="xs:decimal" minOccurs="0"/>
<xs:element name="NomeRio" type="xs:string" minOccurs="0"/>
<xs:element name="Origem" type="xs:string" minOccurs="0"/>
<xs:element name="StatusEstacao" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<Estacoes xmlns="">
<Table diffgr:id="Table1" msdata:rowOrder="0">
<NomeEstacao>GUARAPUAVA - COLÉGIO AGRICOLA</NomeEstacao>
<CodEstacao>02551010</CodEstacao>
<Bacia>6</Bacia>
<SubBacia>65</SubBacia>
<Operadora>00067 - IAPAR - IAPAR</Operadora>
<Responsavel>00067 - IAPAR - IAPAR</Responsavel>
<Municipio-UF>GUARAPUAVA-PR</Municipio-UF>
<Latitude>-25.36470</Latitude>
<Longitude>-51.49690</Longitude>
<Altitude>1045.00</Altitude>
<CodRio>0</CodRio>
<NomeRio/>
<Origem>ANA/SIVAM</Origem>
<StatusEstacao>Ativo</StatusEstacao>
</Table>
</Estacoes>
</diffgr:diffgram>
</DataSet>
From which i want to extract the <Table> element. If i use xpath(/*) i can get the entire document, but i can't figure out a way to extract certain elements from it. Is it even possible to use XPath with WSDL XMLs?
EDIT: i am using PostgreSQL xpath function to query from a XML column

Instead of handling the quite complex issue with the various namespaces on that document you can select this Table element with the XPath expression
xpath(/*/*/*/Table)
It selects all Table elements on every fourth level.

Related

Eclipse Java - Jaxb2 plugin and WSDL

Hi I have to generate a WSDL. I have this xsd schema
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.ima.eu/xml/cattolica"
targetNamespace="http://www.ima.eu/xml/cattolica" elementFormDefault="qualified">
<xs:element name="CreateRequestInput">
<xs:complexType>
<xs:sequence>
<xs:element name="customer" type="xs:string" minOccurs="1"/>
<xs:element name="user" type="xs:string" minOccurs="1"/>
<xs:element name="company" type="xs:string" minOccurs="1"/>
<xs:element name="insuranceNumber" type="xs:string"/>
<xs:element name="number" type="xs:string"/>
<xs:element name="creationDate" type="xs:string" minOccurs="1"/>
<xs:element name="answer" type="xs:string"/>
<xs:element name="answerType" type="xs:string"/>
<xs:element name="description" type="xs:string" minOccurs="1"/>
<xs:element name="idquintuple" type="xs:string" minOccurs="1"/>
<xs:element name="status" type="xs:string"/>
<xs:element name="priority" type="xs:string"/>
<xs:element name="idOTRS" type="xs:string" minOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="CreateRequestOutput">
<xs:complexType>
<xs:sequence>
<xs:element name="CreateRequest" type="tns:CreateRequest"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="CreateRequest">
<xs:sequence>
<xs:element name="idCRM" type="xs:string"/>
<xs:element name="status" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Maven plugin Jaxb2 generate java objects correctly.
I did also soap config java file.
but the wsdl generated doesn't have xs:message part so with for example soap ui i cannot make request:
Why method CreateRequest is not generated in wsdl....
Thx
maybe there is a standard for naming the Responses and Requests?

How to specify a list of complexType in XML?

I have XML specified the following:
<xs:element name="getNewsResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="newsItem" type="tns:newsList"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="newsList">
<xs:list itemType="tns:news"/>
</xs:simpleType>
<xs:complexType name="news">
<xs:sequence>
<xs:element name="id" type="xs:string"/>
<xs:element name="date" type="xs:string"/>
<xs:element name="author" type="tns:author"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="shortDescription" type="xs:string"/>
<xs:element name="content" type="xs:string"/>
</xs:sequence>
</xs:complexType>
I would like to have a list of the news in my response. However when I would like to create Java object with jaxb2 the xml returns the folllowing error when I run mvn clean compile -X:
org.xml.sax.SAXParseException: cos-st-restricts.1.1: The type 'newsList' is atomic, so its {base type definition}, 'tns:news', must be an atomic simple type definition or a built-in primitive datatype.
How I should change my XML to be able to compile?
In addition to using the built-in list types, you can create new list types by derivation from existing atomic types. You cannot create list types from existing list types, nor from complex types.
https://www.w3.org/TR/xmlschema-0/#ListDt
This is from one of my working XSD, a user with multiple addresses:
<xs:complexType name="user">
<xs:sequence>
<xs:element name="addresses" type="tns:addressData" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
Note that addressData is a complexType.
I guess this is what you need:
<xs:element name="getNewsResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="newsItems" type="tns:news" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="news">
<xs:sequence>
<xs:element name="id" type="xs:string"/>
<xs:element name="date" type="xs:string"/>
<xs:element name="author" type="tns:author"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="shortDescription" type="xs:string"/>
<xs:element name="content" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://pro.com/balcao/xdto"
xmlns:tns="http://pro.com/balcao/xdto"
elementFormDefault="qualified">
<xs:element name="salvarCliente" >
<xs:complexType>
<xs:sequence>
<xs:element name="nome" type="xs:string" />
<xs:element name="telefone" type="xs:string" />
<xs:element name="provincia" type="xs:string" />
<xs:element name="municipio" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="listarClientes">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="clientes" type="tns:cliente" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="cliente">
<xs:sequence>
<xs:element name="nome" type="xs:string" />
<xs:element name="telefone" type="xs:string" />
<xs:element name="provincia" type="xs:string" />
<xs:element name="municipio" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>

Java XML Schema Validation elementFormDefault="qualified"

I have a XSD:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified"
elementFormDefault="qualified">
<xs:element name="Structure">
<xs:annotation>
<xs:documentation>Comment describing your root element</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" ref="Tag1"/>
<xs:element maxOccurs="unbounded" minOccurs="0" ref="Tag2"/>
<xs:element minOccurs="0" ref="Properties"/>
<xs:element minOccurs="0" ref="Tag3"/>
</xs:sequence>
<xs:attribute name="url"/>
</xs:complexType>
</xs:element>
<xs:element name="Tag1">
<xs:complexType>
<xs:attribute name="attr"/>
<xs:attribute name="attr2"/>
</xs:complexType>
</xs:element>
<xs:element name="Tag2">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element minOccurs="0" ref="Object"/>
<xs:element maxOccurs="unbounded" minOccurs="0" ref="Tag2"/>
</xs:sequence>
<xs:attribute name="filter"/>
</xs:complexType>
</xs:element>
<xs:element name="Object">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="tag35">
<xs:complexType>
<xs:sequence minOccurs="0">
<xs:element maxOccurs="unbounded" minOccurs="0" ref="Tag3"/>
</xs:sequence>
<xs:attribute name="attr4"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="attr48"/>
</xs:complexType>
</xs:element>
<xs:element name="element52">
<xs:annotation>
<xs:documentation>Text54</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" ref="el58"/>
</xs:sequence>
<xs:attribute name="vector"/>
</xs:complexType>
</xs:element>
<xs:element name="el66" type="el66Type"/>
<xs:element name="el58">
<xs:complexType mixed="true">
<xs:attribute name="ID" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="Tag3">
<xs:complexType>
<xs:sequence maxOccurs="unbounded" minOccurs="0">
<xs:element minOccurs="0" name="MetaProperty">
<xs:complexType>
<xs:sequence minOccurs="0">
<xs:any/>
</xs:sequence>
<xs:attribute name="name"/>
<xs:attribute name="value"/>
</xs:complexType>
</xs:element>
<xs:element minOccurs="0" ref="Property"/>
</xs:sequence>
<xs:attribute name="ID"/>
<xs:attribute name="language" use="optional"/>
</xs:complexType>
</xs:element>
<xs:element name="Property">
<xs:complexType>
<xs:sequence minOccurs="0">
<xs:any minOccurs="0" processContents="skip"/>
</xs:sequence>
<xs:attribute name="name"/>
<xs:attribute name="type"/>
</xs:complexType>
</xs:element>
<xs:element name="Properties">
<xs:complexType>
<xs:sequence maxOccurs="unbounded" minOccurs="0">
<xs:element minOccurs="0" ref="Property"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType mixed="true" name="el66Type">
<xs:attribute name="ID" type="xs:string"/>
</xs:complexType>
</xs:schema>
And an xml:
<Structure url="/Test/url">
<Tag1/>
<Tag2>
<Object>
<tag35/>
</Object>
</Tag2>
<Properties>
<Property name="bla" type="value1"/>
<Property name="bla2" type="value2"/>
</Properties>
</Structure>
I am doing a validation, and I am getting the following error:
org.xml.sax.SAXParseException; cvc-complex-type.2.4.a: Invalid content was found starting with element 'Tag2'. One of '{Tag1, Tag2, Tag3}' is expected.
I checked in previous questions and always the solution is related with elementFormDefault="qualified" but now it is not the case so, could it be possible that there is a bug there? Any clue, will be grateful.
Your XML is valid against your XSD as posted in your question.
The error message in your question would not be returned. It must have been from different XML or XSD documents.
As Michael Kay states in the comments:
elementFormDefault only affects local elements declarations.
elementFormDefault only comes into play when the XSD has a targetNamespace.
For more details about elementFormDefault see this answer.

Don't display the element in xml if its id attribute is null

I have a xsd where there are 2 complextype elements under <Customer> complextype. One is <NormalCustomer> and <PrivilegedCustomer>. In my xml, I want either Normal or Privileged customer to be present under <Customer> tag based on the id attribute of Normal/Privileged customers.
below is my xsd
<xs:choice>
<xs:element name="normalCustomer" type="tns:normalCustomer" minOccurs="0" nillable="true"/>
<xs:element name="privilegedcustomer" type="tns:privilegedcustomer" minOccurs="0" nillable="true"/>
</xs:choice>
Complextype NormalCustomer
<xs:complexType name="normalCustomer">
<xs:sequence>
<xs:element name="id" type="xs:long"/>
<xs:element name="customerName" type="xs:string" minOccurs="1"/>
<xs:element name="customerType" type="xs:string" minOccurs="1"/>
</xs:sequence>
</xs:complexType>
Complextype PrivilegedCustomer
<xs:complexType name="privilegedCustomer">
<xs:sequence>
<xs:element name="id" type="xs:long"/>
<xs:element name="customerName" type="xs:string" minOccurs="1"/>
<xs:element name="customerType" type="xs:string" minOccurs="1"/>
</xs:sequence>
</xs:complexType>
Note: Am using JAXB for processing
Please suggest me how can I modify the tag to achieve my requirement.
Remove minOccurs="0" nillable="true" from both elements. The <xs:choice> enforces that exactly one of the elements are present. If none present are allowed, add minOccurs="0" to <xs:choice>.

Error in creating classes using JAXB

I'm using JAXB for the first time and trying to create class files using my xsd files but unfortunately getting errors.
This is my xsd :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="DbOperation" type="DbOperation"/>
<xs:complexType name="DbOperation">
<xs:sequence>
<xs:element name="ruleList" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element ref="Rule" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Rule">
<xs:sequence>
<xs:element name="Action" type="xs:string" minOccurs="0"/>
<xs:element name="TableName" type="xs:string" minOccurs="0"/>
<xs:element name="conditionList" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element ref="Conditions" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Conditions">
<xs:sequence>
<xs:element name="Condition" type="Condition" minOccurs="0"/>
<xs:element name="Operator" type="xs:string" minOccurs="0"/>
<xs:element name="Condition" type="Condition" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Condition">
<xs:sequence>
<xs:element name="fieldName" type="xs:string" minOccurs="0"/>
<xs:element name="Operation" type="xs:string" minOccurs="0"/>
<xs:element name="Value" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
And these are the errors I'm getting :
parsing a schema...
[ERROR] src-resolve: Cannot resolve the name 'Rule' to a(n) 'element declaration
' component.
line 11 of file:/C:/Documents%20and%20Settings/priyanka.jain/Desktop/JAXB/Rule
.xsd
[ERROR] src-resolve: Cannot resolve the name 'Conditions' to a(n) 'element decla
ration' component.
line 25 of file:/C:/Documents%20and%20Settings/priyanka.jain/Desktop/JAXB/Rule
.xsd
[ERROR] cos-nonambig: Condition and Condition (or elements from their substituti
on group) violate "Unique Particle Attribution". During validation against this
schema, ambiguity would be created for those two particles.
line 32 of file:/C:/Documents%20and%20Settings/priyanka.jain/Desktop/JAXB/Rule
.xsd
Failed to parse a schema.
This is the sample xml for which I've created xsd:
<?xml version="1.0" encoding="UTF-8"?>
<DbOperation>
<Rule>
<Action>Delete</Action>
<TableName>
issue
</TableName>
<Conditions>
<Condition>
<fieldName>
trader
</fieldName>
<Operation>
>=
</Operation>
<Value>
250
</Value>
</Condition>
<Operator>AND</Operator>
<Condition>
<fieldName>
traderName
</fieldName>
<Operation>
=
</Operation>
<Value>
jk
</Value>
</Condition>
</Conditions>
</Rule>
</DbOperation>
Use an XML editor to validate your schema first...
You are referencing a type as if it were an element
<xs:element ref="Conditions" minOccurs="0" maxOccurs="unbounded"/>
<xs:complexType name="Conditions">
<xs:sequence> ....
<xs:element ref="Rule" minOccurs="0" maxOccurs="unbounded"/>
The "REF" attribute indicates that the element and not the type.
Error can be corrected in two ways:
first:
<xs:complexType name="DbOperation">
<xs:sequence>
<xs:element name="ruleList" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="Rule" type="Rule" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
second:
<xs:complexType name="DbOperation">
<xs:sequence>
<xs:element name="ruleList" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element ref="Rule" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:element name="Rule" type="Rule"/>

Categories