XSLT Pattern matching - Remove Ancestors



I'm exporting bar graph data from SSRS as XML and most of the time it looks great but in some cases it gets /SeriesX/CategoryX/ nodes. When .net reads those into a datatable the dat gets split out into three related tables; Series0, Category0, and Value.


I need to remove /SeriesX/CategoryX/ from this:



<My_Location_Chart>
<Series0>
<Category0>
<Value Y="0.813084112149533" />
</Category0>
</Series0>
<Series1>
<Category0>
<Value Y="0.818783068783069" />
</Category0>
</Series1>
<Series2>
<Category0>
<Value Y="0.818709209572154" />
</Category0>
</Series2>
</My_Location_Chart>


So it looks like this:



<My_Location_Chart>
<Value Y="0.813084112149533" />
<Value Y="0.818783068783069" />
<Value Y="0.818709209572154" />
</My_Location_Chart>


There are other charts that use SeriesX values and I don't want to screw those up, but none are in the pattern of /SeriesX/CategoryX/, so I only want to match that pattern.


I have tried the following to remove the Category nodes. It's close, but not exactly what I need:



<xsl:template match="@*|node()" >
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>

<xsl:template match="*[not(descendant-or-self::*[text()[normalize-space()] | @*])]"/>


<xsl:template match="rep:Category0" >
<xsl:apply-templates select="*" />
</xsl:template>


The above creates this:



<My_Location_Chart>
<Series0>
<Value Y="0.813084112149533" />
</Series0>
<Series1>
<Value Y="0.818783068783069" />
</Series1>
<Series2>
<Value Y="0.818709209572154" />
</Series2>
</My_Location_Chart>


Any suggestions are welcome. I'm new to XSLT to go easy on me. Thanks!


No comments:

Post a Comment