传递另一个节点值时获取节点的值

时间:2016-09-07 16:40:34

标签: xml vbscript

我有一个简单的XML,如下所示:

<RealTimeLetter>
  <Customer>
    <RTLtr_Bank>620</RTLtr_Bank>
    <RTLtr_Branch>214</RTLtr_Branch>
    <RTLtr_Loancust>0423436</RTLtr_Loancust>
    <RTLtr_NBR>1001</RTLtr_NBR>
    <RTLtr_LetterNumber>30092</RTLtr_LetterNumber>
    <RTLtr_CustomerAddress1>1234 qwtewret</RTLtr_CustomerAddress1>
    <RTLtr_CustomerAddress2 />
    <RTLtr_CustomerCity>WGEWHG</RTLtr_CustomerCity>
    <RTLtr_CustomerState>CO</RTLtr_CustomerState>
    <RTLtr_CustomerZip>12345</RTLtr_CustomerZip>
    <RTLtr_CoBorrowerName>PYTH SBUH</RTLtr_CoBorrowerName>
    <RTLtr_CustomerName>Alternate1</RTLtr_CustomerName>
    <RTLtr_CustomerSSN>888888888</RTLtr_CustomerSSN>
  </Customer>
  <Customer>
    <RTLtr_Bank>620</RTLtr_Bank>
    <RTLtr_Branch>214</RTLtr_Branch>
    <RTLtr_Loancust>0423437</RTLtr_Loancust>
    <RTLtr_NBR>1001</RTLtr_NBR>
    <RTLtr_LetterNumber>30092</RTLtr_LetterNumber>
    <RTLtr_CustomerAddress1>1234 qwtewret</RTLtr_CustomerAddress1>
    <RTLtr_CustomerAddress2 />
    <RTLtr_CustomerCity>WGEWHG</RTLtr_CustomerCity>
    <RTLtr_CustomerState>CO</RTLtr_CustomerState>
    <RTLtr_CustomerZip>12345</RTLtr_CustomerZip>
    <RTLtr_CoBorrowerName>PYTH SBUH</RTLtr_CoBorrowerName>
    <RTLtr_CustomerName>Alternate2</RTLtr_CustomerName>
    <RTLtr_CustomerSSN>888888888</RTLtr_CustomerSSN>
  </Customer>
</RealTimeLetter>

当我提供<RTLtr_CustomerName>代码的值时,我正在尝试检索<RTLtr_Loancust>代码的值。

我的脚本正在为客户名称返回Null

Dim oFS
Dim sFSpec
Dim objMSXML
Set oFS      = CreateObject("Scripting.FileSystemObject")
sFSpec       = oFS.GetAbsolutePathName("C:\Users\ecz560\Documents\CorrespondenceDocs\VBScript\SampleXMLFile.xml")
Set objMSXML = CreateObject("Msxml2.DOMDocument")
objMSXML.setProperty "SelectionLanguage", "XPath"
objMSXML.async = False
objMSXML.load sFSpec

If 0 = objMSXML.parseError Then
    Dim sXPath : sXPath = "RealTimeLetter/Customer[RTLtr_Loancust=""0423436""]"
    Dim ndlX : Set ndlX = objMSXML.selectNodes(sXPath)
    If 0 = ndlX.Length Then
        WScript.Echo sXPath, "failed"
    Else
        Dim ndX
        For Each ndX In ndlX
            WScript.Echo "CustomerName:", ndX.parentNode.GetAttribute("RTLtr_CustomerName")
        Next
    End If
Else
    WScript.Echo objMSXML.parseError.reason
End If

1 个答案:

答案 0 :(得分:1)

您的查询

"RealTimeLetter/Customer[RTLtr_Loancust=""0423436""]"

返回具有特定贷款/号码的客户。您对RTLtr_CustomerName感兴趣 - 一个节点,而不是一个属性。所以查询

"RealTimeLetter/Customer[RTLtr_Loancust=""0423436""]/RTLtr_CustomerName"

并使用返回的单个节点。在代码中:

...
    Dim sXPath : sXPath = "RealTimeLetter/Customer[RTLtr_Loancust=""0423436""]/RTLtr_CustomerName"
    Dim ndX : Set ndX = objMSXML.selectSingleNode(sXPath)
    If ndX Is Nothing Then
       WScript.Echo sXPath, "failed"
    Else
       WScript.Echo "CustomerName:", ndX.text
    End If
...

输出:

cscript 39375295.vbs
CustomerName: Alternate1