I
infiniti
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
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