【问题标题】:XML Parsing with Xpath(?) for certain value [closed]使用 Xpath(?) 解析 XML 以获取特定值 [关闭]
【发布时间】:2015-11-27 08:51:34
【问题描述】:

我需要解析一组特定的 XML 文件以获取值。

在这种情况下,我需要找到<group>THISGROUP</group> 和<permission>WRITE</permission> 的每个访问ID

在下面的示例中,我需要返回 VX-3422867。此外,我需要能够在 bash 脚本中完成此操作。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AccessControlListDocument>
    <access id="VX-3422867">
        <loc>http://localhost:8080/API/item/VX-410070/access/VX-3422867</loc>
        <grantor>admin</grantor>
        <recursive>true</recursive>
        <permission>WRITE</permission>
        <group>THISGROUP</group>
    </access>
        <access id="VX-3422866">
        <loc>http://localhost:8080/API/item/VX-410070/access/VX-3422866</loc>
        <grantor>admin</grantor>
        <recursive>true</recursive>
        <permission>NONE</permission>
        <operation>
            <shape>
                <tag>original</tag>
            </shape>
        </operation>
        <group>THISGROUP</group>
    </access>
    <access id="VX-3422865">
        <loc>http://localhost:8080/API/item/VX-410070/access/VX-3422865</loc>
        <grantor>admin</grantor>
        <recursive>true</recursive>
        <permission>WRITE</permission>
        <group>TWO</group>
    </access>
    <access id="VX-3422869">
        <loc>http://localhost:8080/API/item/VX-410070/access/VX-3422869</loc>
        <grantor>admin</grantor>
        <recursive>true</recursive>
        <permission>ALL</permission>
        <group>THREE</group>
    </access>
    <access id="VX-3422868">
        <loc>http://localhost:8080/API/item/VX-410070/access/VX-3422868</loc>
        <grantor>admin</grantor>
        <recursive>true</recursive>
        <permission>ALL</permission>
        <group>FOUR</group>
    </access>
    <access id="VX-975588">
        <loc>http://localhost:8080/API/item/VX-410070/access/VX-975588</loc>
        <recursive>true</recursive>
        <permission>OWNER</permission>
        <user>user-one</user>
    </access>
</AccessControlListDocument>

【问题讨论】:

  • 你有很多阅读要做,我想。也许你应该开始here。
  • 顺便说一句,“这是我的输入,请为我编写一个程序,以提供我想要的输出”是“请为我做我的工作”的子类别,这里不是特别欢迎 - - 因此投反对票。努力至少展示你已经尝试或考虑过的东西是有帮助的。
  • 至少他给出了输入和输出;-/ 祝大家好运。
  • 我很抱歉没有解释我所尝试的一切。我敢肯定,您理解处于紧要关头并需要帮助。

标签: bash xpath xml-parsing


【解决方案1】:

打印具有组 THISGROUP 和权限 WRITE 的所有访问条目的 ID:

xmlstarlet sel \
  -t -m '//access[group="THISGROUP"][permission="WRITE"]' \
  -v @id -n <in.xml

【讨论】:

    【解决方案2】:

    您可以使用以下 XPath 选择 group 元素值等于 'THISGROUP' 且 permission 元素值等于 'WRITE' 的访问元素的 @id 属性:

    /AccessControlListDocument/access[group = 'THISGROUP' and permission='WRITE']/@id 
    

    至于在 bash 脚本中执行,你可以看看使用 xmlstarlet:

    xmlstarlet sel -t -v "/AccessControlListDocument/access[group = 'THISGROUP' and permission='WRITE']/@id" myFile.xml
    

    或者使用How to execute XPath one-liners from shell?的答案中建议的各种其他选项(xmllint、saxon-lint、saxon、python、xidel)

    【讨论】:

    • 谢谢,我已经习惯用Xidel了。事实证明我接近我需要的语法,但不完全在那里。
    【解决方案3】:

    使用 xpath:

    如果您只想获取第一个 id 属性:

       <xsl:value-of select="/AccessControlListDocument/access[child::group='THISGROUP' and child::permission='WRITE']/@id"/>
    

    如果您希望每个 id 属性都符合该条件:

    <xsl:for-each select="/AccessControlListDocument/access[child::group='THISGROUP' and child::permission='WRITE']">
    <xsl:value-of select="./@id"/>
    </xsl:for-each>
    

    这意味着从“AccessControlListDocument”父节点的“access”节点子节点获取“id”属性,其中“access ”节点的子节点“group”的值为“THISGROUP”,节点“permission”的值为“WRITE”。

    【讨论】:

    • 这实际上是一个非常好的方法,因为人们更可能拥有 xsltproc,而不是拥有 xmlstarlet 或 xmllint 足够新以拥有--xpath。不过,您或许应该展示如何从 bash 运行它。
    • 我还建议将其描述为“使用 XSLT”而不是“使用 XPath”。
    猜你喜欢
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 2013-12-19
    • 2012-01-13
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多