从包含CDATA标记的SOAPUI中的XML响应中检索节点值

时间:2022-10-27 08:02:16

I am trying to retrieve the Atomic Number using Xpath from the below xml SoapUI response

我试图从下面的xml SoapUI响应中使用Xpath检索原子序号

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <GetAtomicNumberResponse xmlns="http://www.webserviceX.NET">
         <GetAtomicNumberResult><![CDATA[<NewDataSet>
  <Table>
    <AtomicNumber>47</AtomicNumber>
    <ElementName>Silver</ElementName>
    <Symbol>Ag</Symbol>
    <AtomicWeight>107.87</AtomicWeight>
    <BoilingPoint>2485</BoilingPoint>
    <IonisationPotential>7.58</IonisationPotential>
    <EletroNegativity>1.42</EletroNegativity>
    <AtomicRadius>1.34</AtomicRadius>
    <MeltingPoint>1235</MeltingPoint>
    <Density>10490</Density>
  </Table>
</NewDataSet>]]></GetAtomicNumberResult>
      </GetAtomicNumberResponse>
   </soap:Body>
</soap:Envelope>

have tried the below one

尝试了以下一个

declare namespace ns2='http://www.webserviceX.NET';
//ns2:GetAtomicNumberResponse[1]/ns2:GetAtomicNumberResult[1]

am getting all the data starting from [<NewDataset> .... </NewDataSet>], but I just need <AtomicNumber> value.

我从[ .... ]开始获取所有数据,但我只需要 值。

3 个解决方案

#1


3  

The problem here is that CDATA is treated as string inside an Xml and when working as a XPath; so if you want to access a node inside CDATA using XPath you need first to make the path to access CDATA and a way to parse its content as an Xml.

这里的问题是CDATA在Xml中被视为字符串,当作为XPath工作时;因此,如果要使用XPath访问CDATA内的节点,首先需要创建访问CDATA的路径以及将其内容解析为Xml的方法。

Hopefully SOAPUI use Saxon, and Saxon contains the follow function saxon:parse which allows to parse the string content returned from XPath as a Xml.

希望SOAPUI使用Saxon,而Saxon包含跟随函数saxon:parse,它允许将从XPath返回的字符串内容解析为Xml。

Note that Saxon version 9.3 replace this function for saxon:parse-xml however in SOAPUI is not accessible since it use saxon version 9.1.8.

请注意,Saxon版本9.3替换了saxon:parse-xml的此函数,但是由于它使用saxon版本9.1.8,因此无法访问SOAPUI。

So in your Property transfer testStep you can use the follow XPath:

因此,在您的Property transfer testStep中,您可以使用以下XPath:

declare namespace ns2='http://www.webserviceX.NET';
(saxon:parse(//ns2:GetAtomicNumberResponse[1]/ns2:GetAtomicNumberResult[1]))//*:AtomicNumber

Or in a shorter SOAPUI way:

或者以较短的SOAPUI方式:

(saxon:parse(//*:GetAtomicNumberResponse/*:GetAtomicNumberResult))//*:AtomicNumber

从包含CDATA标记的SOAPUI中的XML响应中检索节点值

#2


0  

def respXmlHolder = new XmlHolder(testRunner.testCase.testSteps["GetAtomicNumber"].testRequest.response.getContentAsXml());

respXmlHolder.namespaces["ns"] = "http://www.webserviceX.NET";

def CDATAXml = respXmlHolder.getNodeValue("//ns:GetAtomicNumberResponse/ns:GetAtomicNumberResult");

log.info CDATAXml;

def CDATAXmlHolder = new XmlHolder(CDATAXml);

def atomicNumber=CDATAXmlHolder.getNodeValue("//NewDataSet/Table/AtomicNumber");

com.eviware.soapui.support.UISupport.showInfoMessage("Atomic Number:-"+atomicNumber);

log.info "Atomic Number:-"+atomicNumber

#3


-3  

c#

C#

XDocument doc = XDocument.Load("XMLFile1.xml");
var result = doc.Descendants(XNamespace.Get("http://www.webserviceX.NET")+"AtomicNumber")
                .First();

#1


3  

The problem here is that CDATA is treated as string inside an Xml and when working as a XPath; so if you want to access a node inside CDATA using XPath you need first to make the path to access CDATA and a way to parse its content as an Xml.

这里的问题是CDATA在Xml中被视为字符串,当作为XPath工作时;因此,如果要使用XPath访问CDATA内的节点,首先需要创建访问CDATA的路径以及将其内容解析为Xml的方法。

Hopefully SOAPUI use Saxon, and Saxon contains the follow function saxon:parse which allows to parse the string content returned from XPath as a Xml.

希望SOAPUI使用Saxon,而Saxon包含跟随函数saxon:parse,它允许将从XPath返回的字符串内容解析为Xml。

Note that Saxon version 9.3 replace this function for saxon:parse-xml however in SOAPUI is not accessible since it use saxon version 9.1.8.

请注意,Saxon版本9.3替换了saxon:parse-xml的此函数,但是由于它使用saxon版本9.1.8,因此无法访问SOAPUI。

So in your Property transfer testStep you can use the follow XPath:

因此,在您的Property transfer testStep中,您可以使用以下XPath:

declare namespace ns2='http://www.webserviceX.NET';
(saxon:parse(//ns2:GetAtomicNumberResponse[1]/ns2:GetAtomicNumberResult[1]))//*:AtomicNumber

Or in a shorter SOAPUI way:

或者以较短的SOAPUI方式:

(saxon:parse(//*:GetAtomicNumberResponse/*:GetAtomicNumberResult))//*:AtomicNumber

从包含CDATA标记的SOAPUI中的XML响应中检索节点值

#2


0  

def respXmlHolder = new XmlHolder(testRunner.testCase.testSteps["GetAtomicNumber"].testRequest.response.getContentAsXml());

respXmlHolder.namespaces["ns"] = "http://www.webserviceX.NET";

def CDATAXml = respXmlHolder.getNodeValue("//ns:GetAtomicNumberResponse/ns:GetAtomicNumberResult");

log.info CDATAXml;

def CDATAXmlHolder = new XmlHolder(CDATAXml);

def atomicNumber=CDATAXmlHolder.getNodeValue("//NewDataSet/Table/AtomicNumber");

com.eviware.soapui.support.UISupport.showInfoMessage("Atomic Number:-"+atomicNumber);

log.info "Atomic Number:-"+atomicNumber

#3


-3  

c#

C#

XDocument doc = XDocument.Load("XMLFile1.xml");
var result = doc.Descendants(XNamespace.Get("http://www.webserviceX.NET")+"AtomicNumber")
                .First();