# A few XSL questions.

 Subject: A few XSL questions. From: Michel Goossens Date: Thu, 22 Oct 1998 13:26:25 +0100 (MET)
Below there are three questions regarding XSL syntax (or my lack of
understanding of it). It would be nice if somebody could explain whether
what I want to do is feasible, possible, or not foreseen in the current
implementations/draft (I use J.Clark's xt). Many thanks. Michel Goossens

Question 1: how to get at the parent node in a pattern?

Suppose I have in my DTD something like:

<!ENTITY % sect          "stitle, (%likepara;)* ">
<!ELEMENT stitle         (#PCDATA)>
<!ELEMENT chapter        (%sect;, section*)>
<!ELEMENT section        (%sect;, subsection*)>
<!ELEMENT subsection     (%sect;, subsubsection*)>
etc.

Now I would like to make a generic template that generates
a title for each sect element type, but calls a different
title routine (e.g., in LaTeX).

I can of course go:

<xsl:template match="chapter/stitle">
<xsl:text>\chapter{</xsl:text>
<xsl:process-children/>
<xsl:text>}{</xsl:text>
</xsl:template>
<xsl:template match="section/stitle">
<xsl:text>\section{</xsl:text>
<xsl:process-children/>
<xsl:text>}{</xsl:text>
</xsl:template>
<xsl:template match="subsection/stitle">
<xsl:text>\subsection{</xsl:text>
<xsl:process-children/>
<xsl:text>}{</xsl:text>
</xsl:template>
...

but i'd rather do something more generic, like

<xsl:template match="stitle">
<xsl:choose>
<xsl:when  test="..[chapter]">
<xsl:text>\chapter{</xsl:text>
</xsl:when>
<xsl:when  test="..[section]">
<xsl:text>\section{</xsl:text>
</xsl:when>
<xsl:when  test="..[subsection]">
<xsl:text>\subsection{</xsl:text>
</xsl:when>
...
</xsl:choose>
<xsl:process-children/>
<xsl:text>}
</xsl:text>
</xsl:template>

However, this syntax, although "inspired" by the
example for <xsl:if test=.[not-last-of-type()]"...
in section 2.7.10.1 of the XSL draft (June 19th edition)
does not work. How can I get the name of the parent in a pattern?

What would be _really_ cool is when I could just put the parent's element
type (say it is XXX, where XXX is chapter, section,...)
Then I could write
<xsl:template match="stitle">
<xsl:text>\</xsl:text> element_type(..)
<xsl:text>{</xsl:text>
<xsl:process-children/>}
<xsl:text>}</xsl:text>
</xsl:template>

where element_type(..) would generate the name of the element type
of the parent.

Question 2: looping over attributes
===========

Suppose I have something like:

<includegraphics width="12cm" scale=".25" name="myfig.eps">

and I would like to obtain (in a more or less "generic" way)

\includegraphics[width=12cm, scale=.25]{myfig.eps}

where there are many more attributes possible.
Now I explicitly enumerate all attributes and get their value "by hand".
If I had a function which could loop over all attributes for a
given element, and return the attribute name and its value, such
programming could be simplified.

This is what I do now. The attributes can be specified in any order .
I did not yet find a way to get rid of the dangling comma, which I need
as a separator in my output format.

<xsl:template match="includegraphics">
<xsl:text>\includegraphics[</xsl:text>
<xsl:if test="attribute(width)"><xsl:text>width=</xsl:text>
<xsl:value-of expr="attribute(width)"/><xsl:text>, </xsl:text></xsl:if>
<xsl:if test="attribute(height)"><xsl:text>height=</xsl:text>
<xsl:value-of expr="attribute(height)"/><xsl:text>, </xsl:text></xsl:if>
<xsl:if test="attribute(bb)"><xsl:text>bb="</xsl:text>
<xsl:value-of expr="attribute(bb)"/><xsl:text>, </xsl:text></xsl:if>
<xsl:if test="attribute(angle)"><xsl:text>angle=</xsl:text>
<xsl:value-of expr="attribute(angle)"/><xsl:text>, </xsl:text></xsl:if>
<xsl:if test="attribute(scale)"><xsl:text>scale=</xsl:text>
<xsl:value-of expr="attribute(scale)"/><xsl:text>, </xsl:text></xsl:if>
<xsl:text>]{</xsl:text><xsl:value-of expr="attribute(name)"/>}<xsl:text>
</xsl:text>
</xsl:template>

It would be nicer is there were something like:
<xsl:template match="includegraphics">
<xsl:text>\includegraphics[</xsl:text>
<xsl:foreach select="(attributes[.])">

do something according to the name:
.... attribute_name()=attribute_value() ...

<xsl:text>{</xsl:text><xsl:value-of expr="attribute(name)"/>}<xsl:text>}
</xsl:text>
</xsl:template>

Question 3: Getting at the name of the matched element.
===========

I would like to know the exact name of the element which matched (noted
XXXX in the example below, where XXXX=a1, a2, or a3, depending on which of
them occurred in the XML source), e.g.,

<xsl:template match="a1|a2|a3">
<xsl:text>
\begin{XXXX}
</xsl:text>
<xsl:process-children/>
<xsl:text>
\end{XXXX}
</xsl:text>
</xsl:template>

So, if I have:
... <a1>some text</a1>..
Then I would like this to be transformed into
... \begin{a1}some text\end{a1} (and similarly for a2 and a3).
This would make a lot of the code a lot shorter.

+-------------------------------------------------------------+
| Dr. Michel Goossens          Phone: (+41 22) 767-5028 (NEW) |
| IT Division                  Fax  : (+41 22) 767-8630       |
| CH-1211 Geneva 23            Email: michel.goossens@xxxxxxx |
| Switzerland                                                 |
+-------------------------------------------------------------+

XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



### PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!