Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - Problem with XSL [Thread Next] Re: Problem with XSLTo: NULL Date: 2/2/2006 10:29:00 PM infiniti wrote:
> Hi,
> I am trying to transform this xml section using xsl, but there
> is a glitch in the xsl file which I do not know how to rectify. Any
> help is appreciated.
>
> XML:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xml>
> <RelatedKeyword><Common>anushka</Common><Searches>1032</Searches></RelatedKeyword>
> <RelatedKeyword><Common>anushka
> spa</Common><Searches>301</Searches></RelatedKeyword>
> <RelatedKeyword><Common>anushka day
> spa</Common><Searches>81</Searches></RelatedKeyword>
>
> <GetMarketStateResponse searchTerm="anushka">
> <Listing rank="1" bid="0.31" market="US"/>
> <Listing rank="2" bid="0.22" market="US"/>
> <Listing rank="3" bid="0.20" market="US"/>
> <Listing rank="4" bid="0.10" market="US"/>
> </GetMarketStateResponse>
> <GetMarketStateResponse searchTerm="anushka spa">
> <Listing rank="1" bid="0.11" market="US"/>
> <Listing rank="2" bid="0.22" market="US"/>
> <Listing rank="3" bid="0.40" market="US"/>
> <Listing rank="4" bid="0.34" market="US"/>
> </GetMarketStateResponse>
> </xml>
>
>
> XSL:
> <?xml version="1.0"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> <xsl:template match="/">
> <xsl:apply-templates select="xml" />
> </xsl:template>
> <xsl:variable name="basename" select='bid' />
> <xsl:template match="xml">
> <xml>
> <xsl:for-each select="./GetMarketStateResponse">
> <row>
> <xsl:attribute name="RelatedKeyword">
> <xsl:value-of select="@searchTerm" />
> </xsl:attribute>
> <xsl:variable name="node"
> select="//RelatedKeyword/Common[@name=current()/@searchTerm]" />
> <xsl:attribute name="Searches">
> <xsl:value-of select="$node/Searches" />
> </xsl:attribute>
> <xsl:for-each select="./Listing">
> <xsl:variable name="rank" select="@rank" />
> <xsl:attribute name="bid{$rank}">
> <xsl:value-of select="@bid" />
> </xsl:attribute>
> </xsl:for-each>
> </row>
> </xsl:for-each>
> </xml>
> </xsl:template>
> </xsl:stylesheet>
>
>
>
> Output that I am getting:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xml>
> <row RelatedKeyword="anushka" Searches="" bid1="0.31" bid2="0.22"
> bid3="0.20" bid4="0.10"/>
> <row RelatedKeyword="anushka spa" Searches="" bid1="0.11" bid2="0.22"
> bid3="0.40" bid4="0.34"/>
> <row RelatedKeyword="anushka day spa" Searches=""/>
> </xml>
>
> As you can see, that I am not getting the value for the Searches
> attribute. I am not too familiar with Xpath expressions.
The problem is that your value for the $node variable refers to a
non-existent attribute "name", so $node will always be null.
If you wanted $node to be the Common element which has a content value
matching that of the current()/searchTerm then you need something like
<xsl:variable name="node"
select="//RelatedKeyword/Common[.=current()/@searchTerm]"/>
You may want to use
[normalize-space(.)=normalize-space(current()/@searchTerm)]
if there is any chance of your data containing leading or trailing or
embedded multiple spaces, newlines, TABs, etc.
The related problem is that you refer to $node/Searches, but if $node is
a Common element node, it doesn't have any children called Searches. You
need $node/following-sibling::Searches instead (or
$node/following-sibling::Searches[1] if there could ever be multiple
Searches elements following a Common element).
///Peter
--
XML FAQ: http://xml.silmaril.ie/
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
