2

我正在尝试使用 XSLT 将 XML 转换为 XHTML。我想用 Python 来做,但我并不特别喜欢任何库。按照这里的指示,我一直在尝试这个:

from lxml import etree

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

但我得到这个错误:

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

我也尝试使用这个 python 脚本,它使用 libxslt,但它给了我这些错误:

    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'

我正在使用的 XSL 文件在这里。它是专业创建的,所以我认为它不会有大问题。

有没有办法覆盖这个错误,以便我可以让我的 XML 文件在 python 中进行转换?或者有没有一种不同的方法来做这个 XSLT,我不会一直出错?在浏览器(Firefox)中转换工作正常,但我无法让它在 Python 中工作。

4

1 回答 1

4

恐怕你的承包商让你失望了。XSLT 规范在第 12.2 节中说明了这一点:

使用属性或匹配属性的值包含变量引用是错误的。

所以第key283 到 288 行中的元素editions.xsl不是有效的 XSLT,因为它们的match属性使用了路径 step tei:rdg[contains(@wit,$rdg)]

幸运的是,$rdg在第 183 行定义了一个简单的常量为 be 'lemma',因此如果您将所有六个出现的路径步骤改为tei:rdg[contains(@wit,'lemma')]改为,它应该都适合您。

于 2013-05-19T21:52:26.607 回答