Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: string methods n xsl

From: "Marrow" <m--a-r-r-o-w@--------------------.--->
To: NULL
Date: 10/1/2004 2:07:00 AM
Hi,

> is there any way to do string methods in xsl? e.g. string splitting and
> string concatination? e.g.

Yes... substring(), substring-after() and substring-before() functions for
splitting... and the concat() function for concatenation.

None of which would be particularly useful, on their own, in your
hypothetical example... specifically, you'd never have to use the concat()
function for either process.  The concat() function is way over used by many
XSLT programmers - and is often a tautology of what the transformation
engine was intending to do anyway... serialize the output.

Try these stylesheets...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/foos">
  <abc>
    <xsl:apply-templates/>
  </abc>
</xsl:template>

<xsl:template match="a | b | c">
  <xsl:copy>
    <xsl:attribute name="foos">
      <xsl:for-each select="*">
        <xsl:value-of select="name()"/>
        <xsl:if test="position() != last()">
          <xsl:text>_</xsl:text>
        </xsl:if>
      </xsl:for-each>
    </xsl:attribute>
  </xsl:copy>
</xsl:template>
</xsl:stylesheet>


and...


<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/abc">
  <foos>
    <xsl:apply-templates/>
  </foos>
</xsl:template>

<xsl:template match="a | b | c">
  <xsl:copy>
    <xsl:call-template name="split-foo-att">
      <xsl:with-param name="foo-att" select="@foos"/>
    </xsl:call-template>
    <xsl:attribute name="foos">
      <xsl:for-each select="*">
        <xsl:value-of select="name()"/>
        <xsl:if test="position() != last()">
          <xsl:text>_</xsl:text>
        </xsl:if>
      </xsl:for-each>
    </xsl:attribute>
  </xsl:copy>
</xsl:template>

<xsl:template name="split-foo-att">
  <xsl:param name="foo-att"/>
  <xsl:choose>
    <xsl:when test="contains($foo-att,'_')">
      <xsl:element name="{substring-before($foo-att,'_')}"/>
      <xsl:call-template name="split-foo-att">
        <xsl:with-param name="foo-att"
select="substring-after($foo-att,'_')"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="normalize-space($foo-att)">
      <xsl:element name="{$foo-att}"/>
    </xsl:when>
  </xsl:choose>
</xsl:template>
</xsl:stylesheet>


HTH
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator


"Daniel" <softwareengineer98037@y...> wrote in message
news:eLczOx0pEHA.2456@T......
> is there any way to do string methods in xsl? e.g. string splitting and
> string concatination? e.g.
>
> convert:
> ______________________________________
> <foos>
>     <a>
>         <abh/>
>         <def/>
>          <ghi/>
>     </a>
>      <b>
>           <ccc/>
>           <ddd/>
>           <eee/>
>       </b>
>       <c>
>           <xxx/>
>           <yyy/>
>           <zzz/>
>       </c>
> </foos>
> ______________________________________
>
> to
> ______________________________________
> <abc>
>     <a foo="abh_def_ghi">
>     <b foo="ccc_ddd_eee">
>     <c foo="xxx_yyy_zzz">
> </abc>
> ______________________________________
>
> or vise-versa
>
>




transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent