Question regarding strings and spaces...

Here should go questions about transforming XML with XSLT and FOP.
drshagg
Posts: 2
Joined: Thu Jan 31, 2008 8:03 pm

Question regarding strings and spaces...

Post by drshagg »

Help! I am an xslt newbie working on two different xslts that essentially do the same thing. They are intended to strip out the year, make, and model from a text string. One xslt is working correctly, but the other is not. I am using the substring-after command to look for the space in the string and return the remaining value. For some reason though the substring-after command is not working. It is instead returning nothing. Can anyone tell me why this is not working?

This feed and xslt works correctly.
Feed -
<?xml version="1.0" encoding="utf-8" ?>
<vpage
rurl="http://www.mckaig.net/new.php?i=0&mid=0 ... x=0&pmin=0"
name="McKraig" version="1">
<tag name="SetName">
<value>
<p>0</p>
</value>
<tag name="year_make_model_price">
<value>
<p>2002 SATURN: $9,495</p>
</value>
</tag>
<tag name="year_make_model_price">
<value>
<p>2001 GMC JIMMY: $7,995</p>
</value>
</tag>
</tag>
</vpage>

Xslt -
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes"/>
<xsl:template match="vpage">
<CARS>
<xsl:apply-templates select="tag/tag" mode="car"/>
</CARS>
</xsl:template>

<xsl:template match="tag" mode="car">
<xsl:variable name="year_make_model_price">
<xsl:value-of select="value/p"/>
</xsl:variable>

<xsl:variable name="year">
<xsl:value-of select="substring($year_make_model_price,1,5)"/>
</xsl:variable>

<xsl:variable name="make_model_price">
<xsl:value-of select="substring-after($year_make_model_price,$year)"/>
</xsl:variable>

<xsl:variable name="make_model">
<xsl:value-of select="substring-before($make_model_price,':')"/>
</xsl:variable>

<xsl:variable name="make">
<xsl:choose>
<xsl:when test="contains($make_model,' ')">
<xsl:value-of select="substring-before($make_model,' ')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$make_model"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

<xsl:variable name="model">
<xsl:value-of select="substring-after($make_model,$make)"/>
</xsl:variable>

<YEAR>
<xsl:value-of select="$year"/>
</YEAR>

<MAKE>
<xsl:value-of select="$make"/>
</MAKE>

<MODEL>
<xsl:value-of select="$model"/>
</MODEL>
</xsl:template>
</xsl:stylesheet>

Returns -
<?xml version="1.0" encoding="ISO-8859-1"?>
<CARS>
<YEAR>2002 </YEAR>
<MAKE>SATURN</MAKE>
<MODEL/>
<YEAR>2001 </YEAR>
<MAKE>GMC</MAKE>
<MODEL> JIMMY</MODEL>
</CARS>
_______________________________
This is the feed and xslt that are not working.

Feed -
<?xml version="1.0" encoding="utf-8" ?>
<data>
<vpage agentId="64f4bc8a-2558-4e8c-b8ad-b46bed0cc352"
rurl="http://alliancemazdakia.com/alliance/results.asp">
<vpage id="N12" url="http://alliancemazdakia.com/alliance/results.asp" framepath="/"
schemaId="1d95b865-0ea6-473f-aae8-f47c1e67d4d1"
rurl="http://alliancemazdakia.com/alliance/results.asp" name="Extract" completed="true">
<headers>
<header name="Date" value="Wed, 30 Jan 2008 17:50:59 GMT"/>
<header name="Content-Length" value="18706"/>
<header name="Content-Type" value="text/html"/>
</headers>
<vpage id="I13-14" url="http://alliancemazdakia.com/alliance/detail.asp?ID=1420"
framepath="/" schemaId="6a30ed3c-cb0a-42ff-aaff-4813fb06363a"
rurl="http://alliancemazdakia.com/alliance/detail.asp?ID=1420" name="Extract">
<headers>
<header name="Date" value="Wed, 30 Jan 2008 17:51:00 GMT"/>
<header name="Content-Length" value="14700"/>
<header name="Content-Type" value="text/html"/>
</headers>
<tag name="year_make_model">
<value>2006 kia spectra</value>
</tag>
</vpage>
<vpage id="I13-721" url="http://alliancemazdakia.com/alliance/detail.asp?ID=1421"
framepath="/" schemaId="6a30ed3c-cb0a-42ff-aaff-4813fb06363a"
rurl="http://alliancemazdakia.com/alliance/detail.asp?ID=1421" name="Extract">
<headers>
<header name="Date" value="Wed, 30 Jan 2008 17:51:05 GMT"/>
<header name="Content-Length" value="14818"/>
<header name="Content-Type" value="text/html"/>
</headers>
<tag name="year_make_model">
<value>2007 NISSAN MAXIMA</value>
</tag>
</vpage>
</vpage>
</vpage>
</data>

XSLT -
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes"/>
<xsl:template match="data">
<CARS>
<xsl:apply-templates select="vpage/vpage/vpage" mode="car"/>
</CARS>
</xsl:template>

<xsl:template match="vpage" mode="car">
<xsl:variable name="year_make_model">
<xsl:value-of select="tag/value"/>
</xsl:variable>

<xsl:variable name="year">
<xsl:value-of select="substring($year_make_model,1,5)"/>
</xsl:variable>

<xsl:variable name="make_model">
<xsl:value-of select="substring-after($year_make_model,$year)"/>
</xsl:variable>

<xsl:variable name="make">
<xsl:value-of select="substring-before($make_model,' ')"/>
</xsl:variable>

<xsl:variable name="model">
<xsl:value-of select="substring-after($make_model,$make)"/>
</xsl:variable>

<YEAR>
<xsl:value-of select="$year"/>
</YEAR>

<MAKE>
<xsl:value-of select="$make"/>
</MAKE>

<MODEL>
<xsl:value-of select="$model"/>
</MODEL>
</xsl:template>
</xsl:stylesheet>

Returns -
<?xml version="1.0" encoding="ISO-8859-1"?>
<CARS>
<YEAR>2006 </YEAR>
<MAKE/>
<MODEL>kia spectra</MODEL>
<YEAR>2007 </YEAR>
<MAKE/>
<MODEL>NISSAN MAXIMA</MODEL>
</CARS>


THANKS!!!
jkmyoung
Posts: 89
Joined: Mon Mar 06, 2006 10:13 pm

Re: Question regarding strings and spaces...

Post by jkmyoung »

Strange. The output I get with the last 2 files is:

<CARS>
<YEAR>2006 </YEAR>
<MAKE>kia</MAKE>
<MODEL> spectra</MODEL>
<YEAR>2007 </YEAR>
<MAKE>NISSAN</MAKE>
<MODEL> MAXIMA</MODEL>
</CARS>

Also you might want to use instead:
<xsl:variable name="model">
<xsl:value-of select="substring-after($make_model,' ')"/>
</xsl:variable>

The difference might be the encoding of the space character you are getting from your feed. It might be a non-breaking space or a tab instead of a space. Whereas in this forum it must translate to a space, which is why I get the output above.
drshagg
Posts: 2
Joined: Thu Jan 31, 2008 8:03 pm

Re: Question regarding strings and spaces...

Post by drshagg »

Thanks so much. This was related to non breaking space. I tried using its encoded value in the substring-after command and it now works correctly. Thanks again!
Post Reply