2

I'm trying to transform XML to XHTML using XSLT. I'd like to do it using Python, but I'm not particularly attached to any library. Following the directions here, I've been trying this:

from lxml import etree

xslt_root=etree.parse('editions.xsl')
transform=etree.XSLT(xslt_root)

But I get this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "xslt.pxi", line 406, in lxml.etree.XSLT.__init__ (src/lxml/lxml.etree.c:136874)
lxml.etree.XSLTParseError: Forbidden variable

I also tried using this python script, which uses libxslt, but it gives me these errors:

    Forbidden variable
compilation error: file editions.xsl line 283 element key
xsl:key : 'match' pattern compilation failed '//tei:div/tei:ab/tei:ptr[@type='emendation'][@ana='#folger'] |// tei:rdg[contains(@wit,$rdg)]/tei:ptr[@type='emendation'][@ana='#folger']'
Forbidden variable
compilation error: file editions.xsl line 284 element key
xsl:key : 'match' pattern compilation failed '//tei:div/tei:ab/tei:ptr[@type='emendation'][@ana='#folger'] |// tei:rdg[contains(@wit,$rdg)]/tei:ptr[@type='emendation'][@ana='#folger']'
Forbidden variable
compilation error: file editions.xsl line 285 element key
xsl:key : 'match' pattern compilation failed '//tei:div/tei:ab/tei:ptr[@type='emendation'][@ana='#texta'] |// tei:rdg[contains(@wit,$rdg)]/tei:ptr[@type='emendation'][@ana='#texta']'
Forbidden variable
compilation error: file editions.xsl line 286 element key
xsl:key : 'match' pattern compilation failed '//tei:div/tei:ab/tei:ptr[@type='emendation'][@ana='#texta'] |// tei:rdg[contains(@wit,$rdg)]/tei:ptr[@type='emendation'][@ana='#texta']'
Forbidden variable
compilation error: file editions.xsl line 287 element key
xsl:key : 'match' pattern compilation failed '//tei:div/tei:ab/tei:ptr[@type='emendation'][@ana='#textb'] |// tei:rdg[contains(@wit,$rdg)]/tei:ptr[@type='emendation'][@ana='#textb']'
Forbidden variable
compilation error: file editions.xsl line 288 element key
xsl:key : 'match' pattern compilation failed '//tei:div/tei:ab/tei:ptr[@type='emendation'][@ana='#textb'] |// tei:rdg[contains(@wit,$rdg)]/tei:ptr[@type='emendation'][@ana='#textb']'
Traceback (most recent call last):
  File "transform.py", line 21, in <module>
    result = style.applyStylesheet(doc, None)
AttributeError: 'NoneType' object has no attribute 'applyStylesheet'

The XSL file I'm using is here. It's professionally created, so I wouldn't think there would be big problems with it.

Is there a way to override this error, so that I can get my XML file to transform in python? Or is there a different way to do this XSLT where I won't be getting errors all the time? Transforming in the browser (Firefox) works fine, but I can't get it to work in Python.

Community
  • 1
  • 1
Jonathan
  • 10,571
  • 13
  • 67
  • 103
  • Additional error detail can be found `.error_messages`[See: errors-and-message](https://lxml.de/xpathxslt.html#errors-and-messages) – dank8 Feb 20 '23 at 14:32

1 Answers1

4

I'm afraid your contractor has let you down. The XSLT specification says this in section 12.2:

It is an error for the value of either the use attribute or the match attribute to contain a VariableReference.

So the key elements in lines 283 to 288 of editions.xsl are not valid XSLT because their match attributes use the path step tei:rdg[contains(@wit,$rdg)].

Fortunately, $rdg is a simple constant defined at line 183 to be 'lemma', so if you change all six occurrences of this path step to tei:rdg[contains(@wit,'lemma')] instead, it should all work for you.

Borodin
  • 126,100
  • 9
  • 70
  • 144
  • How did you find that error? Do you know of a useful Validator? – Matthew Wilcoxson May 23 '13 at 17:06
  • @MatthewWilcoxson: Well I looked at your Python error log that says there is a `Forbidden variable` error on each line 283 to 288. Then I looked at line 283 of the file to find the forbidden variable. That seems to be all the validation you should need. – Borodin May 23 '13 at 17:27
  • Thanks @Borodin, I hadn't noticed the error output above, was hoping you had use some superduper validator to find it. ;) – Matthew Wilcoxson May 24 '13 at 11:36