XPath collection() 函数是否适用于 lxml 和 XSLT?
Does the XPath collection () function work with lxml and XSLT?
我最近尝试使用 lxml 包和包含带有 XPath collection() 函数的变量的 XSL 样式表转换 XML 文件,但是当我 运行 我的代码:
lxml.etree.XSLTApplyError: Failed to evaluate the expression of variable 'name'.
以下是我的文件的详细信息:
- XML 来源:catalog.xml
<?xml version="1.0" encoding="UTF-8"?>
<collection>
<doc href="./IR_041698.xml"/>
<doc href="./IR_051379.xml"/>
</collection>
- XSL 文件:test.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:tei="http://www.tei-c.org/ns/1.0"
exclude-result-prefixes="tei">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes" method="xml"/>
<xsl:template match="/">
<xsl:variable name="name" select="collection('catalog.xml')/descendant::archdesc/did/origination/persname/text()"/>
<teiHeader xmlns="http://www.tei-c.org/ns/1.0">
<fileDesc>
<titleStmt>
<title>
<xsl:value-of select="$name"/>
</title>
</titleStmt>
</fileDesc>
</teiHeader>
</xsl:template>
</xsl:stylesheet>
- Python代码:
from lxml import etree as ET
source = ET.parse("catalog.xml")
xslt = ET.parse("test.xsl")
transform = ET.XSLT(xslt)
newdom = transform(source)
print(ET.tostring(newdom, pretty_print=True))
我有点惊讶,因为当我在 Oxygen XML 编辑器下启动转换时它有效但在 Python.
中无效
你有什么建议吗? XPath collection() 函数是 lxml 的问题吗?
提前致谢
collection
函数是 XPath 和 XSLT 2 及更高版本的一部分,因此不受 lxml 支持。但是,在 XSLT 中,您可以将 document
函数用作 document(document('catalog.xml')/*/doc/@href))
到 select 由 href
属性编辑的文档 select 的 "collection" catalog.xml 文档中的 doc
元素节点。
Saxon 9.9 也可以作为 Python 模块 https://www.saxonica.com/saxon-c/doc/html/saxonc.html as part of Saxon C 1.2.1 (Download http://saxonica.com/download/c.xml, documentation: http://www.saxonica.com/saxon-c/documentation/index.html) 使用,因此如果您想在 Python 中使用 XSLT 3,您可以考虑从 lxml 切换到 Saxon-C。 =16=]
我最近尝试使用 lxml 包和包含带有 XPath collection() 函数的变量的 XSL 样式表转换 XML 文件,但是当我 运行 我的代码:
lxml.etree.XSLTApplyError: Failed to evaluate the expression of variable 'name'.
以下是我的文件的详细信息:
- XML 来源:catalog.xml
<?xml version="1.0" encoding="UTF-8"?>
<collection>
<doc href="./IR_041698.xml"/>
<doc href="./IR_051379.xml"/>
</collection>
- XSL 文件:test.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:tei="http://www.tei-c.org/ns/1.0"
exclude-result-prefixes="tei">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes" method="xml"/>
<xsl:template match="/">
<xsl:variable name="name" select="collection('catalog.xml')/descendant::archdesc/did/origination/persname/text()"/>
<teiHeader xmlns="http://www.tei-c.org/ns/1.0">
<fileDesc>
<titleStmt>
<title>
<xsl:value-of select="$name"/>
</title>
</titleStmt>
</fileDesc>
</teiHeader>
</xsl:template>
</xsl:stylesheet>
- Python代码:
from lxml import etree as ET
source = ET.parse("catalog.xml")
xslt = ET.parse("test.xsl")
transform = ET.XSLT(xslt)
newdom = transform(source)
print(ET.tostring(newdom, pretty_print=True))
我有点惊讶,因为当我在 Oxygen XML 编辑器下启动转换时它有效但在 Python.
中无效你有什么建议吗? XPath collection() 函数是 lxml 的问题吗?
提前致谢
collection
函数是 XPath 和 XSLT 2 及更高版本的一部分,因此不受 lxml 支持。但是,在 XSLT 中,您可以将 document
函数用作 document(document('catalog.xml')/*/doc/@href))
到 select 由 href
属性编辑的文档 select 的 "collection" catalog.xml 文档中的 doc
元素节点。
Saxon 9.9 也可以作为 Python 模块 https://www.saxonica.com/saxon-c/doc/html/saxonc.html as part of Saxon C 1.2.1 (Download http://saxonica.com/download/c.xml, documentation: http://www.saxonica.com/saxon-c/documentation/index.html) 使用,因此如果您想在 Python 中使用 XSLT 3,您可以考虑从 lxml 切换到 Saxon-C。 =16=]