我想在XML中的CDATA中发送标签。在XSD内部未得到验证。在XSD中使用序列。
我的XML是这样的。

<hotelnotes>
    <hotelnote><![CDATA[This is <br> Hotel Note <br> End of hotel note]]></hotelnote>
</hotelnotes>


XSD

  <xs:element name="hotelnotes">
      <xs:complexType>
        <xs:sequence>
          <xs:element type="xs:string" name="hotelnote" minOccurs="0"/>
        </xs:sequence>
      </xs:complexType>
  </xs:element>

最佳答案

如果要确保<br>标记在酒店注释中的文本内,则可以基于字符串类型使用带有模式限制的简单类型。

这是此类限制的示例:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="hotelnotes">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="hotelnote" minOccurs="0">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:pattern value=".+&lt;br\s*&gt;.+" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>


该文件将根据上面的XSD代码进行验证:

<?xml version='1.0' encoding='utf-8'?>
<hotelnotes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="../xsd/hotel_example.xsd">
    <hotelnote><![CDATA[This is <br> Hotel Note End of hotel note]]></hotelnote>
</hotelnotes>


而这不会因为不包含<br>标记而导致:

<?xml version='1.0' encoding='utf-8'?>
<hotelnotes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="../xsd/hotel_example.xsd">
    <hotelnote><![CDATA[This is Hotel Note End of hotel note]]></hotelnote>
</hotelnotes>


更新:

如果需要在CDATA中接受更通用的字符串,则可以使用以下XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="hotelnotes">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="hotelnote" minOccurs="0" >
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:pattern value=".+" /><!-- Enter here whichever regular expression which imposes a limitation on the string in CDATA -->
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>


上面的版本仅在CDATA块中需要至少一个字符。

09-30 17:22
查看更多