Reputation: 403
I have simplified version of my xml here and I want to fill out the data in the parent nodes using the IMP value in the children if any, which should be true if any of the IMP values in the children are true. if no children exist then it should be false.
<info>
<parent index='0'>
<name>test1</name>
<children>
</children>
</parent>
<parent index='1'>
<name>test2</name>
<children>
<VALUE index='0'>test3</VALUE>
<VALUE index='1'>test4</VALUE>
</children>
</parent>
<parent index='2'>
<name>test3</name>
<impvalue>true</impvalue>
</parent>
<parent index='3'>
<name>test4</name>
<impvalue>false</impvalue>
</parent>
</info>
the output needed is
<info>
<parent index='0'>
<name>test1</name>
<children>
</children>
<impvalue>false</impvalue>
</parent>
<parent index='1'>
<name>test2</name>
<children>
<VALUE index='0'>test3</VALUE>
<VALUE index='1'>test4</VALUE>
</children>
<impvalue>true</impvalue>
</parent>
<parent index='2'>
<name>test3</name>
<impvalue>true</impvalue>
</parent>
<parent index='3'>
<name>test4</name>
<impvalue>false</impvalue>
</parent>
</info>
Upvotes: 0
Views: 819
Reputation: 3247
As the input and output XMLs are similar, you can start with copying the input data as is using identity transform
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
Then you can match the <parent>
node which do not have <impvalue>
as its child since those are the nodes wherein the child <impvalue>
needs to be added.
<xsl:template match="parent[not(impvalue)]">
Using a condition, you can check the count
of <children>
nodes having its own child nodes.
<xsl:when test="count(children/*) != 0">
The complete XSLT is as below
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<!-- match <parent> having no <impvalue> child -->
<xsl:template match="parent[not(impvalue)]">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
<impvalue>
<xsl:choose>
<xsl:when test="count(children/*) != 0">
<xsl:value-of select="'true'" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'false'" />
</xsl:otherwise>
</xsl:choose>
</impvalue>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Output
<info>
<parent index="0">
<name>test1</name>
<children />
<impvalue>false</impvalue>
</parent>
<parent index="1">
<name>test2</name>
<children>
<VALUE index="0">test3</VALUE>
<VALUE index="1">test4</VALUE>
</children>
<impvalue>true</impvalue>
</parent>
<parent index="2">
<name>test3</name>
<impvalue>true</impvalue>
</parent>
<parent index="3">
<name>test4</name>
<impvalue>false</impvalue>
</parent>
</info>
Upvotes: 1