0
votes

Hello community, unfortunately I have a problem with an XSLT or the Python XML parser lxml. I have a DTD validated XML and I want to use an XSLT to find all IDREF of an ID . Below is a sample XML and the XSLT.

XML 1.0

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE TEST
[
<!ELEMENT TEST (N*,SUB*)>
<!ELEMENT N (#PCDATA)>
<!ELEMENT SUB (E*)>
<!ELEMENT E (#PCDATA)>
<!ATTLIST TEST

>
<!ATTLIST SUB

>
<!ATTLIST N
    Id                  ID          #REQUIRED
    X                   CDATA       #REQUIRED
>
<!ATTLIST E
    EID                 ID          #REQUIRED
    N1                  IDREF       #REQUIRED
    N2                  IDREF       #REQUIRED
>
]>
<TEST>
    <N Id="N1" X="0.0"/>
    <N Id="N2" X="1.0"/>
    <N Id="N3" X="2.0"/>
    <N Id="N4" X="3.0"/>
    <SUB>
        <E EID="E1" N1="N1" N2="N2"/>   
        <E EID="E2" N1="N1" N2="N3"/>
        <E EID="E3" N1="N1" N2="N4"/>
    </SUB>
</TEST>

XSLT 1.0 (

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:output method="xml"/>
    <xsl:strip-space elements="*"/>
    
<xsl:template match="//N">
    <xsl:element name="NREF"> 
        <xsl:for-each select="**idref('N1')**">
                <xsl:value-of select="name()"/>
        </xsl:for-each>

        
    </xsl:element>
</xsl:template> 

</xsl:stylesheet>

My example python script parses the XML and the XSLT, transforms the XML and writes the result to a new file.

Python 3.9

import lxml
from lxml import etree
import xml.etree.ElementTree as ET 

xml_parser = lxml.etree.XMLParser(  attribute_defaults=True, dtd_validation =True, no_network=False)
xml_root  = lxml.etree.parse('XML.xml', parser=xml_parser)
xslt_root = lxml.etree.parse('id_idref_test.xslt')
transform = etree.XSLT(xslt_root)
result_tree = transform(xml_root)
result_tree.write(f"XML_Result.xml",method="xml",pretty_print=True)

Now about my problem. Xpath supports the functions id() and idref(). When I use the id('N1') function in the loop I get the following result.<NREF>N</NREF>. If I use the idref('N1') function I get an error from the parser. lxml.etree.XSLTApplyError: Failed to evaluate the 'select' expression. Is the idref() function not implemented in the lxml scope?

I hope I can explain my problem in sufficient detail. Thanks for the help.

1
The idref() function requires XPath/XSLT 2.0 or higher. - michael.hor257k
Hi Mr michael.hor257k, thanks for the quick reply. However, the id() function is present in Xpath 1.0, do I see that correctly? - Görgen
Yes, the id() function is part of XPath 1.0. But the idref() function is not. You say that you get an error when using the idref() function, but not when using the id() function - just as expected. - michael.hor257k
Ah okay. I understood that then. thanks for the help - Görgen
I would suggest you use the key mechanism to resolve cross-references. This works in XSLT 1.0 and does not require a DTD. - michael.hor257k

1 Answers

0
votes

The idref() function requires XPath/XSLT 2.0 or higher. Thanks to michael.hor257k