2015-04-02 83 views
0

我需要在下面的XML中获取<a:DisplayValue>元素的值。XPath访问复杂XML中的元素

我尝试了使用XPath的不同方式,但是它显示出我没有使用正确的XPath,或者XML没有正确定义。

具体来说,我收到一条错误消息“带有前缀'a'的名称空间尚未声明”。

我使用的XPath是/s:Envelope/s:Body/a:Answers/a:FormAnswerData/a:QuestionName[1]

当我使用下面的XPath我收到 “不匹配” /S:信封/ S:身体/ GetRequestResponse/GetRequestResult/A:答案/ A:FormAnswerData/A :QuestionName [1]

下面是我正在使用的实际XML。

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Body> 
    <GetRequestResponse xmlns="http://intapp.com/Wilco.Service.Web/"> 
     <GetRequestResult xmlns:a="http://intapp.com/Wilco.Service.Web/Data/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
     <a:Answers> 
      <a:FormAnswerData> 
      <a:AddressAnswer i:nil="true" /> 
      <a:AnswerType>Text</a:AnswerType> 
      <a:BooleanAnswer i:nil="true" /> 
      <a:DataTableAnswer i:nil="true" /> 
      <a:DateAnswer i:nil="true" /> 
      <a:DisplayValue>912-345-9876</a:DisplayValue> 
      <a:NumericAnswer i:nil="true" /> 
      <a:QuestionId>14bfb3cb2e6-4-13a3cd74bcd</a:QuestionId> 
      <a:QuestionName>MobilePhone</a:QuestionName> 
      <a:RelatedPartiesAnswer i:nil="true" /> 
      <a:TextAnswer>912-345-9876</a:TextAnswer> 
      <a:TextListAnswer xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true" /> 
      </a:FormAnswerData> 
      <a:FormAnswerData> 
      <a:AddressAnswer i:nil="true" /> 
      <a:AnswerType>Text</a:AnswerType> 
      <a:BooleanAnswer i:nil="true" /> 
      <a:DataTableAnswer i:nil="true" /> 
      <a:DateAnswer i:nil="true" /> 
      <a:DisplayValue>202-346-9089</a:DisplayValue> 
      <a:NumericAnswer i:nil="true" /> 
      <a:QuestionId>14bfba640f0-a-25c218099f</a:QuestionId> 
      <a:QuestionName>HomePhone</a:QuestionName> 
      <a:RelatedPartiesAnswer i:nil="true" /> 
      <a:TextAnswer>202-346-9089</a:TextAnswer> 
      <a:TextListAnswer xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true" /> 
      </a:FormAnswerData> 
      <a:FormAnswerData> 
      <a:AddressAnswer i:nil="true" /> 
      <a:AnswerType>Text</a:AnswerType> 
      <a:BooleanAnswer i:nil="true" /> 
      <a:DataTableAnswer i:nil="true" /> 
      <a:DateAnswer i:nil="true" /> 
      <a:DisplayValue>Mr. T</a:DisplayValue> 
      <a:NumericAnswer i:nil="true" /> 
      <a:QuestionId>14bfbe22e0c-9-bedeb479e2</a:QuestionId> 
      <a:QuestionName>EmerContName</a:QuestionName> 
      <a:RelatedPartiesAnswer i:nil="true" /> 
      <a:TextAnswer>Mr. T</a:TextAnswer> 
      <a:TextListAnswer xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true" /> 
      </a:FormAnswerData> 
      <a:FormAnswerData> 
      <a:AddressAnswer i:nil="true" /> 
      <a:AnswerType>Text</a:AnswerType> 
      <a:BooleanAnswer i:nil="true" /> 
      <a:DataTableAnswer i:nil="true" /> 
      <a:DateAnswer i:nil="true" /> 
      <a:DisplayValue>Uncle</a:DisplayValue> 
      <a:NumericAnswer i:nil="true" /> 
      <a:QuestionId>14bfccc91eb-7-7a1b63329e</a:QuestionId> 
      <a:QuestionName>EmerContRel</a:QuestionName> 
      <a:RelatedPartiesAnswer i:nil="true" /> 
      <a:TextAnswer>Uncle</a:TextAnswer> 
      <a:TextListAnswer xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true" /> 
      </a:FormAnswerData> 
      <a:FormAnswerData> 
      <a:AddressAnswer i:nil="true" /> 
      <a:AnswerType>Text</a:AnswerType> 
      <a:BooleanAnswer i:nil="true" /> 
      <a:DataTableAnswer i:nil="true" /> 
      <a:DateAnswer i:nil="true" /> 
      <a:DisplayValue>321-467-9089</a:DisplayValue> 
      <a:NumericAnswer i:nil="true" /> 
      <a:QuestionId>14bfccd87c4-a-ac3b7a4685</a:QuestionId> 
      <a:QuestionName>EmerContHomePhone</a:QuestionName> 
      <a:RelatedPartiesAnswer i:nil="true" /> 
      <a:TextAnswer>321-467-9089</a:TextAnswer> 
      <a:TextListAnswer xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true" /> 
      </a:FormAnswerData> 
      <a:FormAnswerData> 
      <a:AddressAnswer i:nil="true" /> 
      <a:AnswerType>Text</a:AnswerType> 
      <a:BooleanAnswer i:nil="true" /> 
      <a:DataTableAnswer i:nil="true" /> 
      <a:DateAnswer i:nil="true" /> 
      <a:DisplayValue>123-789-0987</a:DisplayValue> 
      <a:NumericAnswer i:nil="true" /> 
      <a:QuestionId>14bfcceddeb-14-c8e035c212</a:QuestionId> 
      <a:QuestionName>EmerContMobilePhone</a:QuestionName> 
      <a:RelatedPartiesAnswer i:nil="true" /> 
      <a:TextAnswer>123-789-0987</a:TextAnswer> 
      <a:TextListAnswer xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true" /> 
      </a:FormAnswerData> 
      <a:FormAnswerData> 
      <a:AddressAnswer i:nil="true" /> 
      <a:AnswerType>TextList</a:AnswerType> 
      <a:BooleanAnswer i:nil="true" /> 
      <a:DataTableAnswer i:nil="true" /> 
      <a:DateAnswer i:nil="true" /> 
      <a:DisplayValue>Chinese - Spoken Level - Fair, Chinese - Written Level - Poor, Dutch - Written Level - Good</a:DisplayValue> 
      <a:NumericAnswer i:nil="true" /> 
      <a:QuestionId>14c16083084-a-bc76dff8ee</a:QuestionId> 
      <a:QuestionName>LangMDDId</a:QuestionName> 
      <a:RelatedPartiesAnswer i:nil="true" /> 
      <a:TextAnswer i:nil="true" /> 
      <a:TextListAnswer xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays"> 
       <b:string>4</b:string> 
       <b:string>7</b:string> 
       <b:string>27</b:string> 
      </a:TextListAnswer> 
      </a:FormAnswerData> 
      <a:FormAnswerData> 
      <a:AddressAnswer i:nil="true" /> 
      <a:AnswerType>Text</a:AnswerType> 
      <a:BooleanAnswer i:nil="true" /> 
      <a:DataTableAnswer i:nil="true" /> 
      <a:DateAnswer i:nil="true" /> 
      <a:DisplayValue i:nil="true" /> 
      <a:NumericAnswer i:nil="true" /> 
      <a:QuestionId>14c1b10eeda-10-c078be069f</a:QuestionId> 
      <a:QuestionName>NewLanguageAbility</a:QuestionName> 
      <a:RelatedPartiesAnswer i:nil="true" /> 
      <a:TextAnswer i:nil="true" /> 
      <a:TextListAnswer xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true" /> 
      </a:FormAnswerData> 
      <a:FormAnswerData> 
      <a:AddressAnswer i:nil="true" /> 
      <a:AnswerType>Text</a:AnswerType> 
      <a:BooleanAnswer i:nil="true" /> 
      <a:DataTableAnswer i:nil="true" /> 
      <a:DateAnswer i:nil="true" /> 
      <a:DisplayValue>1234 Main St</a:DisplayValue> 
      <a:NumericAnswer i:nil="true" /> 
      <a:QuestionId>14c1dfeb6c7-d-773963938c</a:QuestionId> 
      <a:QuestionName>StreetAddress</a:QuestionName> 
      <a:RelatedPartiesAnswer i:nil="true" /> 
      <a:TextAnswer>1234 Main St</a:TextAnswer> 
      <a:TextListAnswer xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true" /> 
      </a:FormAnswerData> 
      <a:FormAnswerData> 
      <a:AddressAnswer i:nil="true" /> 
      <a:AnswerType>Text</a:AnswerType> 
      <a:BooleanAnswer i:nil="true" /> 
      <a:DataTableAnswer i:nil="true" /> 
      <a:DateAnswer i:nil="true" /> 
      <a:DisplayValue>San Fran</a:DisplayValue> 
      <a:NumericAnswer i:nil="true" /> 
      <a:QuestionId>14c1e3d9368-12-94b02069f0</a:QuestionId> 
      <a:QuestionName>City</a:QuestionName> 
      <a:RelatedPartiesAnswer i:nil="true" /> 
      <a:TextAnswer>San Fran</a:TextAnswer> 
      <a:TextListAnswer xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true" /> 
      </a:FormAnswerData> 
      <a:FormAnswerData> 
      <a:AddressAnswer i:nil="true" /> 
      <a:AnswerType>Text</a:AnswerType> 
      <a:BooleanAnswer i:nil="true" /> 
      <a:DataTableAnswer i:nil="true" /> 
      <a:DateAnswer i:nil="true" /> 
      <a:DisplayValue>CA</a:DisplayValue> 
      <a:NumericAnswer i:nil="true" /> 
      <a:QuestionId>14c1e3dfbff-15-98ef4238d0</a:QuestionId> 
      <a:QuestionName>State</a:QuestionName> 
      <a:RelatedPartiesAnswer i:nil="true" /> 
      <a:TextAnswer>CA</a:TextAnswer> 
      <a:TextListAnswer xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true" /> 
      </a:FormAnswerData> 
      <a:FormAnswerData> 
      <a:AddressAnswer i:nil="true" /> 
      <a:AnswerType>Text</a:AnswerType> 
      <a:BooleanAnswer i:nil="true" /> 
      <a:DataTableAnswer i:nil="true" /> 
      <a:DateAnswer i:nil="true" /> 
      <a:DisplayValue>98567</a:DisplayValue> 
      <a:NumericAnswer i:nil="true" /> 
      <a:QuestionId>14c1e3e46b8-18-a8cc17d184</a:QuestionId> 
      <a:QuestionName>ZipCode</a:QuestionName> 
      <a:RelatedPartiesAnswer i:nil="true" /> 
      <a:TextAnswer>98567</a:TextAnswer> 
      <a:TextListAnswer xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true" /> 
      </a:FormAnswerData> 
      <a:FormAnswerData> 
      <a:AddressAnswer i:nil="true" /> 
      <a:AnswerType>Text</a:AnswerType> 
      <a:BooleanAnswer i:nil="true" /> 
      <a:DataTableAnswer i:nil="true" /> 
      <a:DateAnswer i:nil="true" /> 
      <a:DisplayValue>[email protected]</a:DisplayValue> 
      <a:NumericAnswer i:nil="true" /> 
      <a:QuestionId>14c1e3ed5de-1d-9620413541</a:QuestionId> 
      <a:QuestionName>PersonalEmail</a:QuestionName> 
      <a:RelatedPartiesAnswer i:nil="true" /> 
      <a:TextAnswer>[email protected]</a:TextAnswer> 
      <a:TextListAnswer xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true" /> 
      </a:FormAnswerData> 
      <a:FormAnswerData> 
      <a:AddressAnswer i:nil="true" /> 
      <a:AnswerType>Boolean</a:AnswerType> 
      <a:BooleanAnswer>true</a:BooleanAnswer> 
      <a:DataTableAnswer i:nil="true" /> 
      <a:DateAnswer i:nil="true" /> 
      <a:DisplayValue>Yes</a:DisplayValue> 
      <a:NumericAnswer i:nil="true" /> 
      <a:QuestionId>14c41d58550-24-13c37f2f719</a:QuestionId> 
      <a:QuestionName>Notary</a:QuestionName> 
      <a:RelatedPartiesAnswer i:nil="true" /> 
      <a:TextAnswer i:nil="true" /> 
      <a:TextListAnswer xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true" /> 
      </a:FormAnswerData> 
      <a:FormAnswerData> 
      <a:AddressAnswer i:nil="true" /> 
      <a:AnswerType>Boolean</a:AnswerType> 
      <a:BooleanAnswer>true</a:BooleanAnswer> 
      <a:DataTableAnswer i:nil="true" /> 
      <a:DateAnswer i:nil="true" /> 
      <a:DisplayValue>Yes</a:DisplayValue> 
      <a:NumericAnswer i:nil="true" /> 
      <a:QuestionId>14c41d51ae7-21-41cf999dcd</a:QuestionId> 
      <a:QuestionName>Government</a:QuestionName> 
      <a:RelatedPartiesAnswer i:nil="true" /> 
      <a:TextAnswer i:nil="true" /> 
      <a:TextListAnswer xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true" /> 
      </a:FormAnswerData> 
      <a:FormAnswerData> 
      <a:AddressAnswer i:nil="true" /> 
      <a:AnswerType>Text</a:AnswerType> 
      <a:BooleanAnswer i:nil="true" /> 
      <a:DataTableAnswer i:nil="true" /> 
      <a:DateAnswer i:nil="true" /> 
      <a:DisplayValue>111-222-3333</a:DisplayValue> 
      <a:NumericAnswer i:nil="true" /> 
      <a:QuestionId>14c1e3f5172-20-812001dba7</a:QuestionId> 
      <a:QuestionName>HomeFax</a:QuestionName> 
      <a:RelatedPartiesAnswer i:nil="true" /> 
      <a:TextAnswer>111-222-3333</a:TextAnswer> 
      <a:TextListAnswer xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true" /> 
      </a:FormAnswerData> 
      <a:FormAnswerData> 
      <a:AddressAnswer i:nil="true" /> 
      <a:AnswerType>Text</a:AnswerType> 
      <a:BooleanAnswer i:nil="true" /> 
      <a:DataTableAnswer i:nil="true" /> 
      <a:DateAnswer i:nil="true" /> 
      <a:DisplayValue>222-333-4444</a:DisplayValue> 
      <a:NumericAnswer i:nil="true" /> 
      <a:QuestionId>14c1e3fa12b-23-122dcff1fcd</a:QuestionId> 
      <a:QuestionName>CountryHomePhoneNumber</a:QuestionName> 
      <a:RelatedPartiesAnswer i:nil="true" /> 
      <a:TextAnswer>222-333-4444</a:TextAnswer> 
      <a:TextListAnswer xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true" /> 
      </a:FormAnswerData> 
      <a:FormAnswerData> 
      <a:AddressAnswer i:nil="true" /> 
      <a:AnswerType>Text</a:AnswerType> 
      <a:BooleanAnswer i:nil="true" /> 
      <a:DataTableAnswer i:nil="true" /> 
      <a:DateAnswer i:nil="true" /> 
      <a:DisplayValue>333-444-5555</a:DisplayValue> 
      <a:NumericAnswer i:nil="true" /> 
      <a:QuestionId>14c1e403301-26-460093938e</a:QuestionId> 
      <a:QuestionName>CountryHomeFaxNumber</a:QuestionName> 
      <a:RelatedPartiesAnswer i:nil="true" /> 
      <a:TextAnswer>333-444-5555</a:TextAnswer> 
      <a:TextListAnswer xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true" /> 
      </a:FormAnswerData> 
      <a:FormAnswerData> 
      <a:AddressAnswer i:nil="true" /> 
      <a:AnswerType>Text</a:AnswerType> 
      <a:BooleanAnswer i:nil="true" /> 
      <a:DataTableAnswer i:nil="true" /> 
      <a:DateAnswer i:nil="true" /> 
      <a:DisplayValue>January 1, 2000</a:DisplayValue> 
      <a:NumericAnswer i:nil="true" /> 
      <a:QuestionId>14c41d173f0-15-3d3a0c5447</a:QuestionId> 
      <a:QuestionName>DateOfAdmission</a:QuestionName> 
      <a:RelatedPartiesAnswer i:nil="true" /> 
      <a:TextAnswer>January 1, 2000</a:TextAnswer> 
      <a:TextListAnswer xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true" /> 
      </a:FormAnswerData> 
      <a:FormAnswerData> 
      <a:AddressAnswer i:nil="true" /> 
      <a:AnswerType>Text</a:AnswerType> 
      <a:BooleanAnswer i:nil="true" /> 
      <a:DataTableAnswer i:nil="true" /> 
      <a:DateAnswer i:nil="true" /> 
      <a:DisplayValue>NY, CA, NJ</a:DisplayValue> 
      <a:NumericAnswer i:nil="true" /> 
      <a:QuestionId>14c41d212de-18-bbf50ffa7e</a:QuestionId> 
      <a:QuestionName>BarAdmissions</a:QuestionName> 
      <a:RelatedPartiesAnswer i:nil="true" /> 
      <a:TextAnswer>NY, CA, NJ</a:TextAnswer> 
      <a:TextListAnswer xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true" /> 
      </a:FormAnswerData> 
      <a:FormAnswerData> 
      <a:AddressAnswer i:nil="true" /> 
      <a:AnswerType>Text</a:AnswerType> 
      <a:BooleanAnswer i:nil="true" /> 
      <a:DataTableAnswer i:nil="true" /> 
      <a:DateAnswer i:nil="true" /> 
      <a:DisplayValue>CA, MO, NV</a:DisplayValue> 
      <a:NumericAnswer i:nil="true" /> 
      <a:QuestionId>14c41d36d52-1b-48abe7ae3c</a:QuestionId> 
      <a:QuestionName>CourtAdmissions</a:QuestionName> 
      <a:RelatedPartiesAnswer i:nil="true" /> 
      <a:TextAnswer>CA, MO, NV</a:TextAnswer> 
      <a:TextListAnswer xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true" /> 
      </a:FormAnswerData> 
      <a:FormAnswerData> 
      <a:AddressAnswer i:nil="true" /> 
      <a:AnswerType>Text</a:AnswerType> 
      <a:BooleanAnswer i:nil="true" /> 
      <a:DataTableAnswer i:nil="true" /> 
      <a:DateAnswer i:nil="true" /> 
      <a:DisplayValue>cmt1, cmt2</a:DisplayValue> 
      <a:NumericAnswer i:nil="true" /> 
      <a:QuestionId>14c41d3d8c3-1e-e13c570278</a:QuestionId> 
      <a:QuestionName>Committees</a:QuestionName> 
      <a:RelatedPartiesAnswer i:nil="true" /> 
      <a:TextAnswer>cmt1, cmt2</a:TextAnswer> 
      <a:TextListAnswer xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:nil="true" /> 
      </a:FormAnswerData> 
     </a:Answers> 
     <a:ClientId i:nil="true" /> 
     <a:CreatedBy>Admin</a:CreatedBy> 
     <a:CurrentState>Save Employee Info</a:CurrentState> 
     <a:CustomFields xmlns:b="http://schemas.datacontract.org/2004/07/IntApp.Wilco.Service.Contracts.Data.Common.Entities" /> 
     <a:Department i:nil="true" /> 
     <a:Id>177</a:Id> 
     <a:MatterId i:nil="true" /> 
     <a:Name>Request #177</a:Name> 
     <a:Office i:nil="true" /> 
     <a:PracticeArea i:nil="true" /> 
     <a:RequestType>Update My Employee Info</a:RequestType> 
     <a:RequestedBy i:nil="true" /> 
     <a:Status>InProgress</a:Status> 
     </GetRequestResult> 
    </GetRequestResponse> 
    </s:Body> 
</s:Envelope> 
+2

显示您使用XPath成就的代码。此外,错误消息相当干净(并且您的问题非常常见)。发布您的问题之前,您是否尝试过寻找解决方案? – Tomalak 2015-04-02 16:16:22

+0

我没有代码。我在http://www.freeformatter.com/xpath-tester.html使用在线Xpath测试程序 – Sudheer 2015-04-02 16:30:17

回答

0

有可能问题:

  • 错误Namespace with prefix 'a' has not been declared意味着该命名空间xmlns:a="http://intapp.com/Wilco.Service.Web/Data/"在你的XPath上下文丢失了(是它的一个XSLT或别的东西)
  • 的XPath应该是/s:Envelope/s:Body//a:Answers/a:FormAnswerData/a:QuestionName[1]/s:Envelope/s:Body/GetRequestResponse/GetRequestResult/a:Answers/a:FormAnswerData/a:QuestionName[1]来计入GetRequestResponseGetRequestResult元素
+0

当我使用包含GetRequestResponse和GetRequestResult的Xpath时,我得到“不匹配”。 /秒:信封/秒:车身/ GetRequestResponse/GetRequestResult/A:回答/ A:FormAnswerData/A:QuestionName [1]。 – Sudheer 2015-04-02 16:25:24

+0

您正在使用的在线工具明确表示** XPath测试人员完全支持XML名称空间,但声明必须是显式的,并且必须位于根XML元素上。如果您在那里移动'a'命名空间的声明,它还会警告** XPath查询的默认(无前缀)名称空间URI始终为'',并且不能重新定义为'http://intapp.com/Wilco。 Service.Web /'**;所以,你要么使用不同的工具,要么为名称空间'xmlns =“定义一个前缀”http://intapp.com/Wilco.Service.Web/“' – lfurini 2015-04-02 16:38:11