使用正则表达式通过XSD验证XML值

使用正则表达式通过XSD验证XML值,xml,xsd,xsd-validation,Xml,Xsd,Xsd Validation,我目前拥有以下格式的XML: <?xml version="1.0"?> <trace_data> <pinfo>1</pinfo> <traces> <P0>21:39:09.776762 clock_gettime(CLOCK_BOOTTIME, {68, 27131557}) = 0 </P0> <P1>21:39:09.776831 epoll_ctl(4, EPOLL

我目前拥有以下格式的XML:

<?xml version="1.0"?>
<trace_data>
  <pinfo>1</pinfo>
  <traces>
    <P0>21:39:09.776762 clock_gettime(CLOCK_BOOTTIME, {68, 27131557}) = 0
</P0>
    <P1>21:39:09.776831 epoll_ctl(4, EPOLL_CTL_DEL, 60, NULL) = 0
</P1>
    <P2>21:39:09.776856 close(60)               = 0
</P2>
  </traces>
</trace_data>

1.
21:39:09.776762时钟时间(时钟启动时间,{6827131557})=0
21:39:09.776831 epoll_ctl(4,epoll_ctl_DEL,60,NULL)=0
21:39:09.776856关闭(60)=0
其中,过程元素(P0、P1等)应形成多达n个过程的序列(P0…Pn)

我现在尝试创建一个XSD来验证这些XML。 另一个重要特征是,每个过程的值应以时间开始(例如21:39:09.123123)

我提出了以下XSD,但不知道如何验证流程元素的值

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://www.w3schools.com"
xmlns="https://www.w3schools.com"
elementFormDefault="qualified">


<xs:element name="trace_data">
  <xs:complexType>
    <xs:element name = "pinfo" type="xs:string"/>
    <xs:element name = "traces" type="Process"/>
  </xs:complexType>
</xs:element>


<!-- <THIS DESCRIBES P0 to Pn FOR WINDOWSIZE n> -->
<xs:complexType name="Process">
        <xs:sequence>
            <xs:any minOccurs="1" maxOccurs="unbounded" processContents="lax"/>
        </xs:sequence>
    <xs:assert test=" "
</xs:complexType> 
<!-- <PROCESS ENDS HERE> -->


</xs:schema> 


无论是谁设计了这种XML格式,都没有考虑到什么东西在XSD中工作得很好,或者是在其他XML工具中工作得很好。像这样使用结构化元素名称感觉像是一个聪明的想法,但实际上是一个彻底的麻烦

一种方法是将验证过程分为两步:首先将XML转换为更传统(且更易于处理)的格式:


时钟时间(时钟启动时间,{6827131557})=0
epoll-ctl(4,epoll-ctl-DEL,60,NULL)=0
...
然后对结果应用XSD,现在更简单了

尽管您仍然需要XSD 1.1断言来验证数字是否形成正确的序列:

<xsd:assert test="not(P[position() != @nr + 1])"/>

<xsd:assert test="not(P[position() != @nr + 1])"/>