<Person>
<AdditionalAttributes groupLabel="Profile">
<AdditionalAttribute name="First Name" value="John"/>
<AdditionalAttribute name="Last Name" value="Smith"/>
</AdditionalAttributes>
<AdditionalAttributes groupLabel="Additional">
<AdditionalAttribute name="email" value="John Smith(jsmith)"/>
<AdditionalAttribute name="Created Date" value="2016-04-20T19:50:01Z"/>
</AdditionalAttributes>
</Person>
Can you show me how to use xslt to add @gmail.com to value of element email from John Smith (jsmith) to John Smith(jsmith**@gmail.com**) assuming email value is dynamic
thanks and regards
XSLT 1.0
,可以使用translate
和concat
,不需要xslt
,只需使用普通的xpath
。
//Person/AdditionalAttributes/AdditionalAttribute[@name='email']/concat(translate(@value, ')', '@'), 'gmail.com)')
Try:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="AdditionalAttribute[@name='email']/@value">
<xsl:attribute name="value">
<xsl:value-of select="substring-before(., ')')"/>
<xsl:text>@gmail.com)</xsl:text>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
This is assuming there are no closing parentheses other than the last one. Alternatively, you could do:
<xsl:template match="AdditionalAttribute[@name='email']/@value">
<xsl:attribute name="value">
<xsl:value-of select="substring(., 1, string-length(.) - 1)"/>
<xsl:text>@gmail.com)</xsl:text>
</xsl:attribute>
</xsl:template>
Can you also show me how to replace "John Smith(jsmith)" with "jsmith@gmail.com"?
Extract the text in parentheses and append the domain name to it:
<xsl:template match="AdditionalAttribute[@name='email']/@value">
<xsl:attribute name="value">
<xsl:value-of select="substring-before(substring-after(., '('), ')')"/>
<xsl:text>@gmail.com</xsl:text>
</xsl:attribute>
</xsl:template>
I. XSLT 1.0
Similar to the solution of michael.hor257k, but using AVT s -- Attribute-Value Templates.
Thus no need for an <xsl:attribute>
operator :
<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="AdditionalAttribute[@name='email']">
<AdditionalAttribute name="email"
value="{substring-before(@value, '(')
}({substring-before(
substring-after(@value,'('),
')'
)}@gmail.com)">
<xsl:apply-templates select="@*[not(name()='value')]"/>
</AdditionalAttribute>
</xsl:template>
</xsl:stylesheet>
When applied on the provided XML document :
<Person>
<AdditionalAttributes groupLabel="Profile">
<AdditionalAttribute name="First Name" value="John"/>
<AdditionalAttribute name="Last Name" value="Smith"/>
</AdditionalAttributes>
<AdditionalAttributes groupLabel="Additional">
<AdditionalAttribute name="email" value="John Smith(jsmith)"/>
<AdditionalAttribute name="Created Date" value="2016-04-20T19:50:01Z"/>
</AdditionalAttributes>
</Person>
the wanted, correct result is produced :
<Person>
<AdditionalAttributes groupLabel="Profile">
<AdditionalAttribute name="First Name" value="John"/>
<AdditionalAttribute name="Last Name" value="Smith"/>
</AdditionalAttributes>
<AdditionalAttributes groupLabel="Additional">
<AdditionalAttribute name="email" value="John Smith(jsmith@gmail.com)"/>
<AdditionalAttribute name="Created Date" value="2016-04-20T19:50:01Z"/>
</AdditionalAttributes>
</Person>
II. XSLT 2.0
This transformation uses AVT again and the XPath 2.0 replace()
function:
<xsl:stylesheet version="2.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="AdditionalAttribute[@name='email']">
<AdditionalAttribute name="email"
value="{replace(@value, '\)', '@gmail.com)')}">
<xsl:apply-templates select="@*[not(name()='value')]"/>
</AdditionalAttribute>
</xsl:template>
</xsl:stylesheet>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.