How to validate the xml content against xsd some attributes to search space exist in value

StackOverflow https://stackoverflow.com/questions/23566082

  •  19-07-2023
  •  | 
  •  

Question

Below is my xml

<root>
<word>
 <string ID="S1" CONTENT="50 00"/>
</word>
</root>

I need to validate the CONTENT inside space should not come, If it comes It should through the error.

Any pattern match can we use in XSD. Any reference please.

Here is the XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="word"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="word">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="string">
    <xs:complexType>
      <xs:attribute name="CONTENT" use="required">
        <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:pattern value="^\s*\S+\s*"></xs:pattern>
                </xs:restriction>
            </xs:simpleType>
      </xs:attribute>
      <xs:attribute name="ID" use="required" type="xs:NCName"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

Thanks Advance... Umesh

Was it helpful?

Solution

Either of the patterns \S*, [^\s]* should do fine for you. You don't need, and should not supply, ^ and $ to anchor the regular expression; XSD regular expressions are always anchored, that is to say they must always describe the entire string. You can use the whitespace facet to collapse space before pattern matching, if you want to allow leading and trailing whitespace, or you can use the pattern \s*\S*\s*.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top