0

I have an xml entity like this,

a:news-article xmlns:c="http://abc/core" xmlns:f="http://abc/fields" xmlns:a="http://abc/assets" xmlns:r="http://abc/refdata">
  <c:id>xyz</c:id>
  <c:type>asset</c:type>
  <c:created-on>2016-03-17T08:26:27.764Z</c:created-on>
  <c:released-on>1985-11-03T00:00:00Z</c:released-on>
  <c:expires-on>2009-12-12T00:00:00Z</c:expires-on>
  <f:short-headline>
    <c:content><c:l10n xml:lang="en">
    <p>
      Carbide technology for South Korean project
    </p>
      </c:l10n></c:content>
    <c:resources/>
  </f:short-headline>
</a:news-article>

In this XML, is a XHTML field. I need to validate such XHTML fields using schema validation. i.e. if i provided empty value then it should throw an schema validation error.

camden_kid
  • 12,591
  • 11
  • 52
  • 88

1 Answers1

1

You need separate schemas for each namespace.

In the XSD for the "http://abc/core" namespace you might want a pattern to check if the content of an element is non-empty:

    <xs:element name="id">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:pattern value="\S+"/>
                </xs:restriction>
            </xs:simpleType>
    </xs:element>

You then need to import that schema into your "root" schema (in your example I assume "a:" prefix represents the root schema) like this:

<xs:import namespace="http://abc/core"

schemaLocation="core.xsd"/>

and finally reference the element from a foreign namespace in the right location:

    <xs:element name="authors">
        <xs:complexType>
          <xs:sequence>news-article
            <xs:element ref="c:id"/>
            <xs:element ref="c:type"/>
            <!-- ... -->
          </xs:sequence>
        </xs:complexType>
     </xs:element>

If you want to make sure that the p element is non empty you need to write a schema of your own, following the same pattern as above - declare the non-empty pattern for p and refer to it in the schema for c:l10n element.

Lech Rzedzicki
  • 435
  • 6
  • 17