使用xpath计算XML中的元素数量 [英] counting number of elements from XML using xpath

查看:118
本文介绍了使用xpath计算XML中的元素数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用这个出色的论坛来教自己一些基本的xpath来查询.XML文件.我在这里有我的XML文件的示例,我正在尝试使用Matlab导入XML文件中3个对象的[X,Y]坐标:

I've used this excellent forum to teach myself some basic xpath to query an .XML file. I've got a sample of my XML file here, and I'm trying to import the [X,Y] coordinates of 3 objects in the XML file using Matlab:

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>ROI array</key>
    <array>
        <dict>
            <key>Comments</key>
            <string></string>
            <key>Name</key>
            <string>Unnamed</string>
            <key>ROIPoints</key>
            <array>
                <string>{129.24051549947484, 263.66036033996352}</string>
                <string>{114.61421850240453, 278.56760216125258}</string>
                <string>{123.11826208150609, 289.73859978088149}</string>
                            <string>{125.11111111111111, 295.77777777777777}</string>
            </array>
            <key>Slice</key>
            <integer>58</integer>
        </dict>
        <dict>
            <key>Comments</key>
            <string></string>
            <key>Name</key>
            <string>Unnamed</string>
            <key>ROIPoints</key>
            <array>
                <string>{127.09352448499425, 261.31629753478774}</string>
                <string>{112.50917389905675, 277.25509453185805}</string>
                <string>{126.061969309213, 291.36980247863539}</string>
                <string>{141.48499634778722, 292.16234398254164}</string>
                <string>{149.71229126966222, 277.81281090148696}</string>
            </array>
            <key>Slice</key>
            <integer>59</integer>
        </dict>
        <dict>
            <key>Comments</key>
            <string></string>
            <key>Name</key>
            <string>Unnamed</string>
            <key>ROIPoints</key>
            <array>
                <string>{134.32833430087788, 258.21743274101027}</string>
                <string>{117.0812182120107, 266.44891620048293}</string>
                <string>{114.41427180087788, 292.20427203544386}</string>
                <string>{128.80573603427632, 299.11905932792433}</string>
                <string>{147.92307612216695, 299.11905932792433}</string>
                <string>{152.73700281894429, 285.80526996024855}</string>
                <string>{154.32626673495992, 268.51202655204543}</string>
            </array>
            <key>Slice</key>
            <integer>60</integer>
        </dict>
        </array>
</dict>
</plist>

我已经使用此Matlab代码成功导出了所有坐标:

I've managed to export all of the coordinates using this Matlab code:

expression_2 = xpath.compile('plist/dict/array/dict/array/string');
nodeList_2 = expression_2.evaluate(docNode, XPathConstants.NODESET);
for i = 1:nodeList_2.getLength
    node = nodeList_2.item(i-1);
    coordinate_node{i} = char(node.getFirstChild.getNodeValue);
end

有人知道xpath查询,通过它我可以计算每个对象中[X,Y]坐标的数量吗? IE.返回第一个对象的4个坐标,第二个对象的5个坐标和第三个对象的7个坐标的返回值?

Does anyone know of an xpath query whereby I could count the NUMBER of [X,Y] coordinates in each object? I.e. something that returns 4 coordinates for the first object, 5 coordinates in the second and 7 coordinates in the 3rd?

谢谢, 吉姆

推荐答案

您处在正确的轨道上.但是,问题在于您的代码会以扁平方式提取所有字符串"节点,而忽略父数组"节点.这样您就无法分辨哪个坐标属于哪个对象.

You're on the right track. However, the problem is that your code extracts all "string" nodes in a flat way, disregarding parent "array" nodes. This way you cannot tell which coordinate belongs to which object.

如果稍微修改一下代码,使其以分层方式遍历数组"和字符串"节点,则可以像您想要的那样工作:

If you modify your code a little bit so that it would traverse through the "array" and "string" nodes in a hierarchical way, it can work like you want to:

%// Extract 'array' nodes
expr_array = xpath.compile('plist/dict/array/dict/array');
nodeList_array = expr_array.evaluate(docNode, XPathConstants.NODESET);
C = cell(nodeList_array.getLength, 1);
for k = 1:nodeList_array.getLength

    %// Extract 'string' nodes
    node_array = nodeList_array.item(k - 1);
    expr_string = xpath.compile('string');
    nodeList_string = expr_string.evaluate(node_array, XPathConstants.NODESET);
    coordinates = zeros(nodeList_string.getLength, 2);
    for m = 1:nodeList_string.getLength
        node_string = nodeList_string.item(m - 1);
        s = char(node_string.getFirstChild.getNodeValue); %// Extract string
        coordinates(m, :) = str2num(s(2:end - 1));        %// Convert to numbers
    end
    C{k} = coordinates;
end

现在单元格数组C包含所有坐标(顺便说一句,我将它们转换为数值,以便可以将它们存储在矩阵中并易于操作):

Now cell array C contains all coordinates (by the way, I converted them to numerical values so that they can be stored in a matrix and manipulated easily):

C{1} =
  129.2405  263.6604
  114.6142  278.5676
  123.1183  289.7386
  125.1111  295.7778

C{2} =
  127.0935  261.3163
  112.5092  277.2551
  126.0620  291.3698
  141.4850  292.1623
  149.7123  277.8128

C{3} =
  134.3283  258.2174
  117.0812  266.4489
  114.4143  292.2043
  128.8057  299.1191
  147.9231  299.1191
  152.7370  285.8053
  154.3263  268.5120

现在,如果要每个单元(对象)中的坐标数,只需执行以下操作:

Now, if you want the number of coordinates in each cell (object), simply do this:

cellfun(@(c)size(c, 1), C)

您将获得所需的结果:

ans=
    4
    5
    7

这篇关于使用xpath计算XML中的元素数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆