Is there any difference between 'valid xml' and 'well formed xml'?

前端 未结 14 2706
清酒与你
清酒与你 2020-11-21 04:43

I wasn\'t aware of a difference, but a coworker says there is, although he can\'t back it up. What\'s the difference if any?

14条回答
  •  野性不改
    2020-11-21 05:26

    Taken from Extensible Markup Language (XML) 1.0 (Fifth Edition) - W3C Recommendation 26 November 2008 :

    [Definition: A data object is an XML document if it is well-formed, as defined in this specification. In addition, the XML document is valid if it meets certain further constraints.]


    For those who prefer psuedo-code to paragraphs upon paragraphs of text... :)

    IF is_well_formed() THEN
        # It is well-formed, and can be parsed
        IF is_valid() THEN
            # Well-formed and ALSO valid. Hurray! 
            # **A valid XML doc, is a well-formed doc!**
        ELSE
            # Only well-formed, NOT valid
        END IF
    ELSE
        # Not well-formed, or valid!
    END IF
    
    FUNCTION is_well_formed
        IF  THEN
            RETURN TRUE
        ELSE 
            RETURN FALSE
        END IF
    END FUNCTION 
    
    FUNCTION is_valid
        IF  THEN
            # Standards used to validate XML could be a DTDs or XML Schemas, referenced within the XML document
            RETURN TRUE
        ELSE 
            RETURN FALSE
        END IF
    END FUNCTION
    

    Based on the theory: "Well Formed" vs. Valid

提交回复
热议问题