Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Problem with XSL

From: Peter Flynn <peter.nosp@-.--------.-->
To: 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/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent