Skip to content

Instantly share code, notes, and snippets.

@airvzxf
Last active July 25, 2023 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save airvzxf/66c6ebaaf3ccdf3630bf28233ff2acfa to your computer and use it in GitHub Desktop.
Save airvzxf/66c6ebaaf3ccdf3630bf28233ff2acfa to your computer and use it in GitHub Desktop.
xmllint xmlns error using the XPath
#!/usr/bin/env bash
set -vx
# chmod u+x xmllint-xmlns-solution.bash
# Execute: ./xmllint-xmlns-solution.bash
# DESCRIPTION
# The xmllint program parses one or more XML files, specified on the command line as XML-FILE (or the standard input if the
# filename provided is - ). It prints various types of output, depending upon the options selected. It is useful for detecting
# errors both in XML code and in the XML parser itself.
# --xpath "XPath_expression"
# Run an XPath expression given as argument and print the result. In case of a nodeset result, each node in the node set is
# serialized in full in the output. In case of an empty node set the "XPath set is empty" result will be shown and an error
# exit code will be returned.
# --xpath expr: evaluate the XPath expression, imply --noout
# --noout : don't output the result tree
# ------------------------------------------------------------------------------
xmllint --xpath "//packaging" ./pom.xml
# Show the exit code from the last command.
echo "${?}"
# The output is #10: XPath evaluation error
# DIAGNOSTICS
# xmllint return codes provide information that can be used when calling it from scripts.
# 1: Unclassified
# 2: Error in DTD
# 3: Validation error
# 4: Validation error
# 5: Error in schema compilation
# 6: Error writing output
# 7: Error in pattern (generated when --pattern option is used)
# 8: Error in Reader registration (generated when --chkregister option is used)
# 9: Out of memory error
# 10: XPath evaluation error
# ------------------------------------------------------------------------------
# https://www.w3schools.com/XML/xml_xpath.asp
xmllint --xpath "/project/packaging/text()" ./pom.xml
echo "${?}"
# ------------------------------------------------------------------------------
# PROBLEM:
# The command 'xmllint', kind of check the 'xmlns'. The Maven URL is not found, it should be the problem.
# If you remove the text 'xmlns="http://maven.apache.org/POM/4.0.0"' in the 'project' tag, it works.
#
# xmlns: It means XML namespace.
# Basically, every element (or attribute) in XML belongs to a namespace, a way of "qualifying" the name of the element.
xmllint --xpath "/project/packaging/text()" ./pom-modified.xml
echo "${?}"
# ------------------------------------------------------------------------------
# SOLUTIONS:
# https://stackoverflow.com/questions/8264134/xmllint-failing-to-properly-query-with-xpath-without-namespaces
# https://stackoverflow.com/questions/15390813/xmllint-does-not-work-properly-with-xpath
xmllint --xpath '//*[local-name()="packaging"]/text()' ./pom.xml
echo "${?}"
# --------------------------------------
xmllint --xpath '//*[namespace-uri()="http://maven.apache.org/POM/4.0.0" and local-name()="packaging"]/text()' ./pom.xml
echo "${?}"
# ------------------------------------------------------------------------------
# MY SOLUTION:
# Remove the properties of the 'project' tag and execute the 'xmllint'.
sed -E 's/<project.*/<project>/' ./pom.xml |
sed -E 's/\s*xsi:schemaLocation.*//' |
xmllint --xpath "/project/packaging/text()" -
echo "${?}"
@johnkgerken
Copy link

Very helpful and comprehensive. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment