I've been struggeling to perform a transformation on a document which has identical node names and needs to be split up in specific nodes. Visually you could say it's transforming rows to columns. This is my source file:
<?xml version="1.0" encoding="UTF-8"?>
<Document>
<Car>
<Element>
<Question>Brand</Question>
<Answer>Ford</Answer>
</Element>
<Element>
<Question>Year</Question>
<Answer>1995</Answer>
</Element>
</Car>
<Car>
<Element>
<Question>Brand</Question>
<Answer>Hummer</Answer>
</Element>
<Element>
<Question>Year</Question>
<Answer>1990</Answer>
</Element>
</Car>
</Document>
What I need is the following result:
<Document>
<Car>
<Brand>Ford</Brand>
<Year>1995</Year>
</Car>
<Car>
<Brand>Hummer</Brand>
<Year>1990</Year>
</Car>
</Document>
Can anybody help me?
Well, it is rather trivial, I don't know why you're struggling with it:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/Document">
<Document>
<xsl:for-each select="Car">
<Car>
<xsl:for-each select="Element">
<xsl:element name="{Question}">
<xsl:value-of select="Answer" />
</xsl:element>
</xsl:for-each>
</Car>
</xsl:for-each>
</Document>
</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.