i have this XML:
<AAA>
<BBB>
<CCC>
<DDD/>
</CCC>
<CCC>
<EEE/>
</CCC>
</BBB>
<BBB>
<CCC>
<EEE/>
</CCC>
<CCC>
<DDD/>
</CCC>
</BBB>
<BBB>
<CCC>
<EEE/>
</CCC>
<CCC>
<EEE/>
</CCC>
</BBB>
<BBB>
<CCC>
<DDD/>
</CCC>
<CCC>
<EEE/>
</CCC>
</BBB>
</AAA>
And i want this result using a XSLT file:
<AAA>
<XXX case="1" />
<XXX case="2" />
<XXX case="3" />
<XXX case="1" />
</AAA>
Case 1: CCC[1] has DDD & CCC[2] has EEE
Case 2: CCC[1] has EEE & CCC[2] has DDD
Case 3: CCC[1] has EEE & CCC[2] has EEE
CCC always has only one item
I use the lxml Library in Python. I already got a XPath that matches case="1" in Notepad++:
//BBB/CCC[1]/DDD/../../CCC[2]/EEE
But if i try to use
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="BBB/CCC[1]/DDD/../../CCC[2]/EEE">
<XXX case="1" />
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I get this Error:
Traceback (most recent call last):
File "C:/.../xsltest.py", line 11, in <module>
transform_01 = etree.XSLT(xslt_01)
File "xslt.pxi", line 412, in lxml.etree.XSLT.__init__ (src\lxml\lxml.etree.c:152223)
lxml.etree.XSLTParseError: Cannot parse stylesheet
If i only use BBB/CCC[1]/DDD the match works but it does not with the "..". How can i get the parent? Or is there another solution to my problem?
Waldi
Use this XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="BBB">
<xsl:choose>
<xsl:when test="CCC[1]/DDD and CCC[2]/EEE">
<XXX case="1"/>
</xsl:when>
<xsl:when test="CCC[1]/EEE and CCC[2]/DDD">
<XXX case="2"/>
</xsl:when>
<xsl:when test="CCC[1]/EEE and CCC[2]/EEE">
<XXX case="3"/>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.