xml schema validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fayazmd
    New Member
    • Jul 2007
    • 41

    xml schema validation

    Hi,

    I am validating xml file against a schema. I am able to get errors if the xml tag value data type is not same as of schema. But i am not getting any errors if the tag value is null. I want to get an error whenever tag value is null where in schema it is specified as some data type.

    How to get error if the xml tag value is null by validating against schema?
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Please post the relevant parts of your schema and xml file.

    Comment

    • fayazmd
      New Member
      • Jul 2007
      • 41

      #3
      This is the Tag to which i have to validate. If the tag doesnt have any value in it, it should throw an error.
      <SourceSystem ></SourceSystem>

      For that i have written schema as

      <xs:element name="SourceSys tem" type"xs:string" minOccurs="1" nillable="false " />

      But my c# code is not throwing any error.

      Originally posted by jkmyoung
      Please post the relevant parts of your schema and xml file.

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        That particular tag is not null, it is the empty string; there is a subtle difference. I think you want instead:

        Code:
        <xs:element name="SourceSystem" minOccurs="1" nillable="false">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
          </xs:restriction>
        </xs:simpleType>
        </xs:element>
        --If it were null it would have an xsi:null attribute.

        Comment

        • fayazmd
          New Member
          • Jul 2007
          • 41

          #5
          Yes I understood,

          I want to impliment this code but i am getting error. The error message is

          {"Expected Schema root. Make sure that the root element is <schema> and the namespace is 'http://www.w3.org/2001/XMLSchema' for an XSD schema or 'urn:schemas-microsoft-com:xml-data' for an XDR schema. An error occurred at , (2, 2)." }

          my xsd first two lines are

          <?xml version="1.0"?>
          <xs:schema xmlns="http://www.w3.org/2001/XMLSchema">

          I am giving same namespace even i am getting error.

          Originally posted by jkmyoung
          That particular tag is not null, it is the empty string; there is a subtle difference. I think you want instead:

          Code:
          <xs:element name="SourceSystem" minOccurs="1" nillable="false">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:minLength value="1"/>
            </xs:restriction>
          </xs:simpleType>
          </xs:element>
          --If it were null it would have an xsi:null attribute.

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            Small fix, note the :xs
            [code=xml]
            <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
            [/code]

            Comment

            • fayazmd
              New Member
              • Jul 2007
              • 41

              #7
              Thanks a lot!
              It's working fine.
              But, one request
              It is throwing an error "The 'SourceSystem' element has an invalid value according to its data type."
              Which i feel is improper for my application, since data is empty but it is throwing dataype error.

              I should throw an error that the tag should not be empty or similar to this.

              Originally posted by jkmyoung
              Small fix, note the :xs
              [code=xml]
              <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
              [/code]

              Comment

              • jkmyoung
                Recognized Expert Top Contributor
                • Mar 2006
                • 2057

                #8
                That's really an issue for the data validator. The two possible solutions are: use a different validator. Write code changes to the validator.

                Comment

                • fayazmd
                  New Member
                  • Jul 2007
                  • 41

                  #9
                  Thank you once again.

                  i am doing empty validation in c#.

                  Originally posted by jkmyoung
                  That's really an issue for the data validator. The two possible solutions are: use a different validator. Write code changes to the validator.

                  Comment

                  Working...