pylint and lxml

If you use Python virtualenv, pylint and lxml together, you may see error messages in pylint test results. It`s because only trusted C extension resources (the standard library) should be used. Here is an opportunity to improve the pylint test results.

Generate a .pylintrc file

# generate .pylintrc file
$ pylint --generate-rcfile > .pylintrc

Open the .pylintrc file to edit

# open with vim editor
$ vim .pylintrc

Add lxml to extension-pkg-whitelist

# A comma-separated list of package or module names from where C extensions may
# be loaded. Extensions are loading into the active Python interpreter and may
# run arbitrary code
extension-pkg-whitelist=lxml

If you now perform the pylint test again, no error relating to lxml should appear.

Test XML command line with xmllint

In most Linux and BSD, xmllint is delivered or very easy to install. Xmllint can parse and validate one or more XML files with output as reports. A version for Windows is available, too!

The tiny example XML file:

<?xml version="1.0" encoding="UTF-8"?>
<company>
	<branch lang="de-DE">
		<team id="011">
			<name status="on">Develop</name>
			<size>8</size>
		</team>
	</branch>
	<branch lang="en-US">
		<team id="021">
			<name status="off">Sales</name>
			<size>5</size>
		</team>
		<team id="022">
			<name status="on">QA</name>
			<size>2</size>
		</team>
	</branch>
	<branch lang="de-CH">
		<team id="031">
			<name status="on">Develop</name>
			<size>5</size>
		</team>
		<team id="032">
			<name status="off">Consultant</name>
			<size>3</size>
		</team>
	</branch>
</company>

Usage Examples:

# simple validation
$ xmllint --valid example.xml

# validation but without result tree
$ xmllint --valid --noout example.xml

# validation agains a specific DTD schema
$ xmllint --noout --dtdvalid <URL> example.xml

# again RelaxNG schema
$ xmllint --relaxng <schema> example.xml

# again WXS schema
$ xmllint --schema <schema> example.xml

# again schematron
$ xmllint --schematron <schema> example.xml

Query with xmllint

$ xmllint --shell example.xml

# with cat
/ > cat //name
 -------
<name status="on">Develop</name>
 -------
<name status="off">Sales</name>
 -------
<name status="on">QA</name>
 -------
<name status="on">Develop</name>
 -------
<name status="off">Consultant</name>

# with cat and xpath 
/ > cat //*[contains(*,"Consultant")] 
 -------
<team id="032">
	<name status="off">Consultant</name>
	<size>3</size>
</team>

# with grep
/ > grep QA
/company/branch[2]/team[2]/name : ta-        2 QA

# exit
/ > exit

XPath with xmllint

# by root element
$ xmllint --xpath //company example.xml

# by child elements
$ xmllint --xpath //name example.xml 

# by specific child element
$ xmllint --xpath //branch[3] example.xml

# by last child element
$ xmllint --xpath '/company/branch[last()]' example.xml

# path expression
$ xmllint --xpath 'child::*/child::*/team' example.xml

# by attribute
$ xmllint --xpath '//branch[@lang="de-CH"]' example.xml