Notes on common uses for xmlstarlet.

List attributes for all nodes

xmlstarlet sel -t -m '//node'  -v @attr1 -o ' ' -v @attr2 -n

Explanation:

-t: template
 -m: foreach node /*/node
 -v: value of
 @attr1: attribute access
 -o: output literal string
 -n: newline

Data:

<root>
  <node attr1="foo" attr2="bar"/>
  <node attr1="feed" attr2="back"/>
</root>

Output:

foo bar
feed back

Visual Studio Projects

Must specify the namespace. Tidier to predifine it.

# -N: predefine namespace
xmlstarlet sel -N _=http://schemas.microsoft.com/developer/msbuild/2003 "//_:AdditionalLibraryDirectories" $PROJECT_FILE

Delete Project References

# -L: inplace
# -d: delete nodes where the @Include contains $dep
dep=$1
xmlstarlet ed -L -N _=http://schemas.microsoft.com/developer/msbuild/2003 -d "//_:ProjectReference[contains(@Include,\"${dep}\")]" $PROJECT_FILE

Fix projects that don't inherit from parent

# -u: update the element
# -x: update with expression
xmlstarlet ed -P -L -N _=http://schemas.microsoft.com/developer/msbuild/2003 \
    -u "//_:AdditionalIncludeDirectories[not(contains(.,\"%(AdditionalIncludeDirectories)\"))]" \
    -x "concat(.,\"%(AdditionalIncludeDirectories)\",';')" \
    -u "//_:AdditionalLibraryDirectories[not(contains(.,\"%(AdditionalLibraryDirectories)\"))]" \
    -x "concat(.,\"%(AdditionalLibraryDirectories)\",';')" \
    $*