XML technologies / XSpec / Initials and Regression Tests
A stylesheet should wrap the first character of a paragraph in a span with class initial. That is easy with a plain-text paragraph. It gets more interesting when there is an em element in the middle, or several text nodes. That is the point of the initial-letter exercise from our training material.
The bug is not a difficult calculation. It is an XPath pattern that selects too much. Testing only a plain-text paragraph would miss it.
What should the output look like?
For this example, the rule is deliberately small: mark the first character of the first direct text child of each unnamespaced p. Keep everything else, including attributes, comments and inline markup.
This input contains several text nodes:
examples/02-initial/fixtures/multiple-text.xml
<p>Before <em>inside</em> after.</p>Here is the expected output:
examples/02-initial/expected/multiple-text.xml
<p><span class="initial">B</span>efore <em>inside</em> after.</p>The word "direct" matters. If a paragraph starts with an inline element, the text inside that element is left alone. If the first direct text child starts with a space, that space is wrapped. This is a focused exercise, not a finished typesetting solution for paragraph initials.
The character operation also works on a codepoint, while a visible character can contain several codepoints. Keeping those together, skipping punctuation or finding the first visible letter inside leading inline markup would require additional rules and tests.
Make the test catch the bug
The defective version uses p//text(). That matches all descendant text nodes, including text inside em. An extra initial is then inserted there. The archived example shows this exact problem.
The download deliberately includes the defective stylesheet so that the tests can be tried against it:
python scripts/run-tests.py --only deliberate-failureThree cases are checked. Plain text should pass, while the two mixed-content cases should fail. The runner expects that specific outcome. A crash caused by a missing library would not be the intended result.
Correct the stylesheet
Using p/text()[1] selects only the first direct text child. Here is the corrected version:
examples/02-initial/initial.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no"/>
<!-- Preserve every node and attribute unless a specific rule applies. -->
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>
<!-- First code point of the first direct text child of each unnamespaced p. -->
<xsl:template match="p/text()[1]">
<span class="initial"><xsl:value-of select="substring(., 1, 1)"/></span>
<xsl:value-of select="substring(., 2)"/>
</xsl:template>
</xsl:stylesheet>The 2013 stylesheet uses xsl:analyze-string from XSLT 2.0. The reworked example uses substring operations available in XSLT 1.0. This also allows a direct check with libxslt. It does not change the requirements of XSpec itself.
The corresponding scenario loads the input and expected result from separate files:
examples/02-initial/initial.xspec
<x:scenario xmlns:x="http://www.jenitennison.com/xslt/xspec" label="Handle multiple text input">
<x:context href="fixtures/multiple-text.xml"/>
<x:expect label="Preserve the contract" href="expected/multiple-text.xml" select="/element()"/>
</x:scenario>Do not stop at the first passing test
The full suite has ten small cases: plain text, inline markup, multiple text nodes, an empty paragraph, attributes and a comment. It also covers leading inline markup, a leading space, a non-paragraph element and a paragraph in the XHTML namespace.
Each case answers a specific question. Are attributes preserved? Are other elements left alone? Is an XHTML paragraph treated differently from an unnamespaced p? That is more useful than ten nearly identical paragraphs.
The ten corrected transformations have already passed direct libxslt checks. The two intended mismatches in the defective version were also detected. The results are recorded in reports/local-checks.json. Running these same cases through XSpec remains outstanding in the current package.