简体   繁体   中英

XSD Hide Parent Node When all Child Nodes are Empty

XSD

<xs:element name="Notes" minOccurs="0" maxOccurs="unbounded">
  <xs:complexType>
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
      <xs:element name="Note" minOccurs="0">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Subject" type="xs:string" minOccurs="0"/>
            <xs:element name="Note" type="xs:string" minOccurs="0"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>

Current Output XML when Subject and Note are empty:

<Notes>
   <Note/>
</Notes>

However when Subject and Note are empty, I don't want any output.

Difference between XSD and XSLT

XSD is used for validation.

  • Input : XML document
  • Output : Validation results

An XSD specifies the constraints by which an XML document is valid. The output of validation is either success or failure, and with failure, there are usually diagnostic messages explaining the constraint violations. Your current output XML would be valid according to your XSD element declarations, as would just <Notes/> . However, an empty document would not be valid because it would not be well-formed .

XSLT is used for transformation.

  • Input : XML document *
  • Output : XML document *

An XSLT transformation specifies a mapping from input XML to output XML. It uses XPath to select parts of the input so that they can conditionally be included and arranged in the output. The language of your question suggests that you may wish to use XSLT, not XSD, to achieve your results. XSLT transforms input to output based upon user-provided templates, not the constraints given in an XSD. **

* Output can alternatively be text; input for XSLT 2.0 alternatively can also be text.

** XSLT 2.0 processors can be schema-aware but offer no automated mapping facilities.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM