Search code examples
xpathxmllint

Combine results from multiple xpaths in xmlint


I'd like to return the results from multiple --xpath phrases. I have tried

xmllint --xpath '//ItemGroup/Content/@Include' --xpath '//ItemGroup/None/@Include' --xpath '//Compile/@Include' -

This returns only the last xpath

I have tried the same thing using concat() however that returns only one match from each xpath:

xmllint  --xpath "concat(concat(//ItemGroup/None/@Include,' ', //ItemGroup/Content/@Include), ' ', //ItemGroup/Compile/@Include)" -

Solution

  • You should you the union operator | like this:

    xmllint --xpath '//ItemGroup/Content/@Include | //ItemGroup/None/@Include | //Compile/@Include' input.xml
    

    With a sample XML like

    <?xml version="1.0" encoding="UTF-8"?>
    <class>
        <ItemGroup>
            <Content Include="First Item"> aaa </Content>
            <None Include="Third Item"> bbb </None>
            <Content Include="Second Item"> aaa </Content>
        </ItemGroup>
        <parent>
            <Compile Include="Compiling is great"> aaa </Compile>
            <sub2> bbb </sub2>
        </parent>
    </class>
    

    the output is:

    Include="First Item" Include="Third Item" Include="Second Item" Include="Compiling is great"
    

    This does work with XPath-1.0.