Reputation: 163
I have an incoming XML document. I just need to modify value of one element example <ID>
element in this below incoming XML document. I basically need to check for element called <ID>
if the value is without any hyphen it will take as it is and if the value contains hyphen(-) then i need to take the value before hyphen (-) ex- 4314141
Incoming XML document:
<Message>
<ID>4314141-324234</ID>
<EMAIL>abc</EMAIL>
</Message>
I am using this below XSL to do do this but it is not working as expected. XSL:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dp="http://www.datapower.com/extensions"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
extension-element-prefixes="dp"
exclude-result-prefixes="dp" >
<xsl:variable name="uuid" select="dp:variable('var://context/txn/uuid')" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/ID">
<xsl:copy>
<ID><xsl:value-of select="substring-before($ID, ' -')" /></ID>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="ID"/>
</xsl:stylesheet>
Let me know how i can do this.
Upvotes: 1
Views: 430
Reputation: 243459
Just use this template overriding the identity rule:
<xsl:template match="ID/text()[contains(., '-')]">
<xsl:value-of select="substring-before(., '-')"/>
</xsl:template>
Here is the complete transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ID/text()[contains(., '-')]">
<xsl:value-of select="substring-before(., '-')"/>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<Message>
<ID>4314141-324234</ID>
<EMAIL>abc</EMAIL>
</Message>
the wanted, correct result is produced:
<Message>
<ID>4314141</ID>
<EMAIL>abc</EMAIL>
</Message>
Upvotes: 0
Reputation: 2167
without any hyphen it will take as it is
This will do your identity-copy template.
if the value contains hyphen(-) then i need to take the value before hyphen (-)
<xsl:template match="ID[contains(., '-')]">
<xsl:copy>
<xsl:value-of select="substring-before(., '-')" />
</xsl:copy>
</xsl:template>
Friendly advice: Please be carefull with /
in your matching patterns.
Upvotes: 1