Home > Mobile >  how can modify the text of a element before concatenating it with xslt1.0
how can modify the text of a element before concatenating it with xslt1.0

Time:07-30

I am trying to concatenate in a single element, all tags with the same name (my XSLT already does this), the problem is that the text has a hyphen at the beginning and a blank space at the end that I must remove, I can not do this last part. I have this input:

<LIST>
   <PLACE>
       <level>-this </level>
       <level>-Is </level>
       <level>What</level>
       <level>-I</level>
       <level>Want </level>
   </PLACE>
</LIST>

And need an output like :

<LIST>
   <PLACE>
       <level>thisIsWhatIWant</level>
   </PLACE>
</LIST>

Im using this XSLT :

<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="*"/>

    <xsl:template match="@*|node()">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </xsl:template>
    <xsl:template match="PLACE">
      <PLACE>
            <level>
<!-- I tried this to eliminate the hyphen -->
             <xsl:value-of select="translate(.,'-','')"/>
<!-- and this to eliminate the white space at the end of the elements -->
              <xsl:value-of select="translate(.,' ','')"/>
            <xsl:value-of select="normalize-space(concat(level[1],level[2],level[3],level[4],level[5]))" />
            </level>
      </PLACE>
    </xsl:template>
</xsl:stylesheet>

How can I modify my xslt to eliminate the white spaces and the hyphen from the text before concatenating it.

Thanks

CodePudding user response:

Use this:

<?xml version="1.0" encoding="UTF-8"?>
<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="*"/>
  
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="PLACE">
    <PLACE>
      <level>
          <xsl:apply-templates/>
      </level>
    </PLACE>
  </xsl:template>
  
  <xsl:template match="level">
    <xsl:value-of select="normalize-space(translate(.,'-',''))"/>
  </xsl:template>
</xsl:stylesheet>

EDIT

If you want to have only certain positions (as requested in your comment), you could change it to:

  <xsl:template match="PLACE">
    <PLACE>
      <level>
          <xsl:apply-templates select="level[1]"/>
          <xsl:apply-templates select="level[4]"/>
          <xsl:apply-templates select="level[5]"/>
      </level>
    </PLACE>
  </xsl:template>
  • Related