Suyash
asked on
XSL : XML Transformation - I want to permorm some operations when a particular tag occures else other operation
Hi Experts !!!
Pls Help me out of this prob. What i am trying to do is to transform one xml to another using xsl. my input xml look like
<root>
<child>
<attrib1>value</attrib1>
<attrib1>value</attrib1>
<subchild>
<subattrib>value</subattri b>
</subchild>
</child>
<child>
<attrib1>value</attrib1>
<attrib1>value</attrib1>
<grandchild>
<gattrib>value</gattrib>
</grandchild>
</child>
</root>
Here note that inside child tag either subchild can occure or grandchild but not both.
and in xsl i want to perform something when inside child tag i have subchild tag
& somthing else when i have grandchild inside childtag. & i want to make sure that atleast one exists .
So could pls suggest me how can i determine in xsl that inside child tag wheather subchild exists or grandchild ??
Pls Help me out of this prob. What i am trying to do is to transform one xml to another using xsl. my input xml look like
<root>
<child>
<attrib1>value</attrib1>
<attrib1>value</attrib1>
<subchild>
<subattrib>value</subattri
</subchild>
</child>
<child>
<attrib1>value</attrib1>
<attrib1>value</attrib1>
<grandchild>
<gattrib>value</gattrib>
</grandchild>
</child>
</root>
Here note that inside child tag either subchild can occure or grandchild but not both.
and in xsl i want to perform something when inside child tag i have subchild tag
& somthing else when i have grandchild inside childtag. & i want to make sure that atleast one exists .
So could pls suggest me how can i determine in xsl that inside child tag wheather subchild exists or grandchild ??
... here is a better solution:
<xsl:for-each select="root/child">
<xsl:variable name="subchild" select="count(child::subch ild)"/>
<xsl:variable name="grandchild" select="count(child::grand child)"/>
<xsl:if test="$subchild = 0 and $grandchild >= 1">
... handle the case with one or more grandchildren
</xsl:if>
<xsl:if test="$subchild >= 1 and $grandchild = 0">
... handle the case with one or more subchildren
</xsl:if>
<xsl:if test="$subchild > 0 and $grandchild > 0">
... handle the case with subchild and granchild
</xsl:if>
<xsl:if test="$subchild = 0 and $grandchild = 0">
... handle the case with no subchild and no granchild
</xsl:if>
</xsl:for-each>
<xsl:for-each select="root/child">
<xsl:variable name="subchild" select="count(child::subch
<xsl:variable name="grandchild" select="count(child::grand
<xsl:if test="$subchild = 0 and $grandchild >= 1">
... handle the case with one or more grandchildren
</xsl:if>
<xsl:if test="$subchild >= 1 and $grandchild = 0">
... handle the case with one or more subchildren
</xsl:if>
<xsl:if test="$subchild > 0 and $grandchild > 0">
... handle the case with subchild and granchild
</xsl:if>
<xsl:if test="$subchild = 0 and $grandchild = 0">
... handle the case with no subchild and no granchild
</xsl:if>
</xsl:for-each>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I would make sure that these conditions are met in the XML file, and not leave it up to the transformation to do the error checking. In this case, you can use something like this:
<xsl:for-each select="root">
<xsl:if test="child/subchild">
... do something ...
</xsl:if>
<xsl:if test="child/grandchild">
... do something else ...
</xsl:if>
</xsl:for-each>
This would not correctly handle the case where you have both, or where none of them exists.