I would like to generate an xml using an input xml and a xslt file. I have a sample input xml and xslt files here <https://xsltfiddle.liberty-development.net/aiyned/1>. And the final output is what I would like.
Basically the input.xml is:
<?xml version="1.0" encoding="utf-8" ?> <root> <parent1 value="parent1"> <inserthere value="parent1"/> </parent1> <parent2 value="parent2"> <inserthere value="parent2"/> </parent2> </root> input.xslt is:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml"/> <!-- <xsl:variable name="childDoc" select="document('child.xml')"/> --> <xsl:variable name="childDoc"> <root> <child1 value="child1"/> <child2 value="child2"/> </root> </xsl:variable> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="inserthere"> <xsl:variable name="currentParent" select="."/> <xsl:for-each select="$childDoc/root/node()"> <xsl:copy> <xsl:attribute name="value" select="concat($currentParent/@value,'_',@value)"/> </xsl:copy> </xsl:for-each> </xsl:template> </xsl:stylesheet> Output xml is:
<?xml version="1.0" encoding="UTF-8"?><root> <parent1 value="parent1"> <child1 value="parent1_child1"/> <child2 value="parent1_child2"/> </parent1> <parent2 value="parent2"> <child1 value="parent2_child1"/> <child2 value="parent2_child2"/> </parent2> </root> The problem is the site is using saxon engine, which I believe could require a license. I would like to generate the output xml using lxml or any python library that is free. Currently when I run
import lxml.etree as ET dom = ET.parse("input.xml") xslt = ET.parse("input.xslt") transform = ET.XSLT(xslt) newdom = transform(dom) print(ET.tostring(newdom, pretty_print=True)) I get an error of:
newdom = transform(dom) File "src/lxml/xslt.pxi", line 602, in lxml.etree.XSLT.__call__ lxml.etree.XSLTApplyError: Failed to evaluate the 'select' expression. I think the problem is lxml only support version 1.0? There are some comments here how to use saxon use saxon with python, but I would like to prevent requiring java or other external applications. Is there a way to make the above work with just python? Or is there a way to update my xslt file so that it will work with just lxml transform function?
childDocnodes actually? Are they in a separate XML document, or listed in the XSLT? It makes a difference in XSLT 1.0.