Removing Tabs from source XML in output from XSLT

Discussion in 'XML' started by cmay, Jul 29, 2006.

  1. cmay

    cmay Guest

    I am having this problem...
    Lets say that your source XML is formatted like this:
    <somenode>
    Here is some text
    Here is some more text
    </somenode>

    When to a <xsl:value-of select="somenode" /> I want the output to be
    free of the tabs that are included in the source XML.
    These tabs are not really part of the content, but rather just there
    for formatting reasons.

    I tried doing:
    <xsl:value-of select='translate(somenode,"&#x9", "")'/>
    But that didn't seem to have an effect.


    Any way to do this?
    cmay, Jul 29, 2006
    #1
    1. Advertising

  2. > These tabs are not really part of the content, but rather just there
    > for formatting reasons.


    Well, the best answer is not to put them in for formatting, but to
    format for humans at the time when humans want to read the data.
    However, assuming that you're stuck with them...

    > <xsl:value-of select='translate(somenode,"&#x9", "")'/>


    No, that won't work; translate is strictly a replacement operation, and
    can't be used to remove characters.

    If you know there's only a single tab, you could use
    concat(substring-before(somenode,"&#x9"),
    substring-after(somenode,"&#x9"))
    If there may be multiple tabs, you need to repeat that until all tabs
    have been dealt with -- which in XSLT 1.0 means writing a recursive
    named template and invoking it via call-template. (XSLT 2.0 has a
    slightly more elegant syntax for writing and calling functions.) I'm
    sure there are examples of this approach in the XSLT FAQ, or in many
    decent XSLT tutorials.

    Or you could invoke a non-XSLT extension function, if your processor
    supports that.

    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
    Joe Kesselman, Jul 29, 2006
    #2
    1. Advertising

  3. cmay wrote:


    > I tried doing:
    > <xsl:value-of select='translate(somenode,"&#x9", "")'/>
    > But that didn't seem to have an effect.


    That tab character has code number 9 and a character reference needs to
    end with a semicolon so you need
    <xsl:value-of select='translate(somenode," ", "")'/>

    That should do then in my view, contrary what Joe says, as translate
    <http://www.w3.org/TR/xpath#function-translate> indeed removes
    characters found in the second argument string for which there are no
    corresponding positions in the third argument string.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jul 30, 2006
    #3
  4. Good catch on the missing semicolon, and yes the removal should work; I
    was misremembering. (Sigh. Don't type while tired.)

    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
    Joe Kesselman, Jul 30, 2006
    #4
  5. cmay

    WideBoy Guest

    If you're working on a unix platform another simpler alternative may to
    run your source xml file through a utility program such as 'detab' or
    'entab'?

    This would mean that you would not have to use any form of XSL at all
    ofcourse.

    HTH,

    Naran.

    cmay wrote:
    > I am having this problem...
    > Lets say that your source XML is formatted like this:
    > <somenode>
    > Here is some text
    > Here is some more text
    > </somenode>
    >
    > When to a <xsl:value-of select="somenode" /> I want the output to be
    > free of the tabs that are included in the source XML.
    > These tabs are not really part of the content, but rather just there
    > for formatting reasons.
    >
    > I tried doing:
    > <xsl:value-of select='translate(somenode,"&#x9", "")'/>
    > But that didn't seem to have an effect.
    >
    >
    > Any way to do this?
    WideBoy, Jul 30, 2006
    #5
  6. On 30 Jul 2006 05:37:26 -0700, "WideBoy" wrote in comp.text.xml:

    >If you're working on a unix platform another simpler alternative may to
    >run your source xml file through a utility program such as 'detab' or
    >'entab'?

    [snip]

    There are numerous versions of detab available for Win32, too.

    --
    Michael Bednarek http://mbednarek.com/ "POST NO BILLS"
    Michael Bednarek, Jul 30, 2006
    #6
  7. cmay

    cmay Guest

    Thanks for responding guys.
    Unfortunately, the typo was just in my newsgroup post, and not in my
    code.
    I tried it again but the tabs persist.

    Even though these are definately tabs in my xml source, I wonder if
    they are coming through as spaces somehow.

    Anyway thanks for your help.



    Chris



    Joe Kesselman wrote:
    > Good catch on the missing semicolon, and yes the removal should work; I
    > was misremembering. (Sigh. Don't type while tired.)
    >
    > --
    > () ASCII Ribbon Campaign | Joe Kesselman
    > /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
    cmay, Jul 30, 2006
    #7
  8. cmay wrote:
    > I tried it again but the tabs persist.


    Are you sure the rest of your stylesheet is reasonable -- ie, that this
    expression is indeed being evaluated at the right places?


    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
    Joe Kesselman, Jul 31, 2006
    #8
  9. cmay

    George Bina Guest

    If you can use XSLT 2.0 then you can use a character map. Just add the
    following lines to your stylesheet and the tabs will be removed from
    output:

    <xsl:character-map name="test">
    <xsl:eek:utput-character character=" " string=""/>
    </xsl:character-map>
    <xsl:eek:utput use-character-maps="test"/>

    Best Regards,
    George
    ---------------------------------------------------------------------
    George Cristian Bina
    <oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
    http://www.oxygenxml.com

    cmay wrote:
    > I am having this problem...
    > Lets say that your source XML is formatted like this:
    > <somenode>
    > Here is some text
    > Here is some more text
    > </somenode>
    >
    > When to a <xsl:value-of select="somenode" /> I want the output to be
    > free of the tabs that are included in the source XML.
    > These tabs are not really part of the content, but rather just there
    > for formatting reasons.
    >
    > I tried doing:
    > <xsl:value-of select='translate(somenode,"&#x9", "")'/>
    > But that didn't seem to have an effect.
    >
    >
    > Any way to do this?
    George Bina, Jul 31, 2006
    #9
  10. cmay

    cmay Guest

    Yea it turned out that my xml editor was converting tabs to spaces.

    I found some option to turn that off and the tab removal translate
    function works fine.

    Thanks guys!



    Joe Kesselman wrote:
    > cmay wrote:
    > > I tried it again but the tabs persist.

    >
    > Are you sure the rest of your stylesheet is reasonable -- ie, that this
    > expression is indeed being evaluated at the right places?
    >
    >
    > --
    > () ASCII Ribbon Campaign | Joe Kesselman
    > /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
    cmay, Jul 31, 2006
    #10
  11. cmay

    cmay Guest

    Thanks George, I didn't know about that option and it looks pretty
    good, good thing to know.

    Is there anything similar that will allow for replacement of strings in
    this manner?

    I see that then 2nd parameter can be a string, but everything I have
    read indicates that the first parameter "character" must be a single
    character and can't be a string.

    I'm guessing there isn't anything to do this other than writing your
    own function correct?


    Chris



    George Bina wrote:
    > If you can use XSLT 2.0 then you can use a character map. Just add the
    > following lines to your stylesheet and the tabs will be removed from
    > output:
    >
    > <xsl:character-map name="test">
    > <xsl:eek:utput-character character=" " string=""/>
    > </xsl:character-map>
    > <xsl:eek:utput use-character-maps="test"/>
    >
    > Best Regards,
    > George
    > ---------------------------------------------------------------------
    > George Cristian Bina
    > <oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
    > http://www.oxygenxml.com
    >
    > cmay wrote:
    > > I am having this problem...
    > > Lets say that your source XML is formatted like this:
    > > <somenode>
    > > Here is some text
    > > Here is some more text
    > > </somenode>
    > >
    > > When to a <xsl:value-of select="somenode" /> I want the output to be
    > > free of the tabs that are included in the source XML.
    > > These tabs are not really part of the content, but rather just there
    > > for formatting reasons.
    > >
    > > I tried doing:
    > > <xsl:value-of select='translate(somenode,"&#x9", "")'/>
    > > But that didn't seem to have an effect.
    > >
    > >
    > > Any way to do this?
    cmay, Jul 31, 2006
    #11
  12. cmay

    George Bina Guest

    You cannot replace strings, only characters. The replacement is a
    string, for instance if you want to write an entity reference for a
    specific character then you can map that character to the string that
    represents an entity reference to that character.

    Best Regards,
    George
    ---------------------------------------------------------------------
    George Cristian Bina
    <oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
    http://www.oxygenxml.com

    cmay wrote:
    > Thanks George, I didn't know about that option and it looks pretty
    > good, good thing to know.
    >
    > Is there anything similar that will allow for replacement of strings in
    > this manner?
    >
    > I see that then 2nd parameter can be a string, but everything I have
    > read indicates that the first parameter "character" must be a single
    > character and can't be a string.
    >
    > I'm guessing there isn't anything to do this other than writing your
    > own function correct?
    >
    >
    > Chris
    >
    >
    >
    > George Bina wrote:
    > > If you can use XSLT 2.0 then you can use a character map. Just add the
    > > following lines to your stylesheet and the tabs will be removed from
    > > output:
    > >
    > > <xsl:character-map name="test">
    > > <xsl:eek:utput-character character=" " string=""/>
    > > </xsl:character-map>
    > > <xsl:eek:utput use-character-maps="test"/>
    > >
    > > Best Regards,
    > > George
    > > ---------------------------------------------------------------------
    > > George Cristian Bina
    > > <oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
    > > http://www.oxygenxml.com
    > >
    > > cmay wrote:
    > > > I am having this problem...
    > > > Lets say that your source XML is formatted like this:
    > > > <somenode>
    > > > Here is some text
    > > > Here is some more text
    > > > </somenode>
    > > >
    > > > When to a <xsl:value-of select="somenode" /> I want the output to be
    > > > free of the tabs that are included in the source XML.
    > > > These tabs are not really part of the content, but rather just there
    > > > for formatting reasons.
    > > >
    > > > I tried doing:
    > > > <xsl:value-of select='translate(somenode,"&#x9", "")'/>
    > > > But that didn't seem to have an effect.
    > > >
    > > >
    > > > Any way to do this?
    George Bina, Aug 2, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Blatt
    Replies:
    0
    Views:
    444
    Peter Blatt
    Sep 25, 2004
  2. qwweeeit
    Replies:
    2
    Views:
    647
    qwweeeit
    Dec 14, 2005
  3. Andy Fish
    Replies:
    10
    Views:
    1,134
    Joseph Kesselman
    May 4, 2007
  4. rantingrick

    Tabs -vs- Spaces: Tabs should have won.

    rantingrick, Jul 16, 2011, in forum: Python
    Replies:
    95
    Views:
    1,832
    Roy Smith
    Jul 19, 2011
  5. John Kopanas
    Replies:
    2
    Views:
    291
    Gregory Brown
    Jan 29, 2007
Loading...

Share This Page