tags:

views:

729

answers:

2

I want to pull a series of relationships out of xml files and transform them into a graph I generate with dot. I can obviously do this with a scripting language, but I was curious whether or not this was possible with xslt. Something like:

xsltproc dot.xsl *.xml

which would produce a file like

diagraph {
 state -> state2
 state2 -> state3
 [More state relationships from *.xml files]
}

So I need to both 1) wrap the combined xml transforms with "diagraph {...}" and 2) be able to handle an arbitrary set of xml documents specified on the command line.

Is this possible? Any pointers?

+4  A: 

Using an XSLT 2.0 processor and the collection() function this is really easy.

Below is an example using Saxon:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:param name="pDirName" select="'D:/Temp/xmlFilesDelete'"/>

    <xsl:template match="/">
    <wrap>
        <xsl:apply-templates select=
         "collection(
              concat('file:///',
                      $pDirName,
                      '?select=*.xml;recurse=yes;on-error=ignore'
                       )
                   )/*
          "/>
      </wrap>
    </xsl:template>

    <xsl:template match="*">
      <xsl:copy-of select="."/>
    </xsl:template>
</xsl:stylesheet>

When this transformation is applied on any XML document (not used), it processes all xml files in the file-system subtree starting at the directory, whose value is specified by the global parameter $pDirName.

At the time this transformation was applied there were only two xml files there:

<apples>3</apples>

and

<oranges>3</oranges>

The correct result is produced:

<wrap>
   <apples>3</apples>
   <oranges>3</oranges>
</wrap>

This is the simplest example possible that can be constructed. To fully answer the question, the directory can be specified on the command line invoking Saxon. Read more about the ways to invoke Saxon from the command-line here.

Dimitre Novatchev
+2  A: 

You might want to look at (http://martin-loetzsch.de/DOTML/). It uses xslt to generate dot syntax from xml documents.

Martin Loetzsch