简体   繁体   English

将XSLT 1.0转换为2.0

[英]Convert XSLT 1.0 to 2.0

I need help converting an XSLT 1.0 file to 2.0 (so I can use the XSLT 2.0 replace() function call). 我需要帮助将XSLT 1.0文件转换为2.0(所以我可以使用XSLT 2.0 replace()函数调用)。

I've Googled, searched different books and SO with no success. 我用谷歌搜索,搜索不同的书籍,但没有成功。 I tried changing version="1.0" to 2.0 and changing html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" to simply html but this all just results in XSLTProcessor errors. 我尝试将version="1.0"更改为2.0并将html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"更改为简单的html但这只是结果在XSLTProcessor错误中。

Any help is greatly appreciated. 任何帮助是极大的赞赏。 Thanks in advance. 提前致谢。

Here is my XSLT: 这是我的XSLT:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" indent="no"
        doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" 
        doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" encoding="ISO-8859-1" /> 
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<xsl:element name="meta">
<xsl:attribute name="name">description</xsl:attribute>
<xsl:attribute name="content"><xsl:value-of select="EventList/title"/></xsl:attribute>
</xsl:element>
<xsl:element name="link"><xsl:attribute name="rel">alternate</xsl:attribute><xsl:attribute name="type">application/rss+xml</xsl:attribute><xsl:attribute name="title">RSS</xsl:attribute><xsl:attribute name="href"><xsl:value-of select="EventList/rssURL"/></xsl:attribute></xsl:element>
</head>
<body>
 <xsl:apply-templates select="EventList" />
</body>
</html>
</xsl:template>
<xsl:template match="EventList">
 <xsl:choose>
  <xsl:when test="Event">
   <xsl:apply-templates select="Event"/>
  </xsl:when>
  <xsl:otherwise>
   <div class="eventItem">
    <div class="eventItemText">
     <p>There are currently no events posted for this category.</p>
    </div>
   </div>
  </xsl:otherwise>
 </xsl:choose>
</xsl:template>
<!-- List -->
<xsl:template match="Event">
 <li>
  <!-- Title -->
  <xsl:if test="eventStatus = 2"><xsl:value-of select="eventStatusString"/> - </xsl:if>
  <xsl:element name="a"><xsl:attribute name="href">http://events.stanford.edu/e/e/?d=<xsl:value-of select="replace(detailpath,'/events/','')"/></xsl:attribute><xsl:attribute name="id"><xsl:value-of select="eventID"/></xsl:attribute><xsl:attribute name="rel">external</xsl:attribute>
   <xsl:value-of select="title" disable-output-escaping="yes"/>
  </xsl:element>
  <!-- Date and time -->
  <xsl:element name="a"><xsl:attribute name="href">http://events.stanford.edu/e/details.php?detailpath=<xsl:value-of select="detailpath"/></xsl:attribute><xsl:attribute name="rel">external</xsl:attribute>
   <xsl:choose>
    <xsl:when test="repeatRuleID &gt; 0">
     Ongoing <xsl:value-of select="repeatRuleText"/> from <xsl:value-of select="beginDate"/> through <xsl:value-of select="repeatUntilDate"/>.
     <xsl:if test="repeatRuleID=99">See details for exact dates and times.</xsl:if>
    </xsl:when>
    <xsl:otherwise>
     <xsl:if test="string(beginDay)"><xsl:value-of select="beginDay"/>, </xsl:if>
     <xsl:value-of select="beginDate"/>.
    </xsl:otherwise>
   </xsl:choose>
   <xsl:if test="repeatRuleID!=99">
    <xsl:if test="string(beginTime)">
     <xsl:text disable-output-escaping="yes"></xsl:text><xsl:value-of select="beginTime"/>.
    </xsl:if>
   </xsl:if>
  </xsl:element>
  <!-- Location -->
  <xsl:element name="a"><xsl:attribute name="href">http://events.stanford.edu/e/details.php?detailpath=<xsl:value-of select="detailpath"/></xsl:attribute><xsl:attribute name="rel">external</xsl:attribute>
   <xsl:value-of select="locationText"/>
  </xsl:element>
 </li>
</xsl:template>
</xsl:stylesheet>

In addition to changing the version attribute to '2.0' , you need to feed your XSLT 2.0 code to an XSLT 2.0 processor . 除了将version属性更改为'2.0' ,还需要将XSLT 2.0代码提供给XSLT 2.0处理器

At present, some of the XSLT 2.0 processors I use are: 目前,我使用的一些XSLT 2.0处理器是:

Do note that an existing XSLT 1.0 code may behave differently under XSLT 2.0 -- the most obvious differences are that <xsl:value-of> no longer produces the string value of only the first node from a node-set, and that in XSLT 2.0 the dreaded RTF type has been eliminated, so no xxx:node-set() extension function is needed/provided. 请注意 ,现有的XSLT 1.0代码在XSLT 2.0下可能会有不同的行为 - 最明显的区别是<xsl:value-of>不再生成来自节点集的第一个节点的字符串值,以及XSLT中的字符串值2.0已经消除了可怕的RTF类型,因此不需要/提供xxx:node-set()扩展功能。

You should also check the non normative XSLT 2.0 Appendix J Changes from XSLT 1.0 . 您还应该检查XSLT 1.0中的非规范XSLT 2.0附录J 更改

This section lists all known cases where a stylesheet that was valid (produced no errors) under XSLT 1.0, and whose behavior was fully specified by XSLT 1.0, will produce different results under XSLT 2.0. 本节列出了所有已知的情况,其中样式表在XSLT 1.0下有效(没有产生错误),并且其行为完全由XSLT 1.0指定,在XSLT 2.0下将产生不同的结果。

While you're converting the code, you should get rid of those ugly disable-output-escaping="yes" attributes. 当你转换代码时,你应该摆脱那些丑陋的disable-output-escaping =“yes”属性。 They are almost certainly not wanted. 他们几乎肯定不想要。 The use of this attribute usually indicates that it was written by a novice with a poor understanding of the language. 使用这个属性通常表明它是由一个对语言理解不足的新手编写的。 This also applies to the use of verbose constructs like 这也适用于使用冗长的结构

<xsl:element name="a"><xsl:attribute name="href">http://events.stanford.edu/e/e/?d=<xsl:value-of select="replace(detailpath,'/events/','')"/></xsl:attribute><xsl:attribute name="id"><xsl:value-of select="eventID"/></xsl:attribute><xsl:attribute name="rel">external</xsl:attribute>
   <xsl:value-of select="title" disable-output-escaping="yes"/>
  </xsl:element>

that could be replaced by the much more readable 可以被更可读的替代

<a href="http://events.stanford.edu/e/e/?d={replace(detailpath,'/events/','')}"
   id="{@eventID}" rel="external">
  <xsl:value-of select="title"/>
</a>

Generally, some refactoring is long overdue for this code. 通常,对于此代码,一些重构早就应该进行了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM