开发者

Using Xpath to retrieve nodes satisfying a certain criteria, based on attribute values

<AgoraServersConfig>
  <AgoraServers id="NYQ1">
    <AgoraName>prod</AgoraName>
    <TableName>someTable</TableName>
    <Rule_ID>1</Rule_ID>
    <Rule_ID开发者_运维问答>3</Rule_ID>
    <Rule_ID>5</Rule_ID>
  </AgoraServers>
  <AgoraServers id ="QA03">
    <AgoraName>dev</AgoraName>
    <TableName>someTable</TableName>
    <Rule_ID>1</Rule_ID>
    <Rule_ID>2</Rule_ID>
    <Rule_ID>5</Rule_ID>
  </AgoraServers>
</AgoraServersConfig>

Given the above schema, I would like to know how to frame an Xpath query that returns the children of the node whose id is "QA03", for example.

Many Thanks in advance


Use:

/*/*[@id='QA03']/node()

It is also possible to use the standard XPath function id().

However, for this to work, there must be a DTD for the XML document that defines id as an "ID-type attribute".

Example: id('QA03')/node()


I'm not sure I fully understand your schema, but to return children of a node based on that node's id attribute would look something like

//*[@id='QA03']/*


The xpath would look like this:

/AgoraServersConfig/AgoraServers[@id='QA03']/*

If you wanted to do something a bit more dynamic, you could put the id into a variable, e.g.

<xsl:variable name="targetid">QA03</xsl:variable>
<xsl:for-each select="/AgoraServersConfig/AgoraServers[@id=$targetid]/*">
    <xsl:copy-of select="."/>
</xsl:for-each>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜