By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,046 Members | 2,105 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,046 IT Pros & Developers. It's quick & easy.

XSLT "choose" incorrectly assigning the "Test"

P: n/a
hiya,
i have an xml doc, and I'd like to assign lookup values.
Eg, if the "PRODUCT-TYPE" is 6, then I insert "bike" into the innerText.

<code>
<rows>
<row>
<PRODUCT-TYPE>6</PRODUCT-TYPE>
<PRODUCT-DATE>01/01/2003</PRODUCT-DATE>
</row>
<row>
<PRODUCT-TYPE>7</PRODUCT-TYPE>
<PRODUCT-DATE>01/01/2004</PRODUCT-DATE>
</row>
</rows>
<code>
XSLT:
<code>
<xsl:for-each select="PRODUCT-TYPE">
<xsl:when test="@'PRODUCT-TYPE'">
<xsl:choose> <xsl:when test=".='6'">Bike</xsl:when>
<xsl:when test=".='7'">Car</xsl:when>
<xsl:when test=".='1'">Van</xsl:when>
<xsl:otherwise>UNDEFINED</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</code>

I get the error that "stylesheet wouldn't load".
Any ideas?I think that my syntax near the following lines might be wrong.

<xsl:for-each select="PRODUCT-TYPE">
<xsl:when test="@'PRODUCT-TYPE'">

Can anyone help offer a 2nd pair of eyes?

many thanks,
yogi

--
Message posted via http://www.dotnetmonster.com
Nov 12 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
chris yoker via DotNetMonster.com wrote:

<xsl:for-each select="PRODUCT-TYPE">
<xsl:when test="@'PRODUCT-TYPE'">


@'PRODUCT-TYPE' is syntactically incorrect XPath expression. What did
you mean in plain English?

--
Oleg Tkachenko [XML MVP, MCP]
http://blog.tkachenko.com
Nov 12 '05 #2

P: n/a
in English, i just meant:
"obtain the current value of current <PRODUCT-TYPE> node"

does this make sense?

yogi

--
Message posted via http://www.dotnetmonster.com
Nov 12 '05 #3

P: n/a
chris yoker via DotNetMonster.com wrote:
in English, i just meant:
"obtain the current value of current <PRODUCT-TYPE> node"

does this make sense?


That's redundant. There is only one current node and at this point it's
always PRODUCT-TYPE element (because of xsl:for-each). xsl:when can be
used only within xsl:choice. And it's not clear where you want to obtain
the value too. In short - just get rid of that xsl:when.

--
Oleg Tkachenko [XML MVP, MCP]
http://blog.tkachenko.com
Nov 12 '05 #4

P: n/a
cheers Oleg
I've removed the "<xsl:when>"

my XSLT is now as follows:

<code>
<xsl:for-each select="PRODUCT-TYPE">
<xsl:choose>
<xsl:when test=".='6'">Bike</xsl:when>
<xsl:when test=".='7'">Car</xsl:when>
<xsl:when test=".='1'">Van</xsl:when>
<xsl:otherwise>UNDEFINED</xsl:otherwise>
</xsl:choose>
<xsl:copy-of select="." />
</xsl:for-each>
</code>

However, the output is incorrect.
<code>
<Rows>
<row>Bike<PRODUCT-TYPE>6</PRODUCT-TYPE><PRODUCT_DATE>30/03/2004</PRODUCT-
DATE></row>

//I've included blank lines for clarity.

<row>Car<PRODUCT-TYPE>7</PRODUCT-TYPE><PRODUCT_DATE>30/03/2004</PRODUCT-
DATE></row>
</code>
</Rows>

I want the transform to "overwrite the value "6" with the value "Bike" etc.
Ideally, the transform output would be:
<code>
<Rows>
<row>
<PRODUCT-TYPE>Bike</PRODUCT-TYPE>
<PRODUCT_DATE>30/03/2004</PRODUCT-DATE>
</row>
<row>
<PRODUCT-TYPE>Car</PRODUCT-TYPE>
<PRODUCT_DATE>30/03/2004</PRODUCT-DATE>
</row>
</code>
</Rows>

So, at this stage i've no idea where I am going wrong.
All help appreciated ;-)
ta,
yogi

--
Message posted via http://www.dotnetmonster.com
Nov 12 '05 #5

P: n/a
chris yoker via DotNetMonster.com wrote:

I want the transform to "overwrite the value "6" with the value "Bike" etc.
Ideally, the transform output would be:
<code>
<Rows>
<row>
<PRODUCT-TYPE>Bike</PRODUCT-TYPE>
<PRODUCT_DATE>30/03/2004</PRODUCT-DATE>
</row>
<row>
<PRODUCT-TYPE>Car</PRODUCT-TYPE>
<PRODUCT_DATE>30/03/2004</PRODUCT-DATE>
</row>
</code>
</Rows>

So, at this stage i've no idea where I am going wrong.
All help appreciated ;-)


The simplest way of doing "keep all as is and change bits"
transformations is using identity transform rule:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="PRODUCT-TYPE">
<PRODUCT-TYPE>
<xsl:choose>
<xsl:when test=".='6'">Bike</xsl:when>
<xsl:when test=".='7'">Car</xsl:when>
<xsl:when test=".='1'">Van</xsl:when>
<xsl:otherwise>UNDEFINED</xsl:otherwise>
</xsl:choose>
</PRODUCT-TYPE>
</xsl:template>
</xsl:stylesheet>
--
Oleg Tkachenko [XML MVP, MCP]
http://blog.tkachenko.com
Nov 12 '05 #6

P: n/a
cheers Oleg,

My xslt was nowhere near as elegant.
I was calling a "rows" template then a "fields" template.

Thanks for the help.
yogi

--
Message posted via http://www.dotnetmonster.com
Nov 12 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.