2015-11-04 142 views
0

我具有以下XML内容:选择xml元素匹配属性

<root> 
    <content> 
     <figure id="fig-001"> 
      <graphic id="fig-001-graId-001"/> 
      <graphic id="fig-001-graId-002"/> 
      <graphic id="fig-001-graId-003"/> 
     </figure> 
     <figure id="fig-002"> 
      <graphic id="fig-002-graId-001"/> 
      <graphic id="fig-002-graId-002"/> 
     </figure>   
     <refs> 
      <ref ref-id="fig-001-graId-001" type="test"/> 
      <ref ref-id="fig-002-graId-001" type="test"/> 
     </refs> 
    </content> 
</root> 

我使用氧

我点击的元件参/ REF,例如在

<ref ref-id="fig-01-graId-001 type="test" /> 

我想要一个xpath,其中有图形/ @ id = ref/@ ref-id(我单击时)的图形元素,类似这样的事情:

count(// content/figure/graphic [@id = // ref/@ ref-id]/parent ::/graphic)==> 3(ref-id = fig-001-graId -001有3个图形元素)

+0

您希望对该输入样本有哪个结果? –

+0

我编辑了我的问题,对不起,这是不正确的 – Valeriane

+0

“*我点击一个元素refs/ref *”XPath不知道你点击了哪里。 –

回答

0

我想你想做的事:

count(//graphic[@id=//ref/@ref-id]/../graphic) 

或者,如果你喜欢:

count(//figure[graphic/@id=//ref/@ref-id]/graphic) 
+0

是的,但只有一个/ ref/@ ref-id – Valeriane

+0

@Valeriane请解释一下你的意思。你的输入例子中只有一个ref/@ ref-id。如果有两个(或更多),两者都会被考虑在内。 –

0

用钥匙,您可以使用

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

    <xsl:key name="id" match="figure" use="graphic/@id"/> 

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="/"> 
     <xsl:value-of select="count(key('id', //ref/@ref-id)/graphic)"/> 
    </xsl:template> 
</xsl:transform> 

看到http://xsltransform.net/ej9EGc1

+0

感谢您的回复。不幸的是,我不能使用xsl – Valeriane

+1

然后考虑删除xslt标签。 –

1

您特别提到氧气,所以这里的一个选项:

for $refid in @ref-id return count(//figure[graphic/@id = $refid]/graphic) 

此XPath 2.0将需要在XPath工具栏使用(请确保您有XPath 2.0中选择)。我不认为你可以使用XPath构建器,因为你失去了你的上下文。

我唯一能够在100%的时间内正常工作的时间是确保当我单击ref元素时,元素开始和结束时出现的2个括号(单击XPath后单击工具栏来运行XPath)。这确保了上下文是正确的。 (这可以通过在<ref之后直接点击完成。)

括号在下面的屏幕截图中突出显示。还要注意的截图底部的正确结果:

enter image description here

更新:添加ancestor-or-self::*[1]似乎与得到正确的情况下,无论以帮助那里的元素,你点击:

for $refid in ancestor-or-self::*[1]/@ref-id return count(//figure[graphic/@id = $refid]/graphic)