I have and XML snippet like this. I need to do an XSD validation such that, for example: if the field attribute 'name' is port, then it has to match a regex for whether it is a number (like '\p{Nd}+')
<Data>
<Attributes>
<Field name="nodeType">abcd</Field>
<Field name="port">5462</Field>
</Attributes>
</Data>
In my XSD , I'm using an assert as shown below
<xs:element name="Field" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="name" use="optional"/>
<xs:assert test="if(#name='port') then matches($value,'\p{Nd}+') else false())"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
But, when I run, I am getting the following Exception thrown
xml is NOT valid reason:org.xml.sax.SAXParseException;cvc-xpath.3.13.4.2a: XPST0003 - Assertion XPath expression ('if(#name='port') then matches($value,'\p{Nd}+') else false())') on the schema type '#AnonType_FieldAttributesDataContainerRtms' couldn't compile successfully
You've got a stray closing bracket:
false())
^
Saxon error message for comparison:
Error at xs:assert on line 7 column 102 of test.xsd:
Unexpected token ")" beyond end of expression
Related
Here is the validation.xml
<constraint-mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.1.xsd"
xmlns="http://jboss.org/xml/ns/javax/validation/mapping" version="1.1">
<default-package>com.esq.rbac.model</default-package>
<bean class="Tenant" ignore-annotations="true">
...
<field name="ivrPin">
<constraint annotation="javax.validation.constraints.Size">
<element name="min">6</element>
<element name="max">6</element>
<message>IVR Pin must be 6 digit numeric</message>
</constraint>
<constraint annotation="javax.validation.constraints.Pattern">
<element name="regexp">[0-9]+</element>
</constraint>
</field>
</bean>
During execution getting error saying
c.e.u.imports.Main [run] - HV000115: Error parsing mapping file.
javax.validation.ValidationException: HV000115: Error parsing mapping file.
Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.a:
Invalid content was found starting with element 'message'. One of '{"http://jboss.org/xml/ns/javax/validation/mapping":element}' is expected.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
How to include the error message properly
You just need to make one change by placing your message directly after constraint annotation tag, before the element tags.
<bean class="Tenant" ignore-annotations="true">
...
<field name="ivrPin">
<constraint annotation="javax.validation.constraints.Size">
<message>IVR Pin must be 6 digit numeric</message>
<element name="min">6</element>
<element name="max">6</element>
</constraint>
The reason for this is as follows:
If you look at the xsd file for validation-mapping:
validation-mapping-1.1.xsd
<xs:complexType name="constraintType">
<xs:sequence>
<xs:element type="xs:string" name="message" minOccurs="0"/>
<xs:element type="map:groupsType"
name="groups"
minOccurs="0"/>
<xs:element type="map:payloadType"
name="payload"
minOccurs="0"/>
<xs:element type="map:elementType"
name="element"
maxOccurs="unbounded"
minOccurs="0"/>
</xs:sequence>
<xs:attribute type="xs:string" name="annotation" use="required"/>
</xs:complexType>
It is using xs:sequence tag which specifies that the child elements must appear in a sequence. Refer element sequence. Here message attribute comes before other attributes like groups, payload or element.
I am trying to construct an XML document using org.w3c.dom.Document to create some XML for another existing tool. The problem I am having is that the tool seems to have the XML namespaces in a strange way. How can I replicate this using the Java API's?
<ns1:myroot xmlns:ns1="http://foo.com/ns1/">
<ns2:bar xmlns:ns2="http://foo.com/xml/bar/">
<ns2:bar_thing>abc</ns2:bar_thing>
</ns2:bar>
<ns3:data xmlns:ns3="http://foo.com/xml/special-ns">
<!--These are not namespaced for some reason. If I use the ns3 prefix, or
use a default xmls="...", the tool fails to load the document, saying the
elements have invalid values.
-->
<a>Element without namespace</a>
<b>
<bi>1</bi>
<bii>2</bii>
</b>
</ns3:data>
</ns1:myroot>
I can build most of the document easily with createElementNS and setAttributeNS. However I can't get the ns3:data contents to be correct.
Trying to use the non-namespace createElement still left an xmlns="http://foo.com/xml/special-ns"> on my a and b elements, as did using createElementNS with an empty namespace, and obviously a non-empty namespace puts them in a namespace.
The schema for http://foo.com/xml/special-ns has a bunch of declarations like these below, not sure what the tns thing is about, but otherwise does not seem special (although I am not 100% sure the tool actually does anything with the XSD and I don't have access to the source).
<xs:schema version="1.0" targetNamespace="http://foo.com/xml/special-ns">
<!--Bunch of xs:element such as this-->
<xs:element name="data" type="tns:data" />
<!--types declared after-->
<xs:complexType name="data">
<xs:sequence>
<xs:element name="a" type="tns:aDataObj" minOccurs="0"/>
<xs:element name="b" type="tns:bDataObj" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="aDataObj">
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9 ]+" />
</xs:restriction>
</xs:simpleType>
<xs:complexType name="bDataObj">
<xs:sequence>
<xs:element name="bi" type="xs:string"/>
<xs:element name="bii" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
I am trying to generate JAXB model from the following xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://abc.com/mmm/trt" attributeFormDefault="unqualified"
elementFormDefault="qualified">
<xs:annotation>
<xs:documentation>Schema for Cache Refresh Event.
</xs:documentation>
</xs:annotation>
<xs:simpleType name="cacheCode">
<xs:restriction base="xs:string">
<xs:enumeration value="BUSINESS_RULE" />
<xs:enumeration value="USER" />
</xs:restriction>
</xs:simpleType>
<xs:element name="cacheRefreshEvent">
<xs:complexType>
<xs:sequence>
<xs:element name="cacheCode" type="cacheCode" minOccurs="1"
maxOccurs="1" />
<xs:element name="entityRefId" type="xs:string" minOccurs="0"
maxOccurs="20" />
<xs:element name="cacheRefreshDate" type="xs:dateTime"
minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
But, it is giving me the following error, which i got in Eclipse console
parsing a schema...
[ERROR] src-resolve.4.1: Error resolving component 'cacheCode'. It was detected that 'cacheCode' has no namespace, but components with no target namespace are not referenceable from schema document 'file:/F:/Sprint6/cache/src/main/resources/CacheRefreshEvent.xsd'. If 'cacheCode' is intended to have a namespace, perhaps a prefix needs to be provided. If it is intended that 'cacheCode' has no namespace, then an 'import' without a "namespace" attribute should be added to 'file:/F:/Sprint6/cache/src/main/resources/CacheRefreshEvent.xsd'.
line 18 of file:/F:/Sprint6/cache/src/main/resources/CacheRefreshEvent.xsd
Failed to parse a schema.
Please help.
Remove this extra namespace attribute targetNamespace="http://www.hcentive.com/mea/trr"
OR add this xmlns="http://www.hcentive.com/mea/trr"
Both solutions will work :)
For you reference targetnamespace-and-xmlns
I want to generate the xml file that holds its schema as well as the xml data contain using java,as per my knowledge it is possible in C# .NET.Is it possible in java???
My XML file should be look like as given below.
<transaction>
<xs:schema id="transaction" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="transaction" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="id">
<xs:complexType>
<xs:sequence>
<xs:element name="in" type="xs:string" minOccurs="0" />
<xs:element name="sn" type="xs:string" minOccurs="0" />
<xs:element name="book" type="xs:string" minOccurs="0" />
<xs:element name="author" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="data">
<xs:complexType>
<xs:sequence>
<xs:element name="dateTime" type="xs:dateTime" minOccurs="0" />
<xs:element name="key" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="productData">
<xs:complexType>
<xs:sequence>
<xs:element name="dateTime" type="xs:dateTime" minOccurs="0" />
<xs:element name="key" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<id>
<in>computer</in>
<sn>1234567</sn>
<book>JAVA</book>
<author>klen</author>
</id>
<data>
<dateTime>2011-06-24T17:08:36.3727674+05:30</dateTime>
<key>Err</key>
</data>
</transaction>
In my given example my xml file contain data as well as schema I need to generate this type of file from schema using java.
I can only create the xml part using jaxb and main part of my code is look like as
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT,true);
jaxbMarshaller.marshal(transaction, file);
jaxbMarshaller.marshal(transaction, System.out);
but I can not add the inline xml schema part with my xml file.
#jtahlborn ok I will try to dig it up thanks for your help.I have another question I heard about that stax is better than dom to xml write,so I want to use stax is it possible to set namespace and other thing. I have another question is it true that jaxb is only use to convert xml to xml schema(un marshaling) and xml schema to xml(marshaling) and if I need to write xml file then we need to use jaxb[DOM,STAX(stream based reading writing),SAX(stream only reading)] .
You would:
create a DOM document from your schema (e.g. parse the schema file)
create a new DOM document
add the root node to your new DOM document (e.g. "transaction")
append the schema document from step 1. as the first child of the "transaction" element
append the actual document data as subsequent children of the "transaction" element
Alternately, if you want to use JAXB to generate the "main" xml output, then you can:
populate jaxb models (created from the schema)
marshal the jaxb models to a DOM document
create a DOM document from your schema (e.g. parse the schema file)
insert the schema document from step 3. as the first child of the "transaction" element in your DOM document
(with a few jaxb config tricks, you could probably get your Transaction model to have an Element "schema" property, and then you could set that property from the parsed schema doc and marshal the whole model at one time)
I have a XML structure somewhat like this:
<root>
<a/>
<b/>
<b/>
<a/>
<a/>
</root>
My XSD looks like this:
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:choice maxOccurs="unbounded">
<xs:element ref="a"/>
<xs:element ref="b"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="a" type="xs:string" />
<xs:element name="b" type="xs:string" />
I.e. i have a random sequence of two different sub tags.
Using XMLBeans i get a Root object with access methods:
getAArray(), getBArray()
And here's my problem:
The tags are grouped by name and the original order (a,b,b,a,a) is lost.
But i need to know the order of those elements.
What is the best / easiest way to do that with XMLBeans?
Try
xml.selectPath("./*")
ok i got it.
the selectPath method of XmlObject returns an array of objects so it gives the sequence.