简体   繁体   English

无法使该XPath正常工作

[英]Can't get this XPath to work

I have some XML: 我有一些XML:

<metadata>
  <dataIdInfo>
    <idCitation>
      <resRefDate>
        <refDate>1996</refDate>
        <refDateTyp>
          <DateTypCd value="007" />
        </refDateTyp>
      </resRefDate>
      <resRefDate>
        <refDate>1998</refDate>
        <refDateTypCd>
          <DateTypCd value="003" />
        </refDateTypCd>
      </resRefDate>
    </idCitation>
  </dataIdInfo>
</metadata>

I'm trying to get the 1996 value from the key 007 so far I have got this 我试图从键007获得1996年的价值

XmlDocument doc = new xmlDocument();
doc.LoadXml(myXmlString);  
XmlNode node = doc.SelectSingleNode("metadata/dataIdInfo/idCitation/resRefDate/refDate[refDateType/DateTypCd[@value=\"007\"]");

But I keep getting the error that the address has an invalid token. 但是我不断收到错误消息,指出地址具有无效的令牌。 I've been reviewing the XPath example doc on msdn but obviously I have gone wrong in the square brackets part - help! 我一直在审查msdn上的XPath示例文档,但显然我在方括号部分中出错了-帮助!

Thanks Rob 谢谢罗布

Two problems. 两个问题。

Firstly you were failing to close both sets of square brackets, this is the invalid token. 首先,您无法关闭两组方括号,这是无效令牌。

Secondly, your filter expression (the square brackets) are in the wrong place, they should be before the refDate, as it is the resRefDate you are filtering. 其次,您的过滤器表达式(方括号)放在错误的位置,它们应该在refDate之前,因为它是您要过滤的resRefDate。 Below is the correct expression. 下面是正确的表达。

metadata/dataIdInfo/idCitation/resRefDate
                                [refDateTyp/DateTypCd/@value="007"]/refDate

refDateTyp is not a subtag of refDate, which is indicated by your condition in the query. refDateTyp不是refDate的子标记,它由查询中的条件指示。

you should probably replace ... /resRefDate/refDate[ ... by ... /resRefDate[ ... 您应该将... /resRefDate/refDate[ ...替换为... /resRefDate[ ...

看起来您使用的是“ refDateType”(结尾处带有“ e”),而不是我在XML片段中看到的“ refDateTyp”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM