×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

To be honest, i just start learning XSL from this morning after seeing your post,

本文发表在 rolia.net 枫下论坛and i have only IE5.5, which does not allow me to try some new functions(if, variable), so my codes are very very un-professional, please dont laugh at me. See if it helps or not

xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<root>
<entry>111</entry>
<entry b="1">222</entry>
<entry href="google">333</entry>
<entry href="yahoo" b="1">444</entry>
<entry></entry>
</root>


<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited with XML Spy v4.2 -->
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/TR/WD-xsl">

<xsl:template match="/">


<table border="1">

<xsl:for-each select="root/entry">
<tr>
<td>
<xsl:choose>
<xsl:when test=".[@b='1']">
<b>

<xsl:choose>
<xsl:when test="@href">
<xsl:element name="A">
<xsl:attribute name="HREF">
<xsl:value-of select="@href"/>
</xsl:attribute>
<xsl:value-of select="."/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>

</b>

</xsl:when>
<xsl:when test="@href">
<xsl:element name="A">
<xsl:attribute name="HREF">
<xsl:value-of select="@href"/>
</xsl:attribute>
<xsl:value-of select="."/>
</xsl:element>
</xsl:when>
<xsl:when test=".[.='']">
N/A
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>

</xsl:choose>
</td>
</tr>
</xsl:for-each>

</table>

</xsl:template>
</xsl:stylesheet>更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / 是XSL不好用还是我太苯?请教一个XSL问题
    以下是我的xml:
    <entry>111</entry>
    <entry b="1">222</entry>
    <entry href="google">333</entry>
    <entry href="yahoo" b="1">444</entry>
    <entry></entry>

    要显示在html的table里面:
    111 -> 正常
    222 -> 黑体<b>222</b>
    333 -> 链接<a href="google">333</a>
    444 -> 黑体并链接<a href="yahoo"><b>333</b></a>
    最后一项显示"NA"

    好象choose-when-otherwise里面不能再嵌套if之类的语句了。

    请各位高手牛人给个提示,谢谢。
    • you can use several "when"s and you may also need use more filter operators as well in your boolean expression.
      • when不能嵌套?我比较蠢,能否给点详细提示?
        • Hope this helps:
          http://www.w3schools.com/xsl/xsl_choose.asp
          • Thanks, but it can not solve my problem.
            • Why not? if you still cant get it, reply this message and i will post the code
              • Thanks, please see my code:
                以下是我的xml:
                <entry>111</entry>
                <entry b="1">222</entry>
                <entry href="google">333</entry>
                <entry href="yahoo" b="1">444</entry>
                <entry></entry>

                要显示在html的table里面:
                111 -> 正常
                222 -> 黑体<b>222</b>
                333 -> 链接<a href="google">333</a>
                444 -> 黑体并链接<a href="yahoo"><b>333</b></a>
                最后一项显示"NA"

                <xsl:template match="entry">
                <xsl:variable name="link" select="@href" />
                <xsl:variable name="b" select="@b" />

                <xsl:choose>
                <xsl:when test='boolean($link)'>
                <a href="{$link}"><xsl:value-of select="."/></a>
                </xsl:when>

                <xsl:otherwise>
                <xsl:value-of select="."/>
                </xsl:otherwise>
                </xsl:choose>

                </xsl:template>

                接下来该怎么写呢?我试图在when里面再用if来判断,可是不行。请指点。(如果可以,请把code贴出来,这样就省了一大堆解释的话了。)
                • To be honest, i just start learning XSL from this morning after seeing your post,
                  本文发表在 rolia.net 枫下论坛and i have only IE5.5, which does not allow me to try some new functions(if, variable), so my codes are very very un-professional, please dont laugh at me. See if it helps or not

                  xml:
                  <?xml version="1.0" encoding="ISO-8859-1"?>
                  <?xml-stylesheet type="text/xsl" href="test.xsl"?>
                  <root>
                  <entry>111</entry>
                  <entry b="1">222</entry>
                  <entry href="google">333</entry>
                  <entry href="yahoo" b="1">444</entry>
                  <entry></entry>
                  </root>


                  <?xml version="1.0" encoding="ISO-8859-1"?>
                  <!-- Edited with XML Spy v4.2 -->
                  <xsl:stylesheet
                  xmlns:xsl="http://www.w3.org/TR/WD-xsl">

                  <xsl:template match="/">


                  <table border="1">

                  <xsl:for-each select="root/entry">
                  <tr>
                  <td>
                  <xsl:choose>
                  <xsl:when test=".[@b='1']">
                  <b>

                  <xsl:choose>
                  <xsl:when test="@href">
                  <xsl:element name="A">
                  <xsl:attribute name="HREF">
                  <xsl:value-of select="@href"/>
                  </xsl:attribute>
                  <xsl:value-of select="."/>
                  </xsl:element>
                  </xsl:when>
                  <xsl:otherwise>
                  <xsl:value-of select="."/>
                  </xsl:otherwise>
                  </xsl:choose>

                  </b>

                  </xsl:when>
                  <xsl:when test="@href">
                  <xsl:element name="A">
                  <xsl:attribute name="HREF">
                  <xsl:value-of select="@href"/>
                  </xsl:attribute>
                  <xsl:value-of select="."/>
                  </xsl:element>
                  </xsl:when>
                  <xsl:when test=".[.='']">
                  N/A
                  </xsl:when>
                  <xsl:otherwise>
                  <xsl:value-of select="."/>
                  </xsl:otherwise>

                  </xsl:choose>
                  </td>
                  </tr>
                  </xsl:for-each>

                  </table>

                  </xsl:template>
                  </xsl:stylesheet>更多精彩文章及讨论,请光临枫下论坛 rolia.net
                  • I have just added a small patch for "N/A" staff, please check.
                    • 原子弹同志确实是个好同志啊,谢谢。
                      • 我其实也想帮你想来着, 因为 1.今天挺忙 2.XSLT确实别扭 3.看你总夸别人是好同志 所以我就没管你... :-P
                        • 你的MCAD考了吗?
                          • 谢谢你还替我惦记着呢 :-) 最近比较颓废, 去年借口说到今年再考, 现在想过了春节再说, 后来发现还有情人节, 某些纪念日什么的....还是等冬天没什么事儿的时候再说吧.. :-P
                            • 我过了2门了。。。:)
                              • @@ Wb Form, Windows Form, Web Service? 哪两门? 萝卜秧你看的是什么书呀, 借我看看吧, 有什么考题吗? 以请你吃饭. 如果这个忙都不肯帮, 实在太不够意思了吧!! 嘿嘿, 你说咱们什么时候见面呢? :-P
                        • 这东西用起来是太别扭。//我夸别人你就不理我,争宠?爱卿平身~~~~
                          • 嘿嘿...行, 下次我"热心"地把你往歪路上引你可别怪我....
                            • 孤不怪你
                  • Thank you so much!
                    You are really great! At last i worked it out, and here's my "very very un-professional" code (i'm also new to it, hehe) for your reference.

                    <xsl:template match="entry">

                    <xsl:variable name="link" select="@href" />
                    <xsl:variable name="b" select="@b" />

                    <xsl:choose>
                    <xsl:when test='boolean($link)'>
                    <a href="{$link}">

                    <xsl:choose>
                    <xsl:when test='boolean($b)'>
                    <b><xsl:value-of select="."/></b>
                    </xsl:when>
                    <xsl:otherwise>
                    <xsl:value-of select="."/>
                    </xsl:otherwise>
                    </xsl:choose>

                    </a>
                    </xsl:when>

                    <xsl:otherwise>

                    <xsl:choose>
                    <xsl:when test='boolean($b)'>
                    <b><xsl:value-of select="."/></b>
                    </xsl:when>
                    <xsl:otherwise>
                    <xsl:value-of select="."/>
                    </xsl:otherwise>
                    </xsl:choose>

                    </xsl:otherwise>
                    </xsl:choose>

                    </xsl:template>