valyman
valyman

Reputation: 105

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>

Upvotes: 10

Views: 9562

Answers (2)

Andrew MacKenzie
Andrew MacKenzie

Reputation: 1

I got this to work by using the string() function:

xmllint --xpath "string(//*[local-name()='PackageReference']/@Include)" ~/path/to/xml.xml

Upvotes: 0

Jack Fleeting
Jack Fleeting

Reputation: 24930

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

Upvotes: 15

Related Questions