# Import elements of XML-Files in LyX/Latex

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.

-
Did you try xslt? – Alex Jun 20 '13 at 20:41
@Alex: Thanks. I created a XSL-File which converts my XML into a HTML structure which comes pretty close to what I want. <xsl:template match="/"> <html> <body> <xsl:for-each select="model/dims/dim"> <h2><xsl:value-of select="@name"/></h2> <p><xsl:value-of select="def"/></p> <xsl:for-each select="lvls/lvl"> <h3><xsl:value-of select="@name"/></h3> <p><xsl:value-of select="def"/></p> </xsl:for-each> </xsl:for-each> </body> </html> </xsl:template> Now, how do I do the same in Latex/LyX? There is no browser to process XSL in between. – BackfromHell Jun 24 '13 at 21:43

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?

-
Sorry, I just answered my own question. It looks quite the same, but yours looks more polished, so I accepted yours. I can confirm it works! Thanks! – BackfromHell Jun 25 '13 at 20:58

The final link to the solution was xsltproc.

First I created a XSL with latex-commands inside:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/">
<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:for-each select="hiers/hier">
<xsl:text>\subsection*{</xsl:text>
<xsl:value-of select="@name"/>
<xsl:text>}</xsl:text>
<xsl:value-of select="def"/>
</xsl:for-each>
<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:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


Note that there is no \begin{document} or \end{document} because I used the input function of LyX in another sub-document (which is already an included document). To create a standalone tex-file those commands need to be added.

Second, I created a tex file with xsltproc:

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


It gave me a unable to parse output.tex, but it still worked.

Third, I included this output.tex in LyX. It works!

-