代码之家  ›  专栏  ›  技术社区  ›  user6248190

如何检查XML中的空白元素?

  •  2
  • user6248190  · 技术社区  · 7 年前

    <ClientRequest> 是否填充标记?

    <Response xmlns="http://Test/Types"> 
     <ClientRequest>
        <Name>TEST</Name>
        <Id><222/Id>
        <Parameters>
          <SID>123456</SID>
        </RequestParams>
        <StartDate>2017-10-13T23:00:01.000+01:00</StartDate>
        <EndDate>2017-10-14T22:59:59.000+01:00</EndDate>
        <URL></URL>
      </ClientRequest>
      <Install/>
      <Types/>
      <LR/>
      <Package/>
      <Services/>
      <Issues/>
      <Complaints/>
    </Response>
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   kjhughes    7 年前

    使用XML解析器或XPath(而不是regex)检查或解析XML。

    这个XPath,

    //*[not(text()) and not(*)]
    

    将选择所有具有 没有文本或元素子级 .

    //*[not(node())]
    

    将全选 空的

    请注意,您的XML格式不正确。以下是更正:

    <Response xmlns="http://Test/Types"> 
     <ClientRequest>
        <Name>TEST</Name>
        <Id>222</Id>
        <Parameters>
          <SID>123456</SID>
        </Parameters>
        <StartDate>2017-10-13T23:00:01.000+01:00</StartDate>
        <EndDate>2017-10-14T22:59:59.000+01:00</EndDate>
        <URL></URL>
      </ClientRequest>
      <Install/>
      <Types/>
      <LR/>
      <Package/>
      <Services/>
      <Issues/>
      <Complaints/>
    </Response>
    

    boolean() count() 返回此类填充元素存在的指示器或计数。