0

I can’t figure out a very simple thing!

I am trying to write a template for transforming definition lists.

<list type="gloss">
  <head>Slovníček pojmů</head>
  <label xml:lang="cs">Pojem</label>
  <item>Dojem!</item>
  <label xml:lang="cs">Stavba</label>
  <item>Stavení</item>
</list>

current template:

<xsl:template match="tei:list[@type='gloss']">
    <div class="glossary">
        <p>
            <b>
                <xsl:apply-templates select="tei:head"/>
            </b>
        </p>
        <dl>
            <xsl:choose>
                <xsl:when test="tei:label">
                    <dt>
                        <xsl:apply-templates select="tei:label"/>
                    </dt>
                </xsl:when>
                <xsl:otherwise>
                    <dd>
                        <xsl:apply-templates select="tei:item"/>
                    </dd>
                </xsl:otherwise>
            </xsl:choose>
        </dl>
    </div>
</xsl:template>

Nothing works. I have tried for-each looping, which makes problems because of applying templates to an atomic values. External templates (outside of this one) usually render the head tag in a wrong way (twice). Is there any simple way how to do this?

The template above throws error mentioning there are too many nested calls for templates (the stylesheet may be looping).

Mathias Müller
  • 22,203
  • 13
  • 58
  • 75
Honza Hejzl
  • 874
  • 8
  • 23
  • 4
    Can you show more of your XML and XSLT please? Your XSLT refers to the namespace prefix `tei` but you have not shown the namespace declaration for this. Additionally, your XML does not have any namespaces at all, so it would not be matched by `tei:list`. You should probably also show the templates that match `head`, `label` and `item` too, to allow us to reproduce the error. Thanks! – Tim C Jan 18 '16 at 13:09

1 Answers1

1

This should work with the snippet you have posted:

<xsl:template match="list[@type='gloss']">
    <div class="glossary">
        <p>
            <b>
                <xsl:value-of select="head"/>
            </b>
        </p>
        <dl>
            <xsl:for-each select="label">
                <dt>
                    <xsl:value-of select="."/>
                </dt>
                <dd>
                    <xsl:value-of select="following-sibling::item[1]"/>
                </dd>
            </xsl:for-each>
        </dl>
    </div>
</xsl:template>

Or, if you prefer:

<xsl:template match="list[@type='gloss']">
    <div class="glossary">
        <xsl:apply-templates select="head"/>
        <dl>
            <xsl:apply-templates select="label | item"/>
        </dl>
    </div>
</xsl:template>

<xsl:template match="head">
    <p>
        <b>
            <xsl:value-of select="."/>
        </b>
    </p>
</xsl:template>

<xsl:template match="label">
    <dt>
        <xsl:value-of select="."/>
    </dt>
</xsl:template>

<xsl:template match="item">
    <dd>
        <xsl:value-of select="."/>
    </dd>
</xsl:template>

Result

<div class="glossary">
  <p>
    <b>Slovníček pojmů</b>
  </p>
  <dl>
    <dt>Pojem</dt>
    <dd>Dojem!</dd>
    <dt>Stavba</dt>
    <dd>Stavení</dd>
  </dl>
</div>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • It should not work! In the template provided, there is no dl tag. With it, it is problem to separate the head and put it above the whole dl list. Thank you, anyway! – Honza Hejzl Jan 18 '16 at 13:41
  • @HonzaHejzl I am not sure what you mean by that. Is the result shown in my answer different from the result you expect? – michael.hor257k Jan 18 '16 at 13:43
  • 1
    @HonzaHejzl It is easier to answer the question if you show a _complete_ example that we can run ourselves. For XSLT questions, this means: an XML input document, an XSLT stylesheet and the XML output you expect. See http://stackoverflow.com/help/mcve. – Mathias Müller Jan 18 '16 at 13:57
  • Michael, in your stylesheet, there is no DL _wrapper_ for the whole list (above it there should be the head). If I add the dl tag there, the problem is the head is put **inside** the list. Of course, Mathias, [the file](http://46.28.111.241:8081/exist/rest/db/custom_jh/bukwor.xml), [sheet 1](http://46.28.111.241:8081/exist/rest/db/custom_jh/xslt/style-web.xsl), [sheet 2](http://46.28.111.241:8081/exist/rest/db/custom_jh/xslt/body-web.xsl), [attrset](http://46.28.111.241:8081/exist/rest/db/custom_jh/xslt/attrsets-web.xsl) (just for tables). – Honza Hejzl Jan 18 '16 at 14:14
  • @HonzaHejzl Oh, you mean in the second version. I have added it now. – michael.hor257k Jan 18 '16 at 14:24
  • Thanks a lot, it works now! The `select="label | item"` does the trick. Marked as answered! – Honza Hejzl Jan 18 '16 at 14:42