xmlstarlet:如果子元素存在,则选择属性

时间:2017-01-03 06:53:03

标签: xml svg xpath xmlstarlet

我有一个包含以下内容的svg(已删除):

<svg
   xmlns="http://www.w3.org/2000/svg"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape">
   <g
     inkscape:label="base"
     inkscape:groupmode="layer"
     id="layer1"
     transform="translate(0,-600)">
    <image
       sodipodi:absref="/Untitled.png"
       xlink:href="/Untitled.png"
       width="800"
       height="500"
       preserveAspectRatio="none"
       id="image5168"
       x="3.67"
       y="596" />
  </g>
  <g
     inkscape:groupmode="layer"
     id="layer30"
     inkscape:label="triangle">
    <path
       style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
       d="M 273,340 290,127 563,307 Z"
       id="path5174"
       inkscape:connector-curvature="0" />
  </g>
</svg>

Complete XML

当我查询时:

xmlstarlet sel -t -v "//_:svg/_:g/@inkscape:label" drawing.svg

我明白了:

base
triangle

我一直在深入研究这些条件,但我找不到一种方法来选择g的{​​{1}}只有它包含inkscape:label节点[或者其他,如果它不包含path节点。

1 个答案:

答案 0 :(得分:3)

要仅获得包含子DECLARE @I INT = 1 DECLARE @ID INT = 0 DECLARE @MaxPackID VARCHAR(50) DECLARE @PackID VARCHAR(50) SELECT @PackID = MAX(PackID) FROM tblPacks WHERE PackID LIKE 'PK%' IF(@String = 'Packs') BEGIN WHILE @I <= @Count BEGIN IF @PackID IS NULL BEGIN SET @I = @I + 1 SET @ID = @ID + 1 INSERT INTO tblPacks (T_ID, BatchNumber, PackID, Status, BlistersCount) VALUES (@T_ID, @BatchNumber, 'PK' + CAST(@ID AS VARCHAR(50)), 0, (SELECT NumberOfBlistersInEachPack FROM tblPackDetails WHERE T_ID = @T_ID AND BatchNumber = @BatchNumber)) END ELSE BEGIN SET @I = @I + 1 SELECT @MaxPackID = MAX(SUBSTRING(PackID,PATINDEX('%[0-9]%',PackID),LEN(PackID))) FROM tblPacks SELECT @ID = @MaxPackID SET @ID = @ID + 1 INSERT INTO tblPacks (T_ID, BatchNumber, PackID, Status, BlistersCount) VALUES (@T_ID, @BatchNumber, 'PK' + CAST(@ID AS VARCHAR(50)), 0, (SELECT NumberOfBlistersInEachPack FROM tblPackDetails WHERE T_ID = @T_ID AND BatchNumber = @BatchNumber)) END END END END 的{​​{1}},您只需在g的谓词中添加path

path