3 Replies Latest reply on Nov 11, 2019 1:30 PM by Raj__K

    XML Schema regular expression validation

    Raj__K

      Hi XML Experts,

       

      Please let me know whether a regular expression be used to validate the duplicate values in a xml element. The xml element can have comma seperated value from a list of values.

      If it is feasible, please share a pattern.

         

      Sample xsd   

          <schema targetNamespace="http://www.xyz.com/xyz"

              xmlns="http://www.w3.org/2001/XMLSchema"

              xmlns:x="http://www.xyz.com/xyz"

              elementFormDefault="qualified">

              <element name="CompanyDetails">

                  <complexType>

                      <sequence>

                          <element name="CountryList" type="x:CountryListType"/>

                      </sequence>

                  </complexType>

              </element>

              <simpleType name="CountryListType">

                  <restriction base="string">

                      <pattern value="France|Germany|India"/>

                  </restriction>

              </simpleType>

          </schema>   

         

      Sample xml

          <CompanyDetails>

              <CountryList>France,Germany,India</CountryList>

          </CompanyDetails>

       

      For example in the CountryList xml element if there is a duplicate entry then the validation need to fail.

          <CountryList>France,Germany,India,Germany</CountryList>

       

      Thank You!

        • 1. Re: XML Schema regular expression validation
          Martien van den Akker

          Hi KRaj,

           

          Nope. That cannot be done, as far as I know. These kinds of validations I would do using an XSLT. I'd create an XSD that with a list of faults. And then transform the message from your xsd to that "xmlvalidate.xsd". Use xsl:if's to check for these kinds of  exceptions. For a check like this you would need to a create recursive subtemplate that loops over different parts to see if the particular country exists in the remaining list. In every iteration you strip the first country from the list, check if it exists in the remaining list. If so, add a fault to the target xml.

          If the remaning list contains a comma (and thus still has multiple elements) have the sub-template call itself, with the remaining list. (If the remainder of the list does not contain a comma, then the remainder has only one element left, that does not need to be checked of course.)

           

          Kind regards,
          Martien

          1 person found this helpful
          • 2. Re: XML Schema regular expression validation
            cormaco

            It can not be done by using a xs:pattern restriction, but if your validator supports XSD 1.1 you can use xs:assertion to validate using an XQuery expression.

            Here is a pattern to match duplicates in a string:

            with countrylist(str) as (
                select 'France' from dual union all
                select 'France,France' from dual union all
                select 'France,Germany,India,Germany' from dual
            )
            select str from countrylist where regexp_like(str,'(^|,)(\w+)(,|$).*\2') 
            
            STR                         
            ----------------------------
            France,France
            France,Germany,India,Germany
            
            
            

             

            To validate strings with no duplicates, you assert the negation of the match of the pattern:

            <simpleType name="CountryListType">
            
                <restriction base="string">
                    <assertion test="not(matches($value,'(^|,)(\w+)(,|$).*\2'))" />
            
                </restriction>
            
            </simpleType>
            
            1 person found this helpful
            • 3. Re: XML Schema regular expression validation
              Raj__K

              Hi Martien van den Akker & cormaco,

              Thanks for yous response. True there are various way to achieve this, however I am curious to know whether this could be achieved in XSD using regular expression. Based on your response and my analysis, looks like it is not feasible.

               

              Regards!