xmlxmllint

xmllint returns XPath set is empty


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>

Solution

  • 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