XML

Commandline tools

Most Linux distributions have a host of commandline tools, readily installed, that are very useful when you're developing a system that uses XML for something. For example, you're testing transformation of an XML datagram to HTML:

    xsltproc stylesheet.xsl datagram.xml > rendered.html

Shell scripting

To get values from an XML, we first create a generic stylesheet with an XPath expression in it that's actually a parameter:

  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text"/>
    <xsl:template match="/">
      <xsl:value-of select="$path"/>
    </xsl:template>
  </xsl:stylesheet>

Then we run it against an XML file:

  $ xsltproc --param path "/path/to/something" getpath.xsl test.xml

To get the value in a shellscript variable:

  #!/bin/sh
  SOMETHING=`xsltproc --param path "/path/to/something" getpath.xsl test.xml`
  echo "Something: $SOMETHING"