xmllint returns XPath set is empty
Asked Answered
V

1

10

What I do wrong? I get XPath set is empty when run the following command. xmllint --xpath './/PackageReference[@Include="Tips"]/Version/text()' sdk/Test/TestSample.xml

Please find below the xml file content.

<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
 <PackageReference Include="Tips">
   <Version>2.2.2</Version>
 </PackageReference>
</ItemGroup>
<ItemGroup>
 <PackageReference Include="Hips">
   <Version>1.1.1</Version>
 </PackageReference>
</ItemGroup>

</Project>
Vaden answered 18/1, 2022 at 13:58 Comment(0)
L
15

You are getting entangled with the dreaded namespaces. Since xmllint doesn't support namespace declarations, you can use this:

xmllint --xpath "//*[local-name()='PackageReference'][@Include='Tips']/*[local-name()='Version']/text()" your_file.xml

Alternatively, you can use xmlstarlet, like this:

xml sel -N x="http://schemas.microsoft.com/developer/msbuild/2003" -t -m "//x:PackageReference[@Include='Tips']/x:Version/text()" -v . your_file.xml

They should both output

2.2.2
Larcher answered 18/1, 2022 at 14:57 Comment(3)
@JackFleeting is that a typo in your second command ? what's the xml command ?Corbin
@Corbin Not a typo; xml is an alias (shortcut) for xmlstarlet; you can use either.Larcher
I ended up just using libxml-xpath-perl. It's xpath tool works with namespaces just fine.Franck

© 2022 - 2024 — McMap. All rights reserved.