问题
I am trying to convert an XML document into LaTeX using XSLT. I have two tags <lin> and <gra> which, roughly speaking, correspond to a paragraph and a choice of text size, respectively. However, in the case where <lin> is run from within a <blok ryk="lyrik"> tag, I want <lin> to be applied identically to <lin ryk="lang"><gra str="-1">. I know I could just copy the definition of the latter, but for several reasons, the mentioned solution is to be preferred. I felt very lucky and decided to try the following code:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes" encoding="UTF-8"/>
<xsl:template match="/">
\documentclass{memoir}
\begin{document}
<xsl:apply-templates/>
\end{document}
</xsl:template>
<xsl:template match="lin[@ryk='lang']"><!--
-->{\parindent=3em <xsl:apply-templates/><xsl:text>}
</xsl:text><!--
--></xsl:template>
<xsl:template match="gra[@str='-1']">{\small <xsl:apply-templates/>}</xsl:template>
<xsl:template match="blok[@ryk='lyrik']/lin" priority="7">
<lin ryk="lang"><gra str="-1">
<xsl:apply-templates/>
</gra></lin>
</xsl:template>
This did not work, since the <lin ryk="lang"><gra str="-1"> code is completely ignored. It was perhaps a bit optimistic to hope that you could just simply write out the code this way. But then what should I have done? Here is a piece of example XML code to apply it to:
<?xml version="1.0" encoding="UTF-8"?>
<blok ryk="lyrik">
<lin>Attributes are red.</lin>
<lin>Tags are blue.</lin>
<lin>Text is black.</lin>
<lin>Please help my code compute.</lin>
</blok>
The output I get is simply the same without the tags.
\documentclass{memoir}
\begin{document}
Attributes are red.
Tags are blue.
Text is black.
Please help my code compute.
\end{document}
What I want is the following:
{\small\parindent=3em Attributes are red.}
{\small\parindent=3em Tags are blue.}
{\small\parindent=3em Text is black.}
{\small\parindent=3em Please help my code compute.}}
P.S. If someone uses <xsl:copy> in the solution, please explain to me what that tag really does. I've read countless tutorials and examples and still don't really get it.
回答1:
I think you want to create a variable
<xsl:template match="blok[@ryk='lyrik']/lin" priority="7">
<xsl:variable name="l1">
<lin ryk="lang"><gra str="-1"> <xsl:copy-of select="node()"/> </gra></lin>
</xsl:variable>
<xsl:apply-templates select="$l1/node()"/>
</xsl:template>
来源:https://stackoverflow.com/questions/24640254/apply-xslt-tags-that-arent-there