2012-11-09 90 views
1

有關XML & XSL (我使用SharePoint 2007)XML/XSL:如何比較兩個節點並根據比較結果設置另一個節點的值?

你如何比較兩個節點的一個問題?

在下面的例子中,我想比較'promotionprice'和'price'。

如果'promotionprice'等於或大於'price',那麼'OK'應該是'NO'。 如果'promotionprice'小於'price',那麼'OK'應該是'YES'。

我不確定我是否使用了正確的語法,因爲在sharepoint中它不起作用,它總是給出YES。

XML example: 

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<catalog> 
<cd> 
<title>Empire Burlesque</title> 
<artist>Bob Dylan</artist> 
<country>USA</country> 
<company>Columbia</company> 
<price>10.90</price> 
<promotionprice>15.00</promotionprice> 
<year>1985</year> 
<OK>Yes</OK> 
</cd> 
<cd> 
<title>Hide your heart</title> 
<artist>Bonnie Tyler</artist> 
<country>UK</country> 
<company>CBS Records</company> 
<price>9.90</price> 
<promotionprice>5.00</promotionprice> 
<year>1988</year> 
<OK>Yes</OK> 
</cd> 
</catalog> 


XSL example: 
... 
<!-- title node --> 
<td> 
<xsl:value-of select="@title"/> 
</td> 
<!-- artist node --> 
<td> 
<xsl:value-of select="@artist"/> 
</td> 
... 
<!-- OK node --> 
<td> 
<xsl:choose> 
<xsl:when test="promotionprice &gt;= price"> 
<xsl:value-of select="'NO'"/> 
</xsl:when> 
<xsl:otherwise> 
<xsl:value-of select="'YES'"/> 
</xsl:otherwise> 
</xsl:choose> 
</td> 

WANTED RESULT: 
<table border="1"> 
<tr> 
    <td>Empire Burlesque</td> 
    <td>Bob Dylan</td> 
    <td>USA</td> 
    <td>Columbia</td> 
    <td>10.90</td> 
    <td>15.00</td> 
    <td>1985</td> 
    <td>NO</td> 
</tr> 
<tr> 
    <td>Hide your heart</td> 
    <td>Bonnie Tyler</td> 
    <td>UK</td> 
    <td>CBS Records</td> 
    <td>9.90</td> 
    <td>5.00</td> 
    <td>1988</td> 
    <td>YES</td> 
</tr> 
</table> 

非常感謝!

+1

請編輯問題並提供轉換的確切結果。 –

回答

1

這種轉變

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="/*"> 
    <table border="1"> 
     <xsl:apply-templates/> 
    </table> 
</xsl:template> 

<xsl:template match="cd"> 
    <tr><xsl:apply-templates/></tr> 
</xsl:template> 

<xsl:template match="title|artist"> 
    <td><xsl:value-of select="."/></td> 
</xsl:template> 

<xsl:template match="OK"> 
    <td> 
    <xsl:value-of select= 
    "substring('YESNO', 4 -3*(../price >= ../promotionprice),3)"/> 
    </td> 
</xsl:template> 
<xsl:template match="text()"/> 
</xsl:stylesheet> 

時所提供的XML文檔應用:

<catalog> 
    <cd> 
     <title>Empire Burlesque</title> 
     <artist>Bob Dylan</artist> 
     <country>USA</country> 
     <company>Columbia</company> 
     <price>10.90</price> 
     <promotionprice>15.00</promotionprice> 
     <year>1985</year> 
     <OK>Yes</OK> 
    </cd> 
    <cd> 
     <title>Hide your heart</title> 
     <artist>Bonnie Tyler</artist> 
     <country>UK</country> 
     <company>CBS Records</company> 
     <price>9.90</price> 
     <promotionprice>5.00</promotionprice> 
     <year>1988</year> 
     <OK>Yes</OK> 
    </cd> 
</catalog> 

生產什麼我是通緝的結果:

<table border="1"> 
    <tr> 
     <td>Empire Burlesque</td> 
     <td>Bob Dylan</td> 
     <td>NO</td> 
    </tr> 
    <tr> 
     <td>Hide your heart</td> 
     <td>Bonnie Tyler</td> 
     <td>YES</td> 
    </tr> 
</table> 
+0

對於'substring()'而不是'xsl:choose'。 @ dee-ln - 如果你把'match ='title | artist''改成'match =「*」'並移除''正在尋找。 –

+0

@DevNull,不客氣。 –