xsl-list
[Top] [All Lists]

[xsl] conditional replacement

2009-01-13 07:22:36
Dear All,

Input XML is:

<authors>
<ce:author>
    <ce:given-name>Michael</ce:given-name>
    <ce:surname>Wininger</ce:surname>
</ce:author>
<ce:author>
     <ce:given-name>Nam-Hun</ce:given-name>
     <ce:surname>Kim</ce:surname>
</ce:author>
<ce:author>
     <ce:given-name>Patrick A.</ce:given-name>
     <ce:surname>Costigan</ce:surname>
</ce:author>
<ce:author>
     <ce:given-name>K.</ce:given-name>
     <ce:surname>Mithraratne</ce:surname>
</ce:author>
<ce:author>
     <ce:given-name>N.S.</ce:given-name>
     <ce:surname>Stott</ce:surname>
</ce:author>
</authors>


I have successfully transformed the given-name into its initials with
the below XSL

<xsl:template match="authors">
      <xsl:apply-templates select="ce:author"/>
</xsl:template>


<xsl:template match="ce:author">
<xsl:choose>
        <xsl:when test="matches(ce:given-name,'^[A-Z][a-z]')">
                <xsl:variable name="text" select="tokenize(ce:given-name,' ')"/>
                <xsl:for-each select="$text">
                        <xsl:choose>
                                <xsl:when test="contains(.,'-')">
                                        <xsl:variable name="htext" 
select="tokenize(.,'-')"/>
                                        <xsl:value-of
select="concat(substring(subsequence($htext,1,1),1,1),'.','-,substring(subsequence($htext,2,2),1,1),'.')"/>
                                </xsl:when>
                                <xsl:otherwise>
                                     <xsl:choose>
                                          <xsl:when test="not(contains(.,'.'))">
                                                <xsl:value-of 
select="concat(substring(.,1,1),'.')"/>
                                          </xsl:when>
                                          <xsl:otherwise>
                                                <xsl:value-of select="."/>
                                          </xsl:otherwise>
                                    </xsl:choose>
                              </xsl:otherwise>
                        </xsl:choose>
                 </xsl:for-each>
            </xsl:when>
            <xsl:otherwise>
                 <xsl:value-of select="ce:given-name"/>
            </xsl:otherwise>
</xsl:choose>
</xsl:template>

        
and got the output like below:

    <author>M. Wininger</author>
     <author>N.-H. Kim</author>
     <author>P.A. Costigan</author>
     <author>K. Mithraratne</author>
      <author>N.S. Stott</author>

Now I am having two requirements. 1. insert space between initials. 2.
remove '.' in the initials. These two conditions I am taking from
ini.xml file.

I have tried with the following expressions but I am not getting the result.

<xsl:template match="ce:author">
<xsl:if test="document('ini.xml')//init/space = 1">
        <xsl:variable name="space" select=" "/>
</xsl:if>

<xsl:choose>
<xsl:when test="document('ini.xml')//init/remove = 1">
        <xsl:variable name="pat" select="string('.')"/>
</xsl:when>
<xsl:otherwise>
        <xsl:variable name="pat" select="\.[^$]"/>
</xsl:otherwise>
<xsl:choose>

        <xsl:when test="matches(ce:given-name,'^[A-Z][a-z]')">
                <xsl:variable name="text" select="tokenize(ce:given-name,' ')"/>
                <xsl:for-each select="$text">
                        <xsl:choose>
                                <xsl:when test="contains(.,'-')">
                                        <xsl:variable name="htext" 
select="tokenize(.,'-')"/>
                                        <xsl:value-of
select="concat(substring(subsequence($htext,1,1),1,1),'.','-,substring(subsequence($htext,2,2),1,1),'.')"/>
                                </xsl:when>
                                <xsl:otherwise>
                                     <xsl:choose>
                                          <xsl:when test="not(contains(.,'.'))">
                                                <xsl:value-of
select="replace(concat(substring(.,1,1),'.'),$pat,'.{$space}')"/>
                                          </xsl:when>
                                          <xsl:otherwise>
                                                <xsl:value-of 
select="replace(.,$pat,'.{$space}')"/>
                                          </xsl:otherwise>
                                    </xsl:choose>
                              </xsl:otherwise>
                        </xsl:choose>
                 </xsl:for-each>
            </xsl:when>
            <xsl:otherwise>
                 <xsl:value-of
select="replace(ce:given-name,$pat,'.{$space}')"/>
            </xsl:otherwise>
</xsl:choose>
</xsl:template>

Check for the replace function and variable declaration.

Please help me.

Regards,
Ganesh

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>