xslt_您在w3schools上没有学过的XSLT技巧

xslt

这是一些有用的xslt通用技巧的简短列表,而这些技巧在w3schools上并未讲授。

  1. 属性值模板
    W3C官方说明和示例

    这是当您要将动态值放在元素的属性中时。 除了使用<xsl:attribute>元素,您还可以简单地将xpath放在属性本身中。

    最常见的用法是创建超链接。

    例:
    源XML
    
    <item>
      <link>www.google.ca</link>
      <desc>Search Engine</desc>
    </item>
    <item>
      <link>http://www.mytelus.com/ncp_news/</link>
      <desc>news</desc>
    </item> 
    旧的XSLT:
    
    <xsl:template match="item">
      <a>
        <xsl:attribute name="href">
          <xsl:value-of select="link"/>
        </xsl:attribute>
        <xsl:value-of select="desc"/>
      </a>
    </xsl:template> 
    新的XSLT:
    
    <xsl:template match="item">
      <a href="{link}"><xsl:value-of select="desc"/></a>
    </xsl:template> 
    结果:
    
    <a href="www.google.ca">Search Engine</a>
    <a href="http://www.mytelus.com/ncp_news/">news</a> 
    现在不是更好了吗?

  2. Muenchian分组
    Jeni的XSLT页面:使用Muenchian方法进行分组
    这是一种按特定值将元素分组在一起的方法。 由于许多字段是重复的,因此它在解释数据库转储生成的数据时特别有用。 仅xslt 1.0才需要此功能,因为2.0已内置分组功能。

    例:
    
    <book>
      <title>How to Win Friends and Influence People</title>
      <author>Dale Carnegie</author>
    </book>
    <book>
      <title>Harry Potter and the Philosopher's Stone</title>
      <author>J.K. Rowling</author>
    </book>
    <book>
      <title>Harry Potter and the Deathly Hallows /title>
      <author>J.K. Rowling</author>
    </book> 
    在这里,我们要按作者对书籍进行分组,因此我们有xslt代码,如下所示:
    
    <xsl:key name="bookByAuthor" match="book" use="author"> 
    <xsl:for-each select="//book[count(.|key('bookByAuthor', author)[1]) = 1]">
      <author>
        <name><xsl:value-of select="author"/></name>
        <xsl:for-each select="key('bookByAuthor', author)">
          <book><xsl:value-of select="title"/></book>
        </xsl:for-each>
      </author> 
    输出:
    
    <author>
      <name>Dale Carnegie</name>
      <book>How to Win Friends and Influence People</book>
    </author>
    <author>
      <name>J.K. Rowling</name>
      <book>Harry Potter and the Philosopher's Stone</book>
      <book>Harry Potter and the Deathly Hallows</book>
    </author> 
  3. 轴数
    很多时候,您需要选择特定的节点,但是没有简单的方法来选择它们。 解? 使用轴! 最重要的如下:
    1. following-sibling ::和previous-sibling ::
    2.后代::(也写成.//)
    3.祖先:
    有关所有轴的列表,请参见链接。
    XSLT 1.0:轴

    这是一个人为的示例:
    
    <stock symbol="SYM">
      <price day="11022008" close="156">
      <price day="12022008" close="135">
      <price day="13022008" close="158">
      <price day="14022008" close="158">
      <price day="15022008" close="146">
    </stock> 
    为了找到前一天的更改,我们执行以下操作:
    
    <xsl:template match="stock">
      <table>
        <tr>
          <th>day</th>
          <th>close</th>
          <th>change</th>
        </tr>
        <xsl:apply-templates/>
      </table>
    </xsl:template>
    <xsl:template match="price">
    <tr>
      <td><xsl:value-of select="@day"></td>
      <td><xsl:value-of select="@close"></td>
      <td>
        <xsl:choose>
          <xsl:when test="position() = 1">-</xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="@close - preceding-sibling::price[1]/@close"/>
          </xsl:otherwise>
        </xsl:choose>
      </td>
    </tr> 
    因此,如果我们要检查的日期不是第一天,则从这一天中减去先前价格的值,以查看更改。 请注意价格后使用[1]。 这告诉处理器,我们只希望第一个价格节点在我们正在处理的价格节点之前。 轴上的节点具有基于轴的位置。

    结果:
    
    <table>
      <tr><th>day</th><th>close</th><th>change</th></tr>
      <tr><td>11022008</td><td>156</td><td>-</td></tr>
      <tr><td>12022008</td><td>135</td><td>-21</td></tr>
      <tr><td>13022008</td><td>158</td><td>23</td></tr>
      <tr><td>14022008</td><td>158</td><td>0</td></tr>
      <tr><td>15022008</td><td>146</td><td>-12</td></tr>
    </table> 
  4. 命名空间
    很多时候,有人抱怨由于元素开头的命名空间前缀而导致某人无法选择元素。

    
    <atta:root xmlns:atta="www.atta.com">
       <atta:table>
         <atta:row>
            <atta:city>Vancouver</atta:city>
            <atta:hnde>24</atta:hnde>
         </atta:row>
         <atta:row>
            <atta:city>Montreal</atta:city>
            <atta:hnde>36</atta:hnde>
         </atta:row>
       </atta:table>
     </atta> 
    要访问Vancouver下的24,xpath可能类似于:
    “ / atta:root / atta:table / atta:row [atta:city ='Vancouver'] / atta:hnde”


  5. 值得一提的是:节点集功能。
    由于这并不是严格意义上的xslt,而是依赖于扩展功能,因此我没有提供它。 它将xsl变量视为节点集(在XSLT 2.0中是自动的)。 例如。 exsl:node-set()xmlns:exslt =“ http://exslt.org/common”或
    msxsl:node-set()xmlns:msxsl =“ urn:schemas-microsoft-com:xslt”
    EXSLT-exsl:node-set
    支持msxsl:node-set()函数

一般参考清单:

  1. XSLT教程
  2. XSLT参考
    XSLT 1.0的良好参考。 特别是轴。
  3. XSLT参考
    XSL元素
    XPath,XQuery和XSLT函数参考
    XPath 2.0功能。

如果您有任何其他提示应在此处添加,请随时回答。

翻译自: https://bytes.com/topic/xml/insights/768835-xslt-tips-you-dont-learn-w3schools

xslt

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值