Reputation: 5
I need to get the value for each '/' but I can't get the correct output because of the special characters included in the file. How can I use the correct regex to be used in the checking? I am using the <xsl:analyze-string>
element to get the value. Here is my sample file:
INPUTFile:
<Communication>
<DialNumber>Phone/+31-3424-27385/null/Phone/+06-32-7890-565/Mobile(Office)/null/+313-(424)-28500/Fax</DialNumber>
</Communication>
EXPECTED OUTPUT
<Communication>
<ChannelCode>Phone</ChannelCode>
<UseCode>null</UseCode>
<DialNumber>+31-3424-27385</DialNumber>
</Communication>
<Communication>
<ChannelCode>Phone</ChannelCode>
<UseCode>Mobile(Office)</UseCode>
<DialNumber>+06-32-7890-565</DialNumber>
</Communication>
<Communication>
<ChannelCode>null</ChannelCode>
<UseCode>Fax</UseCode>
<DialNumber>+313-(424)-28500</DialNumber>
</Communication>
XSLTCode
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="DialNumber">
<xsl:analyze-string select="normalize-space()" regex="(\w+)/(\w+)/(\w+)">
<xsl:matching-substring>
<Communication>
<ChannelCode>
<xsl:value-of select="regex-group(1)"/>
</ChannelCode>
<UseCode>
<xsl:value-of select="regex-group(3)"/>
</UseCode>
<DialNumber>
<xsl:value-of select="regex-group(2)"/>
</DialNumber>
</Communication>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:template>
</xsl:stylesheet>
I need to check the 1st 3 words after '/' and the next 3 words again, and the last 3 words. It looks like this:
Phone/+31-3424-27385/null
Phone/+06-32-7890-565/Mobile(Office)
null/+313-(424)-28500/Fax
The 1st regex I need to assign in <Channel>
, 2nd in <DialNumber>
and 3rd in <UseCode>
.
Thank you in advance for your feedback.
Upvotes: 0
Views: 105
Reputation: 163322
No need for analyze-string
here, tokenize()
will work fine
<xsl:variable name="tokens" select="tokenize(., '/')"/>
<xsl:for-each-group select="$tokens" group-adjacent="(position()-1) idiv 3">
<Communication>
<ChannelCode>
<xsl:value-of select="current-group()[1]" />
</ChannelCode>
<UseCode>
<xsl:value-of select="current-group()[3]" />
</UseCode>
<DialNumber>
<xsl:value-of select="current-group()[2]" />
</DialNumber>
</Communication>
</xsl:for-each-group>
Upvotes: 1
Reputation: 116992
If - as it seems - your input is organized in groups of three, you could do simply:
<xsl:template match="Communication">
<xsl:for-each-group select="tokenize(DialNumber, '/')" group-by="(position()-1) idiv 3">
<Communication>
<ChannelCode>
<xsl:value-of select="current-group()[1]" />
</ChannelCode>
<UseCode>
<xsl:value-of select="current-group()[3]" />
</UseCode>
<DialNumber>
<xsl:value-of select="current-group()[2]" />
</DialNumber>
</Communication>
</xsl:for-each-group>
</xsl:template>
Demo: http://xsltransform.net/3MvmrA5/1
Upvotes: 2
Reputation: 5432
You can replace your analyze-string
with the following if the separator is /
character:
<xsl:analyze-string select="normalize-space()" regex="(.+?)/(.+?)/(.+?)(/|$)" >
Here, (.+?)/
does a lazy search matching a set of characters before a /
. And /|$
will consider the last token after slash, as $
denotes end of string.
Upvotes: 0