检查值中带撇号的节点

时间:2021-10-06 22:30:17

I have a set of code that searches for a person of a particular height that the user selects in a combo box, after which removes all the subjects that don't match in a list. The values for the combo box are like this: 5'0-5'5. My problem is the apostrophes in the 5'0-5'5 are throwing up errors. Here is my code

我有一组代码,用于搜索用户在组合框中选择的特定高度的人,之后删除列表中不匹配的所有主题。组合框的值如下:5'0-5'5。我的问题是5'0-5'5中的撇号都在抛出错误。这是我的代码

If ComboBox5.Value <> "" Then
    i = 0  

    Do While i <= ListBox26.ListCount - 1
        Set CheckHeight = objDom.SelectSingleNode("//IDNum/LastName[@LName = '" _
            & LastName.Text & "']/FirstName[@FName='" & FirstName.Text _
            & "']/DateOfBirth[@DOB='" & Dob.Text & "']/Height[.='" _
            & ComboBox5.Value & "']")

        If CheckHeight Is Nothing Then
            ListBox26.RemoveItem (i)
        Else
            i = i + 1
        End If
    Loop

The XML looks like this:

XML看起来像这样:

<LastName LName="Rodriguez Jesus Luis">
  <FirstName FName="Armondo">
    <DateOfBirth DOB="7/10/1975">
       <Hair>Black</Hair> 
       <Eyes>Brown</Eyes> 
       <Weight>150 - 175 lbs</Weight> 
       <Height>5'6 - 5'9</Height> 
    </DateOfBirth>
  </FirstName>
</LastName>

I have tried replacing one apostrophe "'" with two "''", tried Chr(39), tried &apos; but still errors.

我试过用两个“'”替换一个撇号“'”,试过Chr(39),试过'但仍然是错误。

I can get it to work if I use getElementsByTagName and write a bunch more code to compare and remove, but I was hopping to use the above code as it works with other all the other fields I am using.

如果我使用getElementsByTagName并编写更多代码来比较和删除,我可以让它工作,但我正在跳跃使用上面的代码,因为它适用于我正在使用的其他所有其他字段。

The error I am getting is:

我得到的错误是:

Expected token ']' found 'NUMBER'.

预期令牌']'找到'NUMBER'。

//IDNum/LastName[@LName = 'Rodriguez Jesus Luis']/FirstName[@FName='Armondo']/DateOfBirth[@DOB='7/10/1975']/Height[.='5'-->0<-- - 5'3']

// IDNum / LastName [@LName ='Rodriguez Jesus Luis'] / FirstName [@FName ='Armondo'] / DateOfBirth [@ DOB ='7/10 / 1975'] /高度[。='5' - > 0 < - - 5'3']

1 个解决方案

#1


Use double quotes around the string that may contain single quotes. In VBScript nested double quotes in strings are escaped by doubling them:

在可能包含单引号的字符串周围使用双引号。在VBScript中,字符串中的嵌套双引号通过加倍来转义:

Set CheckHeight = objDom.SelectSingleNode("//IDNum/LastName[@LName = '" & _
  LastName.Text & "']/FirstName[@FName='" & FirstName.Text & _
  "']/DateOfBirth[@DOB='" & Dob.Text & _
  "']/Height[.=""" & ComboBox5.Value & """]")
               ^^                       ^^

As a side note, your data format is seriously messed up. Your entire hierarchy should rather be attributes of the same node:

作为旁注,您的数据格式严重混乱。您的整个层次结构应该是同一节点的属性:

<Person LastName="Rodriguez Jesus Luis"
    FirstName="Armondo"
    DateOfBirth="7/10/1975"
    Hair="Black"
    Eyes="Brown"
    Weight="150 - 175 lbs"
    Height="5'6 - 5'9"
/>

#1


Use double quotes around the string that may contain single quotes. In VBScript nested double quotes in strings are escaped by doubling them:

在可能包含单引号的字符串周围使用双引号。在VBScript中,字符串中的嵌套双引号通过加倍来转义:

Set CheckHeight = objDom.SelectSingleNode("//IDNum/LastName[@LName = '" & _
  LastName.Text & "']/FirstName[@FName='" & FirstName.Text & _
  "']/DateOfBirth[@DOB='" & Dob.Text & _
  "']/Height[.=""" & ComboBox5.Value & """]")
               ^^                       ^^

As a side note, your data format is seriously messed up. Your entire hierarchy should rather be attributes of the same node:

作为旁注,您的数据格式严重混乱。您的整个层次结构应该是同一节点的属性:

<Person LastName="Rodriguez Jesus Luis"
    FirstName="Armondo"
    DateOfBirth="7/10/1975"
    Hair="Black"
    Eyes="Brown"
    Weight="150 - 175 lbs"
    Height="5'6 - 5'9"
/>