XML validation problem
I am trying to validate XML against XSD using the "xmllint" Unix command. The problem I'm running into is this:
In the XSD, the " state
" field is specified as follows:
<xs:element name="state">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="2"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
pay attention to <xs:maxLength value="2"/>
And in XML, the status field appears like this:
TYPE 1:
<state>
FL
</state>
OR
TYPE 2:
<state>FL</state>
For TYPE 1 I am getting the following error:
test.xml:243: element state: Schemas validity error : Element 'state': [facet 'maxLength'] The value has a length of '32'; this exceeds the allowed maximum length of '2'.
test.xml:243: element state: Schemas validity error : Element 'state': '
FL
' is not a valid value of the local atomic type.
And for TYPE 2, it validates correctly without errors.
So, mostly white spaces in formatted XML create problems. I want XML to be passed for both cases. Is there a way to do this with or without xmllint?
Many thanks.
+3
source to share
1 answer
Try this schema, QTAssistant validates your XML, I'm sure your validator should too:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="state">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
<xs:maxLength value="2"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>
The trick is the whiteSpace proposal.
+4
source to share