开发者

XML DOMDocument method selectSingleSpace failing on carriage returns in tags

I'm trying to select a node in an xml file which contains a known string, but also contains a potentially variable number of whitespace characters such as spaces and carriage returns. Is there a way to. When I try a line like:

Set objXML = CreateObject("MSXML2.DOMDocument.4.0")

Set objNode = objXML.documentElement.selectSingleNode("//Main/Subgroup/MyTag[Label='SomeText']")

It works with this:

<?xml version="1.0" encoding="UTF-16" ?> 
  <Main>
    <Subgroup>
      <MyTag>
        <Label>SomeText</Label>
      </MyTag>
    </Subgroup>
  </Main>

But it returns null with this:

<?xml version="1.0" encoding="UTF-16" ?> 
  <Main>
    <Subgroup>
      <MyTag>
        <Label>
          SomeText
        </Label>
      </MyTag>
    </Subgroup>
  </Main>

Is there a way to format that Label text, with wildcard characters or something similar, or is that a flag to have it ignore whitespace? I found this preserveWhiteSpace member开发者_运维问答 variable for objXML, but setting it to false didn't seem to do anything.


if you're using XPath, you can use the contains(node, selectiontext) function, ie

Set objXML = CreateObject("MSXML2.DOMDocument.4.0")
objXML.setProperty "SelectionLanguage", "XPath"
objXML.load "file.xml"
Set objNode = objXML.documentElement.selectSingleNode("//Main/Subgroup/MyTag[contains(Label, 'SomeText')]")

although this will also match any string that incorporates 'SomeText'. Another approach would be to use normalize-space(node):

Set objNode = objXML.documentElement.selectSingleNode("//Main/Subgroup/MyTag[normalize-space(Label) = 'SomeText']")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜