xslt - Read attributes of element and replace part of string in the attribute based on some criteria -
i have problem in creating xsl transformation below case:
<text> <data> <object class="centre" name="country-1/centre-1/rty-1" version="1"> <p name="map">20</p> <p name="required">0</p> </object> <object class="left" name="country-1/left-1/rty-1" version="1"> <p name="map">20</p> <p name="required">0</p> </object> <object class="right" name="country-1/right-1/uio-1" version="1"> <p name="map">20</p> <p name="required">0</p> </object> </data> </text>
in above snippet have to:
- find element class attribute centre
- and replace centre side in class attribute value , sub string replace name attribute value country-1/centre-1/rty-1 country-1/side-1/rty-1**.
- rest elements should not affected , should retained is.
i tried substring replacement through method replace-all-string unsuccessful. in advance
output xml sholud like:
<text> <data> <object class="side" name="country-1/side-1/rty-1" version="1"> <p name="map">20</p> <p name="required">0</p> </object> <object class="left" name="country-1/left-1/rty-1" version="1"> <p name="map">20</p> <p name="required">0</p> </object> <object class="right" name="country-1/right-1/uio-1" version="1"> <p name="map">20</p> <p name="required">0</p> </object> </data> </text>
try this:
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0"> <xsl:output method="xml" indent="yes"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="object[@class = 'centre' ]"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:attribute name="class"> <xsl:value-of select="'side'"/> </xsl:attribute> <xsl:attribute name="name"> <xsl:value-of select="concat(substring-before( @name,'centre' ), 'side', substring-after( @name, 'centre'))"/> </xsl:attribute> <xsl:apply-templates select="node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet>
which generate following output:
<text> <data> <object class="side" name="country-1/side-1/rty-1" version="1"> <p name="map">20</p> <p name="required">0</p> </object> <object class="left" name="country-1/left-1/rty-1" version="1"> <p name="map">20</p> <p name="required">0</p> </object> <object class="right" name="country-1/right-1/uio-1" version="1"> <p name="map">20</p> <p name="required">0</p> </object> </data> </text>
Comments
Post a Comment