Skip to content

Instantly share code, notes, and snippets.

@bitsgalore
Created April 13, 2016 15:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitsgalore/3e403b02f776f03444c0622cb3b08b56 to your computer and use it in GitHub Desktop.
Save bitsgalore/3e403b02f776f03444c0622cb3b08b56 to your computer and use it in GitHub Desktop.
Namespace handling in xmllint

The problem

When used from the command line, the xmllint tool doesn't accept namespaces in xpath expressions. This makes it difficult to process XML documents like the one below (file demo.xml):

<?xml version="1.0" standalone="yes"?>
<svrl:schematron-output xmlns:svrl="http://purl.oclc.org/dsdl/svrl" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:schold="http://www.ascc.net/xml/schematron" xmlns:sch="http://www.ascc.net/xml/schematron" xmlns:iso="http://purl.oclc.org

/dsdl/schematron" title="" schemaVersion="">
  <!--   
           
           
         -->
  <svrl:active-pattern/>
  <svrl:fired-rule context="/"/>
   <svrl:fired-rule context="/preflight/errors/error"/>
  <svrl:fired-rule context="/preflight/errors/error"/>
  <svrl:failed-assert test="code != '3.1.3'" location="/preflight/errors/error[2]">
    <svrl:text>Error on the "Font File x" in the Font Descriptor</svrl:text>
  </svrl:failed-assert>
  <svrl:fired-rule context="/preflight/errors/error"/>
  <svrl:failed-assert test="code != '3.1.3'" location="/preflight/errors/error[8]">
    <svrl:text>Error on the "Font File x" in the Font Descriptor</svrl:text>
  </svrl:failed-assert>
  <svrl:fired-rule context="/preflight/errors/error"/>
  <svrl:failed-assert test="code != '3.1.9'" location="/preflight/errors/error[9]">
    <svrl:text>The CIDToGID is invalid</svrl:text>
  </svrl:failed-assert>
  <svrl:failed-assert test="code != '3.1.11'" location="/preflight/errors/error[14]">
    <svrl:text>The CIDSet entry i mandatory from a subset of composite font</svrl:text>
  </svrl:failed-assert>
</svrl:schematron-output>

Solution

Solution B from Stack Overflow below:

http://stackoverflow.com/a/8266075/1209004

The example there isn't particularly clear, which is why I'm providing some additional examples based on my own example above.

Example 1: get all 'failed-assert' elements

xmllint --xpath "//*[local-name()='schematron-output']/*[local-name()='failed-assert']" demo.xml

Result:

<svrl:failed-assert test="code != '3.1.3'" location="/preflight/errors/error[2]">
    <svrl:text>Error on the "Font File x" in the Font Descriptor</svrl:text>
  </svrl:failed-assert><svrl:failed-assert test="code != '3.1.3'" location="/preflight/errors/error[8]">
    <svrl:text>Error on the "Font File x" in the Font Descriptor</svrl:text>
  </svrl:failed-assert><svrl:failed-assert test="code != '3.1.9'" location="/preflight/errors/error[9]">
    <svrl:text>The CIDToGID is invalid</svrl:text>
  </svrl:failed-assert><svrl:failed-assert test="code != '3.1.11'" location="/preflight/errors/error[14]">
    <svrl:text>The CIDSet entry i mandatory from a subset of composite font</svrl:text>
  </svrl:failed-assert>

Example 2: get all 'test' attributes

xmllint --xpath "//*[local-name()='schematron-output']/*[local-name()='failed-assert']/@test" demo.xml

Result:

test="code != '3.1.3'" test="code != '3.1.3'" test="code != '3.1.9'" test="code != '3.1.11'"

Example 3: get all 'text' elements

xmllint --xpath "//*[local-name()='schematron-output']/*[local-name()='failed-assert']/*[local-name()='text']" demo.xml

Result:

<svrl:text>Error on the "Font File x" in the Font Descriptor</svrl:text><svrl:text>Error on the "Font File x" in the Font Descriptor</svrl:text>
<svrl:text>The CIDToGID is invalid</svrl:text><svrl:text>The CIDSet entry i mandatory from a subset of composite font</svrl:text>

Example 4: get text from all 'text' elements

xmllint --xpath "//*[local-name()='schematron-output']/*[local-name()='failed-assert']/*[local-name()='text']/text()" demo.xml

Result:

Error on the "Font File x" in the Font DescriptorError on the "Font File x" in the Font DescriptorThe CIDToGID is invalidThe CIDSet 
entry i mandatory from a subset of composite font
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment