如何从XML的嵌套元素内的键值中获取值

时间:2019-05-23 05:46:35

标签: xml xpath

请查看我的XML

    <?xml version="1.0" encoding="utf-8"?><d:query xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" m:type="Microsoft.Office.Server.Search.REST.SearchResult">
    <d:ElapsedTime m:type="Edm.Int32">39</d:ElapsedTime>
    <d:PrimaryQueryResult m:type="Microsoft.Office.Server.Search.REST.QueryResult">
    <d:CustomResults m:type="Collection(Microsoft.Office.Server.Search.REST.CustomResult)"/>
    <d:QueryId>aae2457e-d42a-4d71-a18c-fbd72ab09f11</d:QueryId>
    <d:QueryRuleId m:type="Edm.Guid">00000000-0000-0000-0000-000000000000</d:QueryRuleId>
    <d:RefinementResults m:null="true"/>
    <d:RelevantResults m:type="Microsoft.Office.Server.Search.REST.RelevantResults">
    <d:GroupTemplateId m:null="true"/>
    <d:ItemTemplateId m:null="true"/>
    <d:Properties m:type="Collection(SP.KeyValue)">...</d:Properties>
    <d:ResultTitle m:null="true"/>
    <d:ResultTitleUrl m:null="true"/>
    <d:RowCount m:type="Edm.Int32">1</d:RowCount>
    <d:Table m:type="SP.SimpleDataTable">
    <d:Rows>
    <d:element m:type="SP.SimpleDataRow">
    <d:Cells>
    <d:element m:type="SP.KeyValue">
    <d:Key>Rank</d:Key>
    <d:Value>6.4468879699707</d:Value>
    <d:ValueType>Edm.Double</d:ValueType>
    </d:element>
    <d:element m:type="SP.KeyValue">
    <d:Key>AccountName</d:Key>
    <d:Value>domain\tverma</d:Value>
    <d:ValueType>Edm.String</d:ValueType>
    </d:element>
    </d:Cells>
    </d:element>
    </d:Rows>
    </d:Table>
    <d:TotalRows m:type="Edm.Int32">1</d:TotalRows>
    <d:TotalRowsIncludingDuplicates m:type="Edm.Int32">1</d:TotalRowsIncludingDuplicates>
    </d:RelevantResults>
    <d:SpecialTermResults m:null="true"/>
    </d:PrimaryQueryResult>
    <d:SecondaryQueryResults m:type="Collection(Microsoft.Office.Server.Search.REST.QueryResult)"/>
    <d:SpellingSuggestion m:null="true"/>
    <d:TriggeredRules m:type="Collection(Edm.Guid)"/>
    </d:query>

我要从此XML中获取我创建了xpath的“帐户名”值,但该值不起作用:

d:query/d:PrimaryQueryResult/d:RelevantResults/d:Table/d:Rows/d:element/d:Cells/d:element[d:Key='AccountName')]/d:Value

正确的xpath是什么?

2 个答案:

答案 0 :(得分:0)

绝对一:

/d:query/d:PrimaryQueryResult/d:RelevantResults/d:Table/d:Rows/d:element/d:Cells/d:element[./d:Key/text() = 'AccountName']/d:Value/text()

又快又脏:

//d:element[./d:Key/text() = 'AccountName']/d:Value/text()

带有缩进的XML,以供将来的读者参考

<?xml version="1.0" encoding="utf-8"?>
<d:query xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" m:type="Microsoft.Office.Server.Search.REST.SearchResult">
<d:ElapsedTime m:type="Edm.Int32">39</d:ElapsedTime>
<d:PrimaryQueryResult m:type="Microsoft.Office.Server.Search.REST.QueryResult">
  <d:CustomResults m:type="Collection(Microsoft.Office.Server.Search.REST.CustomResult)"/>
  <d:QueryId>aae2457e-d42a-4d71-a18c-fbd72ab09f11</d:QueryId>
  <d:QueryRuleId m:type="Edm.Guid">00000000-0000-0000-0000-000000000000</d:QueryRuleId>
  <d:RefinementResults m:null="true"/>
  <d:RelevantResults m:type="Microsoft.Office.Server.Search.REST.RelevantResults">
    <d:GroupTemplateId m:null="true"/>
    <d:ItemTemplateId m:null="true"/>
    <d:Properties m:type="Collection(SP.KeyValue)">...</d:Properties>
    <d:ResultTitle m:null="true"/>
    <d:ResultTitleUrl m:null="true"/>
    <d:RowCount m:type="Edm.Int32">1</d:RowCount>
    <d:Table m:type="SP.SimpleDataTable">
      <d:Rows>
        <d:element m:type="SP.SimpleDataRow">
          <d:Cells>
            <d:element m:type="SP.KeyValue">
              <d:Key>Rank</d:Key>
              <d:Value>6.4468879699707</d:Value>
              <d:ValueType>Edm.Double</d:ValueType>
            </d:element>
            <d:element m:type="SP.KeyValue">
              <d:Key>AccountName</d:Key>
              <d:Value>domain\tverma</d:Value>
              <d:ValueType>Edm.String</d:ValueType>
            </d:element>
          </d:Cells>
        </d:element>
      </d:Rows>
    </d:Table>
    <d:TotalRows m:type="Edm.Int32">1</d:TotalRows>
    <d:TotalRowsIncludingDuplicates m:type="Edm.Int32">1</d:TotalRowsIncludingDuplicates>
  </d:RelevantResults>
  <d:SpecialTermResults m:null="true"/>
</d:PrimaryQueryResult>
<d:SecondaryQueryResults m:type="Collection(Microsoft.Office.Server.Search.REST.QueryResult)"/>
<d:SpellingSuggestion m:null="true"/>
<d:TriggeredRules m:type="Collection(Edm.Guid)"/>
</d:query>

答案 1 :(得分:0)

另一个选择:

//d:Key[text()='AccountName']/following::d:Value/text()