如何将参数传递到.xsl文件中的$document.ready

问题描述

一个Windows应用程序,当单击按钮时,该应用程序会吐出网页。网页的格式内置于.xsl文件中,而内容填充于.cs文件中。 .xsl标记中包含一些.css,标记中包含jquery,后跟一些html和.xsl代码。 在页面加载时,需要更改其颜色在标签中设置的TABLE行,具体取决于服务器端的某些值。

有一种方法可以将该值从服务器端传递给.xsl端的jquery。

这是.xsl文件的一小部分

          $( document ).ready(function() {
          $("#tEntSetup tr.daTarow").hide();
          $("#tEntOrg tr.daTarow").hide();
          $("#tCoSetup tr.daTarow").hide();
          $("#tEmpCount tr.daTarow").hide();
          $("#tRuleCode tr.daTarow").hide();
          $('#tVersionDetails').on('click','tr.header',function(){
          $(this).nextUntil('tr.header').slidetoggle(200);
          });
          $('#tEntSetup').on('click',function(){
          $(this).nextUntil('tr.header').slidetoggle(200);
          });
          $('#tEntOrg').on('click',function(){
          $(this).nextUntil('tr.header').slidetoggle(200);
          });
          $('#tCoSetup').on('click',function(){
          $(this).nextUntil('tr.header').slidetoggle(200);
          });
          $('#tEmpCount').on('click',function(){
          $(this).nextUntil('tr.header').slidetoggle(200);
          });
          $('#tRuleCode').on('click',function(){
          $(this).nextUntil('tr.header').slidetoggle(200);
          });

          $('#tVersionDetails tr.header').css('background-color','#0073C2');
          });
         
          table {
          font-family: arial,sans-serif;
          border-collapse: collapse;
          width: 100%;
          }
          td,th {
          border: 1px solid #dddddd;
          text-align: left;
          padding: 8px;
          }
          tr.daTarow th {
          background-color: #fFeedd;
          }
          .header {
          background-color: #3EAA48;
          color:#FFFFFF;
          cursor: pointer;
          }
          .daTarow{
          background-color: whitesmoke;
          }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="gridtable" id="tVersionDetails">
          <tr class="header"><th colspan="2" class="Title">Version Details</th></tr>
          <tr class="daTarow">
            <th>Title</th>
            <th>Version</th>
          </tr>
          <xsl:for-each select="//information/Productinformation/VersionInfo/data">
            <tr class="daTarow">
              <td>
                <xsl:value-of select="title"/>
              </td>
              <td>
                <xsl:value-of select="version"/>
              </td>
            </tr>
          </xsl:for-each>
        </table>

非常感谢您查看我的问题并帮助我。

解决方法

public void GetProdInformation(bool useCustom,string connString)
        {
            ConnectionString = connString;
            UseCustom = useCustom;
            XmlDocument XD = new XmlDocument();`enter code here`
            XmlElement root = XD.CreateElement("Information");
            XmlElement si = XD.CreateElement("ProductInformation");
            root.AppendChild(si);

            DataSet dsProdInfo = null;
            try
            {
                using (DBHelper helper = new DBHelper(GetConnString()))
                {
                    helper.ClearParameters();
                    helper.CommandType = System.Data.CommandType.StoredProcedure;
                    helper.CommandText = "dbo.pro_product_info_get";
                    dsProdInfo = helper.ExecuteQuery();

                    if (dsProdInfo != null && dsProdInfo.Tables.Count > 0)
                        SetTableNames(ref dsProdInfo);

                    CheckForPayrollInstallation(dsProdInfo);
                }

                si.AppendChild(VersionInfo(XD,dsProdInfo));
                si.AppendChild(PayrollInstalled(XD));
                si.AppendChild(EnterpriseSetup(XD,dsProdInfo));
                si.AppendChild(EnterpriseOrganizations(XD,dsProdInfo));
                si.AppendChild(CompanySetup(XD,dsProdInfo));
                si.AppendChild(EmployeeCount(XD,dsProdInfo));

                si.AppendChild(DetailedCheckBox(XD));
                if (IsDetailedCheckBoxChecked)
                    si.AppendChild(RuleCodeTables(XD,dsProdInfo));

                root.AppendChild(si);
                XD.AppendChild(root);

                String sLogFileName = string.Format(XMLFileName,Path.GetTempPath());
                XD.Save(sLogFileName);

            }
            catch (Exception ex)
            {

            }
            finally
            {
                dsProdInfo.Dispose();
            }
        }
,

您应该编辑原始问题而不是添加答案,因此所有信息都位于同一位置。

您的服务器端代码未显示xsl转换。会是这样的:

            string serverside_p = "a value";
            XsltArgumentList argsList = new XsltArgumentList();
            argsList.AddParam("serverside_param","",serverside_p);

            XslCompiledTransform transform = new XslCompiledTransform(true);
            transform.Load("your_xsl.xsl");

            using (StreamWriter sw = new StreamWriter("result.xml"))
            {
                transform.Transform("your_xml.xml",argsList,sw);
            }

您的XSL必须具有与您传递的参数名称匹配的参数:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>

<xsl:param name="serverside_param" />

....

<!-- To output the value -->

<xsl:value-of select="$serverside_param" />

<!-- or put it inside an attribute value -->
<td class="{$serverside_param}_class">cell value </td>

但是,当您在服务器上构建XML时,为什么不能将参数值直接添加到XML中并像其他XML属性一样访问它呢?