2016-01-28 57 views
0

我正在尝试获取以下工作,但唉,它无法返回预期的结果。虽然与我在限制xpath 1.0之前所问的问题类似。具有名称空间的XPath过滤器属性

我正在寻找使用xpath来获取“副标题”节点内的第一个文本节点。该XML如下:

<topic class="coverPage"> 
    <subtitle id="IDb2907ca1-51fe-472e-bf99-246126937eab"> 
     <xt:delText xt:action="start" xt:author="James Doherty" xt:dateTime="2016-01-27T17:07:00" xt:id="fb72fba6-f502-422e-9e91-1731ed007e98"/> 
      Ignore 
     <xt:delText xt:action="end" xt:id="fb72fba6-f502-422e-9e91-1731ed007e98"/> 
     Sub-title 
     <xt:insText xt:action="start" xt:author="James Doherty" xt:dateTime="2016-01-27T14:55:00" xt:id="44ac82c2-acfc-4721-b962-20ac2b18d9f3"/> 
      Insert Additional Text 
     <xt:insText xt:action="end" xt:id="44ac82c2-acfc-4721-b962-20ac2b18d9f3"/> 
     Extra Text 
    </subtitle> 
</topic> 

替代XML还提供如下:

<topic class="coverPage"> 
    Sub-title 
    <subtitle id="IDb2907ca1-51fe-472e-bf99-246126937eab"> 
     <xt:delText xt:action="start" xt:author="James Doherty" xt:dateTime="2016-01-27T17:07:00" xt:id="fb72fba6-f502-422e-9e91-1731ed007e98"/> 
      Ignore 
     <xt:delText xt:action="end" xt:id="fb72fba6-f502-422e-9e91-1731ed007e98"/> 
     <xt:insText xt:action="start" xt:author="James Doherty" xt:dateTime="2016-01-27T14:55:00" xt:id="44ac82c2-acfc-4721-b962-20ac2b18d9f3"/> 
      Insert Additional Text 
     <xt:insText xt:action="end" xt:id="44ac82c2-acfc-4721-b962-20ac2b18d9f3"/> 
     Extra Text 
    </subtitle> 
</topic> 

我曾尝试以下,但没有运气:

/topic[@class='coverPage']/*[local-name()='subtitle']/text()[1]|/topic[@class='coverPage']/*[local-name()='subtitle']/*[substring(local-name(), string-length(local-name())-string-length('Text')+1)='Text'][@*[local-name()='action']='end'][1]/following-sibling::text()[1] 

我相信这个问题是属性值“action”并且它有一个名称空间。预期的结果将是“小标题”。关于如何让这个工作的任何想法?

+0

查询后半仿佛回到“副标题”按照您的期望:http://www.utilities-online.info/xpath/?save=33963c98-b5b0-44b5-b83c- 3010e5d85f12-xpath#.VqomcOaICUk(我从头开始使用'.',以便它会从根节点开始) –

+0

谢谢@KeithHall,但是您提供的示例包含一个xml命名空间的定义,我的XML不会没有。我还向OP添加了另一个XML示例,其中包含一个应该与xpath的“OR”部分配合使用的示例。谢谢... – JJDoc

+0

会这样的帮助吗? '// topic [@ class =“coverPage”]/descendant :: subtitle/* [position()= 1]' – Newcomer

回答

0

XML缺少xml名称空间定义,正如OP注释中由@KeithHall指出的那样。

<topic class="coverPage" "xmlns:xt="http://stackoverflow.com/questions/35063599/xpath-filter-attribute-with-namespace"> 
    <subtitle id="IDb2907ca1-51fe-472e-bf99-246126937eab"> 
     <xt:delText xt:action="start" xt:author="James Doherty" xt:dateTime="2016-01-27T17:07:00" xt:id="fb72fba6-f502-422e-9e91-1731ed007e98"/> 
      Ignore 
     <xt:delText xt:action="end" xt:id="fb72fba6-f502-422e-9e91-1731ed007e98"/> 
     Sub-title 
     <xt:insText xt:action="start" xt:author="James Doherty" xt:dateTime="2016-01-27T14:55:00" xt:id="44ac82c2-acfc-4721-b962-20ac2b18d9f3"/> 
      Insert Additional Text 
     <xt:insText xt:action="end" xt:id="44ac82c2-acfc-4721-b962-20ac2b18d9f3"/> 
     Extra Text 
    </subtitle> 
</topic> 

要过滤基于属性值的XML,命名空间,以下XPath被用于:

[@*[local-name()='action']='end'] 

完整XPath来实现期望的结果是以下,从OP不变。

/topic[@class='coverPage']/*[local-name()='subtitle']/text()[1]|/topic[@class='coverPage']/*[local-name()='subtitle']/*[substring(local-name(), string-length(local-name())-string-length('Text')+1)='Text'][@*[local-name()='action']='end'][1]/following-sibling::text()[1] 
相关问题