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 function call replace ()).

I googled, searched for different books and without success. I tried changing version="1.0"to 2.0and changing html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"to simple html, but all this just leads to XSLTProcessor errors.

Any help is appreciated. Thanks in advance.

Here is my 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>
+3
source share
3 answers

In addition to changing the attribute versionon '2.0'you need to pass the XSLT 2.0 code to the XSLT 2.0 processor .

Currently, some of the XSLT 2.0 processors I'm using are:

, XSLT 1.0 - XSLT 2.0 - , <xsl:value-of> node node -set, XSLT 2.0 RTF , xxx:node-set().

+3

XSLT 2.0 J XSLT 1.0.

( ) XSLT 1.0, XSLT 1.0, XSLT 2.0.

+1

While you are converting the code, you must get rid of these ugly attributes disable-output-escaping = "yes". They almost certainly do not want. Using this attribute usually indicates that it was written by a beginner with a poor understanding of the language. This also applies to the use of detailed designs such as

<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>

which can be replaced by 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>

As a rule, some refactorings are long delayed for this code.

+1
source

Source: https://habr.com/ru/post/1786994/


All Articles