在RelaxNG中将元素定义为非空 [英] Define an element as non-empty in RelaxNG
问题描述
我已经开始使用RelaxNG来指定XML消息模式,并开始使用PHP DOMDocument来验证和解析传入的消息,但是无法弄清楚如何定义文本节点,因此该文本节点不能为空.模式示例:
I've started using RelaxNG to specify XML message schemas, and using PHP DOMDocument to validate and parse incoming messages, but can't figure out how to define a text node so that it cannot be empty. Example schema:
<?xml version="1.0"?>
<element name="amhAPI" xmlns="http://relaxng.org/ns/structure/1.0">
<element name="auth">
<element name="validateUser">
<element name="username">
<text/>
</element>
<element name="password">
<text/>
</element>
</element>
</element>
</element>
但是, DOMDocument :: relaxNGValidate正在验证以下消息是否正确方法(因为Relaxng 匹配任意字符串 [包括一个空字符串) text
模式),并且等效于):
However, the message below is being validated as correct by the DOMDocument::relaxNGValidate method (since relaxng matches any arbitrary string [including an empty one] with the text
pattern) and is equivalent to ):
<?xml version="1.0"?>
<amhAPI>
<auth>
<validateUser>
<username/>
<password/>
</validateUser>
</auth>
</amhAPI>
因此,我必须对不应该为空的字段进行一堆检查和验证,如果验证者将其识别为非空元素,则可以将其删除.
Because of this, I have to add in a bunch of checks and validation for fields that are not supposed to be empty, which could be removed if the validator identified them as non-empty elements.
是否可以强制非空文本?
Is there a way to force non-empty text?
推荐答案
如果您的RELAX NG验证器支持XSD数据类型(大多数这样做),则可以使用正则表达式来完善文本内容的约束:
If your RELAX NG validator supports XSD data types (most do), then you can use regular expressions to refine the constraints for text content:
<?xml version="1.0"?>
<element name="amhAPI" xmlns="http://relaxng.org/ns/structure/1.0"
datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
<element name="auth">
<element name="validateUser">
<element name="username">
<data type="string">
<param name="pattern">.+</param>
</data>
</element>
<element name="password">
<data type="string">
<param name="pattern">.+</param>
</data>
</element>
</element>
</element>
</element>
这篇关于在RelaxNG中将元素定义为非空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!