如果特定子节点存在特定兄弟节点,则选择元素

时间:2015-04-23 17:02:24

标签: xml xpath

我正在尝试选择所有具有下一个兄弟节点 dict 且其中一个子节点为 true 的键,例如在下面的xml中:

<dict>
    <key>A</key>
    <dict>
        <key>Disabled</key>
        <true/>
    </dict>
    <key>B</key>
    <dict>
        <key>Disabled</key>
        <false/>
    </dict>
    <key>C</key>
    <dict>
        <key>Disabled</key>
        <true/>
    </dict>
    <key>D</key>
    <id>1</id>
</dict>

我应该得到A和C.我试过了:

//key[../dict/true]
//key[../dict/[following-sibling::dict/true]]

和其他许多人。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

您需要的路径表达式是

   import UIKit
   import Social
   import CloudKit

   class TeamPage: UITableViewController, UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource, UIAlertViewDelegate {

@IBOutlet weak var sportTitle: UILabel!

var teamSport: String!

override func viewDidLoad() {
    super.viewDidLoad()

    self.sportTitle = UILabel()

    println(teamSport)

    self.sportTitle.text = teamSport

    println(sportTitle.text)

}

转换为

/dict/key[following-sibling::*[position() = 1 and name() = 'dict' and true]]

将您显示的文档作为输入,结果是(个别结果以/dict/key Select an outermost element, and its children `key` [following-sibling::* but only if there is a following sibling [position() = 1 that is the first following sibling and name() = 'dict' and that is also a `dict` element and true]] that has at least one child element named `true`. 分隔):

----------

可替换地:

<key>A</key>
-----------------------
<key>C</key>

答案 1 :(得分:1)

你可以试试这个:

==>type D:\VB_scripts\SO\29832173.vbs
option explicit
Dim commandList(2), str, ii
commandList(0)= "x = 3"
commandList(1)= "y = 4"
commandList(2)= "z = x + y"
For ii = 0 To UBound( commandList)
  Wscript.Echo "To execute: " & commandList( ii), CStr( Eval( commandList( ii)))
  Execute commandList( ii)
  Wscript.Echo "  Executed: " & commandList( ii), CStr( Eval( commandList( ii)))
Next
Wscript.Echo x, y, z

==>cscript D:\VB_scripts\SO\29832173.vbs
To execute: x = 3 False
  Executed: x = 3 True
To execute: y = 4 False
  Executed: y = 4 True
To execute: z = x + y False
  Executed: z = x + y True
3 4 7

==>

这会选择//key[following-sibling::dict[position()=1]/true] dict的{​​{1}}元素(意味着紧跟在position()之后以及key的后代。

true选择所有以下的兄弟姐妹,但following-sibling::node()仅选择 FIRST