Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 2nd, 2007, 11:37 PM
Authorized User
 
Join Date: Oct 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Removing whitespaces with certain conditions

Hello all,

I'm trying to remove spaces (both whitespaces and others) from <book> elements with the given conditions:

1) if <book> has a child element <topic>programming<topic> then spaces in all its child elements be removed. Point to note here is that both preceding, trailing and spaces in between elements/nodes and text be removed.

2) If <book> has a child element <topic>Literature<topic> then ALL spaces EXCEPT where preceding child<name> has value "Lang*" should be removed.

Before:

<books>
 <book>
  <topic>programming</topic>
   <title> Learn XML </title>
    <chapters>AL L
     <name> Introduction</name> <name> varia bles</name> </chapters>
  </book>
  <book>
   <topic>Literature</topic>
    <title> something </title>
     <chapters>Ne w<name> Introduction</name>
      <name> Engli sh</name> Language
      <name> variables</name> </chapters>
   </book>
<books>

AFTER:
<books>
 <book>
  <topic>programming</topic>
   <title>Learn XML</title>
    <chapters>ALL<name>Introduction</name>
    <name>variables</name></chapters>
  </book>
  <book>
   <topic>Literature</topic>
    <title> something</title>
     <chapters>New<name>Introduction</name>
     <name>Engl ish</name>Language<name> variables</name>
     </chapters>
   </book>
<books>

So, in the given example all spaces except one with "variables" is removed. Please let me know if this can be done.

Thanks,
Trishla
 
Old October 3rd, 2007, 04:02 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I normally use "whitespace" to mean x9, xA, xD, and x20, and "space" to mean just x20, so I first have to understand what you mean by the terms...

From your rules, I can't see why the space was left in "Engl ish" as in

<name>Engl ish</name>Language<name> variables</name>

Looks to me something like this (this is 2.0):

(a) an identity template

<xsl:template match="*" mode="#all">
  <xsl:copy><xsl:apply-templates mode="#current"/></xsl:copy>
</xsl:template>

(b) A rule for the cases where you want to strip all spaces:

<xsl:template match="book[topic='programming']">
  <xsl:copy><xsl:apply-templates mode="strip-all"/></xsl:copy>
</xsl:template>

<xsl:template match="text()" mode="strip-all">
  <xsl:value-of select="translate(., ' ', '')"/>
</xsl:template>

(c) A rule for the cases where you want to strip some spaces:

<xsl:template match="book[topic='Literature']">
  <xsl:copy><xsl:apply-templates mode="strip-some"/></xsl:copy>
</xsl:template>

<xsl:template match="text()[not(starts-with(preceding::text()[1], 'Lang')" mode="strip-some">
  <xsl:value-of select="translate(., ' ', '')"/>
</xsl:template>


With 1.0, the only difference is that rule 1 needs to be written three times, once for each mode.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old October 3rd, 2007, 09:35 AM
Authorized User
 
Join Date: Oct 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you so much for replying...

Quote:
quote:From your rules, I can't see why the space was left in "Engl ish" as in
Yeah, that was typo..there should be no space in "English"

But the template match for strip-some mode says "Invalid xpath expression, when I try to transform with the exact code you suggested.

 
Old October 3rd, 2007, 10:35 AM
Authorized User
 
Join Date: Oct 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Also, when you say with ver 1.0 rule 1 need to be written 3 times for each mode then what is rule 1 here? I'm using ver 1.0.

Thanks,
Trishla



 
Old October 3rd, 2007, 10:42 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

He is refering to :

<xsl:template match="*" mode="#all">
  <xsl:copy><xsl:apply-templates mode="#current"/></xsl:copy>
</xsl:template>

In XSLT 1.0 the mode="#all" and mode="#current" are not supported, so you would have to create three copies of this no mode, one with mode="strip-all" and one with mode="strip-some".

/- Sam Judson : Wrox Technical Editor -/
 
Old October 3rd, 2007, 11:46 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Yes, there's a missing closing "]", but I assume you found that without my help.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old October 3rd, 2007, 11:59 AM
Authorized User
 
Join Date: Oct 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yup, that I corrected but still the output file is not well formed. could it be because I don't have correct version?

I'm using XMLSpy® 5 Professional Edition Rel. 2 and don't know whether it supports 2.0 or not. And I'm not able to get it correct for ver 1.0 by making changes as suggested here. So, all this for sure proves that I'm a newbie and way to go.

Thanks again.
Trishla

 
Old October 3rd, 2007, 01:48 PM
Authorized User
 
Join Date: Oct 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm getting output exactly same as input...would appreciate any response/comments. Here is what I did to make it work for 1.0

XSLT Code:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:template match="*">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*" mode="strip-all">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*" mode="strip-some">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="book[topic='programming']">
        <xsl:copy>
            <xsl:apply-templates mode="strip-all"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="text()" mode="strip-all">
        <xsl:value-of select="translate(., ' ', '')"/>
    </xsl:template>
    <xsl:template match="book[topic='Literature']">
        <xsl:copy>
            <xsl:apply-templates mode="strip-some"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="text()[not(starts-with(preceding::text()[1], 'Lang'))]" mode="strip-some">
        <xsl:value-of select="translate(., ' ', '')"/>
    </xsl:template>
</xsl:stylesheet>


Input XML is:

<?xml version="1.0" encoding="UTF-8"?>
<books>
    <book>
        <topic>programming</topic>
        <title> Learn XML </title>
        <chapters>AL L
     <name> Introduction</name>
            <name> varia bles</name>
        </chapters>
    </book>
    <book>
        <topic>Literature</topic>
        <title> something </title>
        <chapters>
            <name> Introduction</name>
            <name> Engli sh</name>Language<name> variables</name>
        </chapters>
    </book>
</books>

And the output is exactly same ...no spaces trimmed.



 
Old October 3rd, 2007, 02:18 PM
Authorized User
 
Join Date: Oct 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

got it working...Big thanks to all who responded..I really appreciate all help.

but wait...I'll be posting more question here and bugging everybody :) in coming days

 
Old October 3rd, 2007, 03:05 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

These two templates need to continue in the current mode:

    <xsl:template match="*" mode="strip-all">
        <xsl:copy>
            <xsl:apply-templates mode="strip-all"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*" mode="strip-some">
        <xsl:copy>
            <xsl:apply-templates mode="strip-some"/>
        </xsl:copy>
    </xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I keep leading and trailing whitespaces? 2BOrNot2B XSLT 2 October 31st, 2008 11:35 AM
preserving whitespaces in attribute values? kaps77 XSLT 7 August 13th, 2007 11:48 AM
Preserved leading and tailing whitespaces problem ivan603 XML 0 February 7th, 2006 10:18 PM
checking for any whitespaces furia_cross Javascript How-To 1 September 7th, 2005 05:58 PM
Where are my whitespaces going? dimondwoof XSLT 3 July 17th, 2003 08:02 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.