3

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.

2
  • 1
    Did you try xslt?
    – Alex
    Jun 20, 2013 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. Jun 24, 2013 at 21:43

2 Answers 2

5

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?

1
  • 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! Jun 25, 2013 at 20:58
0

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!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .