Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

c# - forcing xslt to use version 2 with xslCompiledTransform

I have following xslt , I need to use xslt version 2.0 functions like 'format-date'. How do I declare the Xsl sheet to use version 2.0 using XsltCompiledTransform class (c#, .net 4.5).

 <xsl:stylesheet version="2.0"
               xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
               xmlns="http://www.w3.org/TR/xhtml1/strict"
               xmlns:msxsl='urn:schemas-microsoft-com:xslt' 
               xmlns:var='urn:var' 
               xmlns:JS='urn:JS'
                >
  <xsl:output method="html"/>
  <xsl:variable name="n" select="1"/>
  <xsl:template match="/NewDataSet">
    <html>
      <head>
        <style>
          table{border-collapse:collapse;font-family:"Verdana";}
          table,td{border:1px solid black;color:black; background-color:white;font-family:"Verdana";}
          table,th{border:1px solid balck;background-color:black;color:white;font-family:"Verdana"; }
          .rt{color:red;font-family:"Verdana";}
          .nt{color:black;font-family:"Verdana";}
          .redb{color:yellow; background-color:red;font-family:"Verdana";}
          .greenb{color:white;background-color:green;font-family:"Verdana";}
          .blackb{color:white;background-color:black;font-family:"Verdana";}
        </style>
        <title>EDI validation Result </title>
      </head>
      <body>
        <p class="nt">
          EDI validation result  of the PO <span class="rt"><xsl:value-of select="info/pono"/></span>
          received from <xsl:value-of select="info/CustomerName"/>.
        </p>

        <table>
           <th class="blackb" >Position</th>
          <th class="blackb"> Item Code </th>
          <th class="blackb">UoM</th>
          <th class="blackb"> Ordered Qty .</th>
          <th class="blackb">Ship Request</th>
          <th class="blackb"> Net-Quoted </th>
          <th class="blackb"> Net-Catalog </th>
          <th class="blackb">Status</th>
          <xsl:for-each select="Table">
            <tr>
              <xsl:choose>
                <xsl:when test="Status !=''">
                  <xsl:value-of disable-output-escaping="yes" select="JS:IncBlines()"/>
                  <td class="redb"><xsl:value-of select="Position"/></td>
                  <td class="redb"><xsl:value-of select="ItemCode "/></td>
                  <td class="redb"><xsl:value-of select="UoM"/></td>
                  <td class="redb"><xsl:value-of select="QtyOrdered"/></td>
                  <td class="redb"><xsl:value-of select="format-date(RequiredBy,'D1o [MNn] [Y0001]')"/></td>
                  <td class="redb"><xsl:value-of select="PriceQuoted"/></td>
                  <td class="redb"><xsl:value-of select="Net"/></td>
                  <td class="redb"><xsl:value-of select="Status"/></td>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="JS:IncGlines()"/>

                  <td class="greenb"><xsl:value-of select="Position"/></td>
                  <td class="greenb"><xsl:value-of select="ItemCode"/></td>
                  <td class="greenb"><xsl:value-of select="UoM"/></td>
                  <td class="greenb"><xsl:value-of select="QtyOrdered"/></td>
                  <td class="greenb"><xsl:value-of select="format-date(RequiredBy,'D1o [MNn] [Y0001]')"/></td>


                  <td class="greenb"><xsl:value-of select="PriceQuoted"/></td>
                  <td class="greenb"><xsl:value-of select="Net"/></td>
                  <td class="greenb"><xsl:value-of select="Status"/>OK</td>

                 </xsl:otherwise>
                </xsl:choose>
            </tr>
          </xsl:for-each>
        </table>

        <xsl:if test="JS:GetBlines() &gt; 0"    >

          <p class="nt">

              The order validation has failed,

              The order will not be processesed as there are <xsl:value-of select ="JS:GetBlines()"/> lines in error.
              <p class="rt">

                The P.O is rejected as per agreed processing rules.


          </p>
          </p>

        </xsl:if>

        <xsl:if test="JS:GetBlines() &lt; 1">
          <p class="nt">
          The Order validated succesfully.
          Will e-mail Order Acknoledgement (non-edi) shortly.
          </p>
        </xsl:if>

      </body>
    </html>
  </xsl:template>
  <msxsl:script language='JScript' implements-prefix='JS'>

    <![CDATA[
    var j :int=0;
    var blines:int =0;
    var glines:int=0;
    function Inc(current)
    {j=j+current;

  return  j+current;
    }
    function IncBlines()
    {
     blines++;
    }
    function IncGlines()
    {
    glines++;
    }

    function GetBlines()
    {
    return blines;
    }
    function GetGlines()
    {
    return glines;
    }
]]>
  </msxsl:script>
</xsl:stylesheet>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As Martin wrote, Microsoft's processor only supports 1.0 - even now, in 2016. I had a similar problem (I needed to do work with regular expressions within my XSLT) and I resolved it by using inline C# to do the work.

I based my solution on this example: XSLT Stylesheet Scripting Using <msxsl:script> (MSDN)

You'll have to add a few things to your xsl:stylesheet node. Mine looks like:

<xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:msxsl="urn:schemas-microsoft-com:xslt"
            xmlns:user="urn:my-scripts"
            exclude-result-prefixes="msxsl">

Then, you'll define your script. Mine was something like:

<!-- Script to check for URLs in values -->
<msxsl:script language="C#" implements-prefix="user">
    <![CDATA[
      public string ExtractUrl(string str)
      {
          return Regex.Match(str, @"(https?://(www.)?[-a-zA-Z0-9@:%._+~#=]{2,256}.[a-z]{2,6}([-a-zA-Z0-9@:%_+.~#?&//=]*))").Value;
      }
    ]]>
</msxsl:script>

I was then able to invoke the function within my XSLT:

<xsl:variable name="extractedUrl" select="user:ExtractUrl(.)"></xsl:variable>

It's not mentioned on the tutorial page, but where I processed the XSLT on the server, I also had to create an XsltSettings object to enable script execution:

XsltSettings settings = new XsltSettings(false, true); // enable script execution
XsltCompiledTransform transform = new XslCompiledTransform();
transform.Load("template.xsl", settings, new XmlUrlResolver());

Of course, consider security - make sure your XSLT file contains only trusted and/or sanitized input if you're allowing the execution of arbitrary C# scripts.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...