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:

  1. find element class attribute centre
  2. 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**.
  3. 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

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -