使用xpath计算XML中的元素数量

时间:2013-01-23 10:12:59

标签: xml matlab xpath

我使用这个优秀的论坛来教自己一些基本的xpath来查询.XML文件。我在这里得到了一个XML文件样本,我正在尝试使用Matlab导入XML文件中3个对象的[X,Y]坐标:

<!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代码导出所有坐标:

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]坐标的NUMBER个?即返回第一个对象的4个坐标,第二个坐标为5个坐标,第3个坐标为7个坐标?

谢谢, 吉姆

2 个答案:

答案 0 :(得分:1)

我不熟悉你的matlab的XPath实现,但XPath确实有聚合函数,我在Java中使用它们:

http://www.w3.org/TR/xpath-functions/#func-count

请首先参阅有关此主题的matlab手册。你也可以在这个类似的问题中看一眼如何使用它。

How do I use XPath to count the number of nodes with a certain attribute

答案 1 :(得分:1)

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

如果你稍微修改你的代码以便它以分层方式遍历“数组”和“字符串”节点,它可以像你想要的那样工作:

%// 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包含所有坐标(顺便说一句,我将它们转换为数值,以便它们可以存储在矩阵中并轻松操作):

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

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

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

您将获得所需的结果:

ans=
    4
    5
    7