带有defiant.js节点的空格的XPath表达式

时间:2015-03-10 19:06:30

标签: javascript jquery json xpath

我正在使用defiant.js搜索json。根据他们的文档,我需要提供xpath来搜索json中的特定元素。当节点中没有空格时,我很成功。但我的json现在包含空格。这是我的csv。我想获得一个具有特定证书编号的项目。

    [
  {
    "Certificate Number":1019174,
    "Last Name":"MA",
    "First Name":"SHI",
    "Title":"MR",
    "DOB":"1/28/1990",
    "Address":"75 LEYLAND HOUSE",
    "City":"London",
    "Postal Code":"E14 0BU",
    "Country":"UK",
    "Passpot Number":"E0438750",
    "Course Level":7,
    "Course Details":"POST GRADUATE DIPLOMA IN INTERNATIONAL COMMERCIAL LAW",
    "Course Start Date":"2/2/2014",
    "Course End Date":"2/23/2015",
    "Note":"RPL Route Entry",
    "Remarks":""
  },
  {
    "Certificate Number":101235,
    "Last Name":"UD",
    "First Name":"HAN",
    "Title":"MR",
    "DOB":"1/28/1990",
    "Address":"75 LEYLAND HOUSE",
    "City":"London",
    "Postal Code":"E14 0BU",
    "Country":"UK",
    "Passpot Number":"E0438750",
    "Course Level":7,
    "Course Details":"POST GRADUATE DIPLOMA IN INTERNATIONAL COMMERCIAL LAW",
    "Course Start Date":"2/2/2014",
    "Course End Date":"2/23/2015",
    "Note":"RPL Route Entry",
    "Remarks":""
  }
]

我的xpath是

//[Certificate Number="101234"]

当属性名称类似于CertificateNumber时,我没有问题找到该项目。但是,如果证书与编号之间有空格,我就无法获得该项目。那么如何从这个json获得具有特定证书编号的项目。除了defiant.js之外的任何替代解决方案都可以。

2 个答案:

答案 0 :(得分:4)

似乎JSON属性作为XML节点名称无效(因为它们包含空格),它们将被defiant.js转换为命名空间中的XML属性。

例如,这部分JSON ..:

{
    "Certificate Number":101235,
    "Last Name":"UD",
    "First Name":"HAN",
    "Title":"MR",
    "DOB":"1/28/1990",
    "Address":"75 LEYLAND HOUSE",
    "City":"London",
    "Postal Code":"E14 0BU",
    "Country":"UK",
    "Passpot Number":"E0438750",
    "Course Level":7,
    "Course Details":"POST GRADUATE DIPLOMA IN INTERNATIONAL COMMERCIAL LAW",
    "Course Start Date":"2/2/2014",
    "Course End Date":"2/23/2015",
    "Note":"RPL Route Entry",
    "Remarks":""
  }

..转换为以下XML结构(此处仅发布XML的相关部分):

<d:item xmlns:d="defiant-namespace" d:mi="34">
   <d:name d:name="Certificate Number" d:constr="Number" d:mi="18">101235</d:name>
   <d:name d:name="Last Name" d:constr="String" d:mi="19">UD</d:name>
   <d:name d:name="First Name" d:constr="String" d:mi="20">HAN</d:name>
   <Title d:constr="String" d:mi="21">MR</Title>
   <DOB d:constr="String" d:mi="22">1/28/1990</DOB>
   <Address d:constr="String" d:mi="23">75 LEYLAND HOUSE</Address>
   <City d:constr="String" d:mi="24">London</City>
   <d:name d:name="Postal Code" d:constr="String" d:mi="25">E14 0BU</d:name>
   <Country d:constr="String" d:mi="26">UK</Country>
   <d:name d:name="Passpot Number" d:constr="String" d:mi="27">E0438750</d:name>
   <d:name d:name="Course Level" d:constr="Number" d:mi="28">7</d:name>
   <d:name d:name="Course Details" d:constr="String" d:mi="29">POST GRADUATE DIPLOMA IN INTERNATIONAL COMMERCIAL LAW</d:name>
   <d:name d:name="Course Start Date" d:constr="String" d:mi="30">2/2/2014</d:name>
   <d:name d:name="Course End Date" d:constr="String" d:mi="31">2/23/2015</d:name>
   <Note d:constr="String" d:mi="32">RPL Route Entry</Note>
   <Remarks d:constr="String" d:mi="33"/>
</d:item>

因此,根据XML结构,这是一个可能通过&#34;证书编号&#34;价值:

//*[*[@*="Certificate Number" and .=101235]]

答案 1 :(得分:0)

使用JSONPath,您应该可以使用

//Certificate Number="101235"

无需[]