xmlstarlet match/parsing 按属性而不是元素

xmlstarlet match/parsing by attribute instead of element

我有一个结构复杂的 XML 文件,我试图使用 xmlstartlet 从中解析一些值。我认为问题在于逻辑对象是由元素属性而不是实际元素名称标识的。

文件 test.xml 针对此示例进行了简化,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<configDump>
  <cluster top-level-assembly="XYZ12312312312">
  </cluster>
  <views>
    <view status="ok" name="ABC">
      <viewInitiators>
        <viewInitiator id="ABCi1"></viewInitiator>
        <viewInitiator id="ABCi2"></viewInitiator>
      </viewInitiators>
      <viewPorts>
        <viewPort id="ABCp1"></viewPort>
        <viewPort id="ABCp2"></viewPort>
      </viewPorts>
      <viewVolumes>
        <viewVolume lun="1" id="ABCv1"></viewVolume>
        <viewVolume lun="2" id="ABCv2"></viewVolume>
      </viewVolumes>
    </view>
    <view status="ok" name="DEF">
      <viewInitiators>
        <viewInitiator id="DEFi1"></viewInitiator>
        <viewInitiator id="DEFi2"></viewInitiator>
      </viewInitiators>
      <viewPorts>
        <viewPort id="DEFp1"></viewPort>
        <viewPort id="DEFp2"></viewPort>
      </viewPorts>
      <viewVolumes>
        <viewVolume lun="1" id="DEFv1"></viewVolume>
        <viewVolume lun="2" id="DEFv2"></viewVolume>
      </viewVolumes>
    </view>
  </views>
</configDump>

我正在尝试使用如下字段生成输出:

<top-level-assembly>-<view-name>,<viewInitiator-ID>

当我运行以下内容时:

xmlstarlet sel -t \
                -m //cluster \
                        --var sn=@top-level-assembly \
                -m //views/view \
                        --var view=@name \
                -m //views/view/viewInitiators/viewInitiator \
                        -v 'concat($sn,"-",$view,",",@id)' -n test.xml

我得到以下输出:

XYZ12312312312-ABC,ABCi1
XYZ12312312312-ABC,ABCi2
XYZ12312312312-ABC,DEFi1
XYZ12312312312-ABC,DEFi2
XYZ12312312312-DEF,ABCi1
XYZ12312312312-DEF,ABCi2
XYZ12312312312-DEF,DEFi1
XYZ12312312312-DEF,DEFi2

正如您所看到的,我正在获取每个“视图”下的所有“启动器”值,而不仅仅是 XML 中该结构中的那些值。我期待并希望得到:

XYZ12312312312-ABC,ABCi1
XYZ12312312312-ABC,ABCi2
XYZ12312312312-DEF,DEFi1
XYZ12312312312-DEF,DEFi2

对我做错了什么有什么想法吗?

Any thoughts on what I'm doing wrong?

-m 选项嵌套到 xsl:for-each 语句 - 因此您要为每个 view 请求所有 viewInitiator/@id。要查看生成的 XSLT 代码,请在 -t.

之前添加 -C 选项

可以通过以下方式生成所需的输出:

xmlstarlet sel -t \
    --var sn='configDump/cluster/@top-level-assembly' \
    -m '//viewInitiator' \
       -v 'concat($sn,"-",../../@name,",",@id)' -n \
< file.xml