[Tex/LaTex] Import elements of XML-Files in LyX/Latex

lyxxml

I have a software which creates XML-Files with a structure looking like this:

<model>
<dims>
    <dim type="attr" name="foo">
        <def>Insert Definition of the Dimension 'foo'</def>
        <hiers>
            <hier name="foo">
                <def>Insert Definition of the Hierarchy 'foo'</def>
                <lvlrol lname="bar"/>
            </hier>
        </hiers>
        <lvls>
            <lvl tag="bar" name="bar">
                <def>Insert Definition of the Level 'bar'</def>
            </lvl>
        </lvls>
    </dim>
    <dim type="attr" name="baz">
        <def>Definition of baz</def>
        [...]
    </dim>
</dims>
</model>

Now I want to import the definitions (<def>) from every <dim> and every <hier> in Lyx, or at least in Latex.

The Lyx-Document should have a (sub-)chapter for every <dim> in which all corresponding <hier>s are explained. These chapters shall solely be created from the definitions of the XML-File.

How do I do that? I know how to put a whole XML-File into Latex ( Including XML file into LaTeX ) but I just want to import all definitions in a row.

Best Answer

With your xsl file you're translating from xml to html. But you can also translate to plain ascii text, or to latex using the using the <xsl:text> tag. Here's the corresponding xsl file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="html" omit-xml-declaration="yes" />

  <xsl:template match="/">
<xsl:text>
\documentclass{article}
\begin{document}
</xsl:text>
    <xsl:for-each select="model/dims/dim"> 
<xsl:text>\section{</xsl:text><xsl:value-of select="@name"/><xsl:text>}
</xsl:text>
  <xsl:value-of select="def"/><xsl:text>
</xsl:text>
    <xsl:for-each select="lvls/lvl"> 
<xsl:text>\subsection{</xsl:text><xsl:value-of select="@name"/><xsl:text>}
</xsl:text>
  <xsl:value-of select="def"/><xsl:text>
</xsl:text>
    </xsl:for-each> </xsl:for-each> 
<xsl:text>
\end{document}
</xsl:text>
  </xsl:template>
</xsl:stylesheet>

You can process it on the command line, e.g., with xsltproc:

xsltproc -o output.tex template.xsl input.xml

Lyx I never really used. Does it support custom plugins or something like that?

Related Question