Search code examples
attributesxml-namespacesxmlstarletvcxproj

Reading source files in MSVS project using xmlstarlet


I am trying to get the source files and include directories from a vcxproj file. Eg of vcsproj:

<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
.... 
 <ItemGroup>
    <ClCompile Include="$(SrcDir)d1\f1cpp" />
    <ClCompile Include="$(SrcDir)d2\f2.cpp" />
 </ItemGroup>
...
</Project>

I tried this: xmlstarlet sel -t -v "//_:ItemGroup/ClCompile/@Include" myProj.vcxproj but didn't work.

However, when I tried this (copying the code from some page that I came across), it works:

 echo '<?xml version="1.0" encoding="utf-8"?> <ELEMENT xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <SUB_ELEMENT attribute="attr_value"/>
</ELEMENT>' | xmlstarlet sel -t -v '//_:SUB_ELEMENT/@attribute' --nl
o/p: attr_value

I don't see how the two are different with respect to reading an attribute value from an xml with a namespace. I further tried a stripped down version of the vcxproj file:

 echo '<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
    <ClCompile Include="$(SrcDir)d1\f1cpp" /> </ItemGroup> </Project>' | xmlstarlet sel -t -v '//_:ItemGroup/@Include' --nl
o/p: <no o/p>

Any indication on why it is not working or how to get this to work would be very helpful.

Edit: Expected output from the vcxproj would be a list of filenames. For the above command it would be $(SrcDir)d1\f1cpp


Solution

  • Any indication on why it is not working or how to get this to work would be very helpful

    Since you're using the default namespace add the _: shortcut on the node test in each location step, and the -T (--text) option to make text mode output:

    xmlstarlet select -T -t -v "//_:ItemGroup/_:ClCompile/@Include" -n file.xml