Problem with XSL

Discussion in 'XML' started by infiniti, Feb 2, 2006.

  1. infiniti

    infiniti Guest

    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.



    Thanks for your help.

    Amber
    infiniti, Feb 2, 2006
    #1
    1. Advertising

  2. infiniti

    Peter Flynn Guest

    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/
    Peter Flynn, Feb 2, 2006
    #2
    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. Kevin Flood
    Replies:
    0
    Views:
    1,011
    Kevin Flood
    Sep 8, 2004
  2. Kevin Flood
    Replies:
    1
    Views:
    2,718
    Kevin Flood
    Sep 13, 2004
  3. Klaus Friese
    Replies:
    0
    Views:
    460
    Klaus Friese
    Nov 22, 2004
  4. Ray Tayek
    Replies:
    3
    Views:
    504
    Ray Tayek
    Nov 30, 2003
  5. Replies:
    1
    Views:
    3,590
    A. Bolmarcich
    May 27, 2005
Loading...

Share This Page