Reputation: 3
I have an input tag name properties which has many values in it(like key-value pair) separated by semicolon. I need to find particular strings and asigned the value(which is coming after =)into particular output node
For example in the below input.xml i want to take mail.debug=false and assigned the value "false" into output xml debug node.
Input.xml
<mail-session>
<name>MailSession-1</name>
<target>AdminServer</target>
<jndi-name>MailNotification2</jndi-name>
<properties>
mail.debug=false;mail.smtp.user=weblogic;mail.from=master@mydom.com;
</properties>
</mail-session>
My output.xml Should be like this
<?xml version="1.0" encoding="UTF-8"?>
<mail-session jndi-name="java:jboss/mail/mailservice" name="MailSession-1" debug="false" from="master@mydom.com">
<smtp-server outbound-socket-binding-ref="" tls="" ssl="">
<login name="weblogic" password=""/>
</smtp-server>
</mail-session><subsystem>
</subsystem>
Upvotes: 0
Views: 222
Reputation: 505
To perform it more clear and dynamic with changes and requirements I suggest to use as in XSL below:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="get_value_from_str">
<xsl:param name="in.str"/>
<xsl:param name="in.val"/>
<xsl:param name="in.sep"/>
<xsl:choose>
<xsl:when test="string-length($in.val) >0 and string-length($in.sep) >0">
<xsl:value-of select="substring-before(substring-after(normalize-space($in.str), concat($in.val, '=')), $in.sep)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="normalize-space($in.str)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:attribute-set name="mail-session-attr">
<xsl:attribute name="jndi-name">
<xsl:call-template name="get_value_from_str">
<xsl:with-param name="in.str" select="'java:jboss/mail/mailservice'" />
</xsl:call-template>
</xsl:attribute>
<xsl:attribute name="name">
<xsl:call-template name="get_value_from_str">
<xsl:with-param name="in.str" select="/mail-session/name" />
</xsl:call-template>
</xsl:attribute>
<xsl:attribute name="debug">
<xsl:call-template name="get_value_from_str">
<xsl:with-param name="in.str" select="/mail-session/properties" />
<xsl:with-param name="in.val" select="'mail.debug'" />
<xsl:with-param name="in.sep" select="';'" />
</xsl:call-template>
</xsl:attribute>
<xsl:attribute name="from">
<xsl:call-template name="get_value_from_str">
<xsl:with-param name="in.str" select="/mail-session/properties" />
<xsl:with-param name="in.val" select="'mail.from'" />
<xsl:with-param name="in.sep" select="';'" />
</xsl:call-template>
</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="smtp-server-attr">
<xsl:attribute name="outbound-socket-binding-ref" select="''"/>
<xsl:attribute name="tls" select="''"/>
<xsl:attribute name="ssl" select="''"/>
</xsl:attribute-set>
<xsl:attribute-set name="login-attr">
<xsl:attribute name="name">
<xsl:call-template name="get_value_from_str">
<xsl:with-param name="in.str" select="/mail-session/properties" />
<xsl:with-param name="in.val" select="'mail.smtp.user'" />
<xsl:with-param name="in.sep" select="';'" />
</xsl:call-template>
</xsl:attribute>
<xsl:attribute name="password" select="''"/>
</xsl:attribute-set>
<xsl:template match="/">
<xsl:element name="mail-session" use-attribute-sets="mail-session-attr">
<xsl:element name="smtp-server" use-attribute-sets="smtp-server-attr">
<xsl:element name="login" use-attribute-sets="login-attr"/>
</xsl:element>
</xsl:element>
<xsl:element name="subsystem" />
</xsl:template>
</xsl:stylesheet>
Then result will be as expected above:
<?xml version="1.0" encoding="UTF-8"?>
<mail-session jndi-name="java:jboss/mail/mailservice" name="MailSession-1" debug="false" from="master@mydom.com">
<smtp-server outbound-socket-binding-ref="" tls="" ssl="">
<login name="weblogic" password=""/>
</smtp-server>
</mail-session>
<subsystem/>
Hope it will be useful for you.
Upvotes: 0
Reputation: 3424
For XSLT 2.0
You should take a look at Tokenize function (http://www.xsltfunctions.com/xsl/fn_tokenize.html)
tokenize('mail.debug=false;mail.smtp.user=weblogic;mail.from=master@mydom.com;', '[;\s]+')
Older XSLT
If you are using older XSLT version, create your own function:
<xsl:template name="tokenize-string">
<xsl:param name="list" />
<xsl:variable name="newlist" select="concat(normalize-space($list), ' ')" />
<xsl:variable name="first" select="substring-before($newlist, ' ')" />
<xsl:variable name="remaining" select="substring-after($newlist, ' ')" />
<id>
<xsl:value-of select="$first" />
</id>
<xsl:if test="$remaining">
<xsl:call-template name="tokenize-string">
<xsl:with-param name="list" select="$remaining" />
</xsl:call-template>
</xsl:if>
</xsl:template>
Upvotes: 1