Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
611 views
in Technique[技术] by (71.8m points)

asp classic - Issue with XML / SelectNodes using ASP VBScript

Got an issue displaying information from XML. I think it has something to do with selecting the correct node (Company Name). Need a fresh opinion... got a feeling I am probably overlooking something really simple. Code as follows.

XML

<GovTalkMessage>
    <EnvelopeVersion>1.0</EnvelopeVersion>
    <Header>
        <MessageDetails>
            <Class>CompanyDetails</Class>
            <Qualifier>response</Qualifier>
            <TransactionID>9999999999999</TransactionID>
            <GatewayTest>TRUE</GatewayTest>
            <GatewayTimestamp>2013-09-24T17:51:41-00:00</GatewayTimestamp>
        </MessageDetails>
        <SenderDetails>
            <IDAuthentication>
                <SenderID>******</SenderID>
                <Authentication>
                    <Method>CHMD5</Method>
                    <Value></Value>
                </Authentication>
            </IDAuthentication>
            <EmailAddress>rte@rrfsolicitors.com</EmailAddress>
        </SenderDetails>
    </Header>
    <GovTalkDetails>
        <Keys/>
    </GovTalkDetails>
    <Body>
        <CompanyDetails>
            <CompanyName>MILLENNIUM STADIUM PLC</CompanyName>
            <CompanyNumber>03176906</CompanyNumber>
            <RegAddress>
                <AddressLine>MILLENNIUM STADIUM</AddressLine>
                <AddressLine>WESTGATE STREET</AddressLine>
                <AddressLine>CARDIFF</AddressLine>
                <AddressLine>CF10 1NS</AddressLine>
            </RegAddress>
        </CompanyDetails>
    </Body>
</GovTalkMessage>

ASP code:

Set XMLDom = CreateObject("MSXML2.DomDocument.6.0")
XMLDom.Async = False
XMLDom.LoadXML (theXML)

theNode = "/GovTalkMessage/Body/CompanyDetails"

Set NodeList = XMLDom.SelectNodes(theNode)
nodeCount = XMLDom.SelectNodes(theNode).Length
If XMLDom.ParseError = 0 Then
    Response.Write(nodeCount)
    For Each Node in NodeList 
        response.Write(Node.Text & "<br>")
    Next
Else
    response.Write("Error Parsing Results")
End If
Set XMLDom = Nothing

Results: nodeCount = 0

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your XML file uses namespaces. The node

<CompanyDetails xmlns="http://xmlgw.companieshouse.gov.uk/v1-0/schema"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://xmlgw.companieshouse.gov.uk/v1-0/schema
     http://xmlgw.companieshouse.gov.uk/v1-0/schema/CompanyDetails-v2-1.xsd">

defines a default namespace http://xmlgw.companieshouse.gov.uk/v1-0/schema. Unless a node is using an explicit namespace (e.g. <xsi:Something>) that default namespace is used, and you must define and use that default namespace in your code as well. Something like this should work:

uri = "http://xmlgw.companieshouse.gov.uk/v1-0/schema"
XMLDom.setProperty "SelectionNamespaces", "xmlns:ns='" & uri & "'"

theNode = "//ns:CompanyDetails"
Set NodeList = XMLDom.SelectNodes(theNode)
nodeCount = NodeList.Length

WScript.Echo nodeCount

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...