red guardgen
red guardgen

Reputation: 43

How to adjust xslt template match

Here is fragment of html content to be transformed

<div id="ln11413-1665">
<h4 class="niv2">Bijlage  5.51.3 </h4>
<div>
    <div class="comblok">
        <div style="color:gray;font-size:smaller;margin-left:20px;">
            <div class="com comhis">
                <b>
                    <div class="comthis">Wetshistoriek</div>
                </b>
                <div>Bijlage 5.51.3 vervangen bij art.&#160;10 B. Vl. Reg. 6&#160;februari 2004 (<i>B.S.</i>, 1&#160;april 2004 (eerste uitg.)), met ingang van 1&#160;april 2004 (art.&#160;12).</div>
            </div>
        </div>
    </div>
</div>
<div id="ln11413-2443"> 
...
    <div class="historicalVersions" style="display:none">
        <a href="ln11413#ln11413-2443" class="curmod">23 februari 2017</a>
        <a href="ln11413_20170217#ln11413-2443" class="histmod">24 september 2014</a>
        <a href="ln11413_20140922#ln11413-2443" class="histmod">30 juni 2006</a>
    </div>
    ...

Currently I've got template which looks like this:

<xsl:template match="xhtml:h4[@class='artikel']">
    <xsl:variable name="currentAnchor">
      <xsl:value-of select="../@id" />
    </xsl:variable>
    <div class="row article title-bar">
      <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates />
      </xsl:copy>
      <xsl:call-template name="AddHistoricAndFutureLawLinks">
        <xsl:with-param name="currentAnchor" select="$currentAnchor"/>
      </xsl:call-template>
    </div>
  </xsl:template>

And there is outcome(sorry for putting image): AddHistoricAndFutureLawLinks template generates div with "historicalversionsLink titleLinks" class

But it needs to be more generalized. Here are rules:

  1. If there is an h4 with the "Artikel" class, we do not change anything, let it be rendered as it was - right after it as it is shown on image
  2. If there is no h4 with the "Artikel" class, see if there is a div whichs id starts with "ln" and which has a descendant div with the "historicalversions" class
  3. In the same way, we shove it(AddHistoricAndFutureLawLinks template) into an existing div with the “title bar” class(just like "row article title-bar") if there is any h4(after it), otherwise put it right after "title bar" div

Upvotes: 0

Views: 72

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167696

There is no context given in which you are using your code or that particular element, I would certainly in most cases implement 'If there is an h4 with the "Artikel" class, we do not change anything, let it be rendered as it was' by not matching on xhtml:h4[@class='artikel'] at all and rather let the identity transformation (e.g. declared in XSLT 3 as <xsl:mode on-no-match="shallow-copy"/> or implemented as a template in XSLT 2 or 1) copy it and everything else you want to copy.

Of course you can also use e.g. <xsl:template match="xhtml:h4[@class='artikel']"><xsl:copy-of select="."/></xsl:template> if you have no identity transformation set up or want to handle only that particular element.

The other requirements like 'If there is no h4 with the "Artikel" class' are rather vague or at least need some context as to where to check and where to add new contents.

Upvotes: 1

Related Questions