Reputation: 109
I have a xml like below :
<?xml version="1.0" encoding="UTF-8"?>
<data>
<mObject distName="ABC-1/Schedule-New/ScheduleItem-2002" class="PM">
<p name="Active">1</p>
</managedObject>
<mObject distName="ABC-2/Schedule-New/ScheduleItem-2002" class="PM">
<p name="Active">1</p>
</managedObject>
</data>
Now i have to insert a text "XYZ-1" in mObject attibute distName like below
<?xml version="1.0" encoding="UTF-8"?>
<data>
<mObject distName="ABC-1/XYZ-1/Schedule-New/ScheduleItem-2002" class="PM">
<p name="Active">1</p>
</managedObject>
<mObject distName="ABC-2/XYZ-1/Schedule-New/ScheduleItem-2002" class="PM">
<p name="Active">1</p>
</managedObject>
</data>
I could achieve that using below:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output encoding="UTF-8" indent="yes" method="xml" omit-xml-declaration="no" doctype-system="raml21.dtd" />
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="mObject[@class = 'PM' ]">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="distName">
<xsl:value-of select="concat(substring-before( @distName,'Schedule-' ), 'XYZ-1/', substring-after( @distName, '/'))"/>
</xsl:attribute>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
But few questions here:
How does this guarantee that the insertion of XYZ-1 text always happens after 1st occurrence of "/" in distName attribute?
I want the insertion to happen only after first occurrence of "/".
Any ideas or suggestion????
Upvotes: 0
Views: 87
Reputation: 163458
Looks to me like you want
concat(substring-before( @distName,'/' ), '/XYZ-1/', substring-after( @distName, '/'))
Upvotes: 2