lxml for xthon中的xpath只获取一个节点

时间:2017-10-01 11:27:53

标签: python xpath lxml

我有这个xml文档。我试图将此处显示的数据组1-7放入数据类别。当使用Xpath时,我通常可以过滤到我需要的位置,但是当我访问Con1时,似乎只给了我那组孩子,它给了我同一级别的“dict”

<dict>
<key>Tester</key>
    <dict>
        <key>Con1</key>
        <dict>
            <key>KeySet</key>
            <dict>
                <key>B52:B53:B54</key>
                <array>
                    <dict>
                        <key>0:0:0</key>
                        <dict>
                            <key>Data</key>
                            <array>
                                <string>1</string>
                                <string>2</string>
                                <string>3</string>
                                <string>4</string>
                                <string>5</string>
                                <string>6</string>
                                <string>7</string>
                            </array>
                        </dict>
                    </dict>
                    <dict>
                        <key>15:15:3</key>
                        <dict>
                            <key>Data</key>
                            <array/>
                        </dict>
                    </dict>
                    <dict>
                        <key>7:15:0</key>
                        <dict>
                            <key>Data</key>
                            <array/>
                        </dict>
                    </dict>
                    <dict>
                        <key>7:15:1</key>
                        <dict>
                            <key>Data</key>
                            <array/>
                        </dict>
                    </dict>
                </array>
            </dict>
        </dict>
        <key>Con2</key>
        <dict>
            <key>KeySet</key>
            <dict>
                <key>B52:B53:B54</key>
                <array>
                    <dict>
                        <key>0:0:0</key>
                        <dict>
                            <key>Data</key>
                            <array/>
                        </dict>
                    </dict>
                    <dict>
                        <key>0:16:0</key>
                        <dict>
                            <key>Data</key>
                            <array/>
                        </dict>
                    </dict>
                    <dict>
                        <key>0:48:0</key>
                        <dict>
                            <key>Data</key>
                            <array/>
                        </dict>
                    </dict>
                </array>
            </dict>
        </dict>
        <key>Con3</key>
        <dict>
            <key>KeySet</key>
            <dict>
                <key>B52:B53:B54</key>
                <array>
                    <dict>
                        <key>0:0:0</key>
                        <dict>
                            <key>Data</key>
                            <array/>
                        </dict>
                    </dict>
                </array>
            </dict>
        </dict>
    </dict>

所以,如果我写:

myPath = '//dict[key/text()="Tester"]/dict[key/text()="Con1"][1]'
x = tree.xpath(myPath)
etree.tostring(x, pretty_print=True)

我会得到:

     <dict>
        <key>Con1</key>
        <dict>
            <key>KeySet</key>
            <dict>
                <key>B52:B53:B54</key>
                <array>
                    <dict>
                        <key>0:0:0</key>
                        <dict>
                            <key>Data</key>
                            <array>
                                <string>1</string>
                                <string>2</string>
                                <string>3</string>
                                <string>4</string>
                                <string>5</string>
                                <string>6</string>
                                <string>7</string>
                            </array>
                        </dict>
                    </dict>
                    <dict>
                        <key>15:15:3</key>
                        <dict>
                            <key>Data</key>
                            <array/>
                        </dict>
                    </dict>
                    <dict>
                        <key>7:15:0</key>
                        <dict>
                            <key>Data</key>
                            <array/>
                        </dict>
                    </dict>
                    <dict>
                        <key>7:15:1</key>
                        <dict>
                            <key>Data</key>
                            <array/>
                        </dict>
                    </dict>
                </array>
            </dict>
        </dict>
        <key>Con2</key>
        <dict>
            <key>KeySet</key>
            <dict>
                <key>B52:B53:B54</key>
                <array>
                    <dict>
                        <key>0:0:0</key>
                        <dict>
                            <key>Data</key>
                            <array/>
                        </dict>
                    </dict>
                    <dict>
                        <key>0:16:0</key>
                        <dict>
                            <key>Data</key>
                            <array/>
                        </dict>
                    </dict>
                    <dict>
                        <key>0:48:0</key>
                        <dict>
                            <key>Data</key>
                            <array/>
                        </dict>
                    </dict>
                </array>
            </dict>
        </dict>
        <key>Con3</key>
        <dict>
            <key>KeySet</key>
            <dict>
                <key>B52:B53:B54</key>
                <array>
                    <dict>
                        <key>0:0:0</key>
                        <dict>
                            <key>Data</key>
                            <array/>
                        </dict>
                    </dict>
                </array>
            </dict>
        </dict> 

我希望只获得第一个连接集,以便我可以正确地添加我的数据。因此,运行该组代码时的理想输出如下所示:

     <dict>
        <key>Con1</key>
        <dict>
            <key>KeySet</key>
            <dict>
                <key>B52:B53:B54</key>
                <array>
                    <dict>
                        <key>0:0:0</key>
                        <dict>
                            <key>Data</key>
                            <array>
                                <string>1</string>
                                <string>2</string>
                                <string>3</string>
                                <string>4</string>
                                <string>5</string>
                                <string>6</string>
                                <string>7</string>
                            </array>
                        </dict>
                    </dict>
                    <dict>
                        <key>15:15:3</key>
                        <dict>
                            <key>Data</key>
                            <array/>
                        </dict>
                    </dict>
                    <dict>
                        <key>7:15:0</key>
                        <dict>
                            <key>Data</key>
                            <array/>
                        </dict>
                    </dict>
                    <dict>
                        <key>7:15:1</key>
                        <dict>
                            <key>Data</key>
                            <array/>
                        </dict>
                    </dict>
                </array>
            </dict>
        </dict>

我在这里做错了什么?

对于Andersson的回复,我将使用此代码编辑源代码。它确实有效,但由于上述问题,在确定应该放置的正确级别时遇到问题。

for parent in tree.xpath(myPath)
    parent.insert(1, data)

1 个答案:

答案 0 :(得分:0)

经过大量调试后我发现如果我添加&#34; / dict&#34;打电话我可以分开我的部分并使用[#]

访问它们

所以不要这样:

myPath = '//dict[key/text()="Tester"]/dict[key/text()="Con1"][1]'

使用此

myPath = '//dict[key/text()="Tester"]/dict[key/text()="Con1"]/dict[1]'