java - 我想
在 XML 中的 CDATA 中发送标签。哪个没有在 XSD 中得到验证
问题描述
我想
在 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=".+<br\s*>.+" />
</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 块中至少有一个字符。
推荐阅读
- system-verilog - 不可综合的时钟风格:只有条件操作或 if 语句在 sync-async always 块中被支持
- c++ - GLSL:CPU 共享原子负载
- c - C 在屏幕上显示字体图像素
- airflow - 典型方式通过气流或用户调用 azure databricks 应用程序
- c++ - 如何配置 libclang 以使用 Visual Studio 2019?
- spring - Spring将对象从视图发送到控制器
- java - @Modifying 注释即使没有属性“flushautomatically=true”也会刷新持久性上下文
- javascript - 数组字段的 Firebase 更新失败。说传递了未定义的数组,但是传递的数组没问题
- c++ - Qt QTextBrowser/QTextEdit:Tab 键更改项目符号/有序列表缩进
- gnuplot - 使用 gnuplot 制作比较图