如何将xml文件与xslt文件进行合并

89 篇文章 0 订阅
7 篇文章 0 订阅

xml文件(XMLFile1.xml)如:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <folder>
  <node>folder</node>
  </folder>
  <info>
    <node title="姓名" id="Name" type="input" style="needcheck:true;isnull:false;minlength:2;des:姓名"></node>
    <node title="行业" id="Trade" type="select" style="needcheck:true;isnull:false;des:行业">
      <option selected="selected" value="">请选择</option>
      <option value="IT">IT</option>
      <option value="Finance">金融</option>
    </node>
    <node title="区号" id="AreaCode" type="input" style="needcheck:true;isnull:true;isnum:true;minlength:3;maxlength:4;des:区号"></node>
    <node title="电话" id="Phone" type="input" style="needcheck:true;isnull:true;isphone:true;des:;des:电话"></node>
    <node title="分机" id="Extension" type="input" style="needcheck:true;isnull:true;isnum:true;minlength:3;maxlength:4;des:分机"></node>
    <node title="性别" id="Gender" type="radio"  style="needcheck:true;isnull:true;des:性别">
      <option selected="selected" value="man">男</option>
      <option value="woman">女</option>
    </node>
    <node title="爱好" id="Interest" type="checkbox" style="needcheck:true;isnull:false;des:爱好">
      <option value="car">汽车</option>
      <option value="sport">运动</option>
      <option value="music">音乐</option>
    </node>
    <node title="省" id="province" type="province" style="needcheck:true;isnull:false;des:省"></node>
    <node title="市" id="city" type="city" style="needcheck:false;isnull:true;des:市"></node>
  </info>
  <script>
    <node>onsetup();</node>
  </script>

</root>

xslt文件(XSLTFieldStyle.xslt)如:

 

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="html" indent="yes"/>
 <!-- define path template -->
 <xsl:template name="folder">
  <xsl:element name="input">
   <xsl:attribute name="name" >folder</xsl:attribute>
   <xsl:attribute name="id" >folder</xsl:attribute>
   <xsl:attribute name="type" >hidden</xsl:attribute>
   <xsl:attribute name="value" >
    <xsl:value-of select="." />
   </xsl:attribute>
  </xsl:element>
 </xsl:template>
 <!--end path-->
  <!-- define input template -->
  <xsl:template name="input">
    <tr>
      <td>
        <label>
          <xsl:value-of select="@title" />
        </label>
      </td>
      <td>
        <xsl:element name="input">
          <xsl:attribute name="id">
            <xsl:value-of select="@id" />
          </xsl:attribute>
          <xsl:attribute name="name">
            <xsl:value-of select="@id" />
          </xsl:attribute>
          <xsl:attribute name="class">InputValue</xsl:attribute>
          <xsl:attribute name="type">text</xsl:attribute>
          <xsl:attribute name="style">
            <xsl:value-of select="@style" />
          </xsl:attribute>
        </xsl:element>
      </td>
    </tr>
  </xsl:template>
  <!--end input-->

  <!-- define select template -->
  <xsl:template name="select">
    <tr>
      <td>
        <label>
          <xsl:value-of select="@title" />
        </label>
      </td>
      <td>
        <xsl:element name="select">
          <xsl:attribute name="id">
            <xsl:value-of select="@id" />
          </xsl:attribute>
          <xsl:attribute name="name">
            <xsl:value-of select="@id" />
          </xsl:attribute>
          <xsl:attribute name="style">
            <xsl:value-of select="@style" />
          </xsl:attribute>
          <xsl:attribute name="class">Select</xsl:attribute>
          <xsl:for-each select="option">
            <xsl:element name="option">
              <xsl:attribute name="value">
                <xsl:value-of select="@value" />
              </xsl:attribute>
              <xsl:if test="@selected!=''">
                <xsl:attribute name="selected">
                  <xsl:value-of select="@selected" />
                </xsl:attribute>
              </xsl:if>
              <xsl:value-of select="." />
            </xsl:element>
          </xsl:for-each>
        </xsl:element>
      </td>
    </tr>
  </xsl:template>
  <!--end select-->

  <!-- define radio & checkbox template-->
  <xsl:template name="choose">
    <tr>
      <td>
        <label>
          <xsl:value-of select="@title" />
        </label>
      </td>
      <td>
        <xsl:element name="div">
          <xsl:variable name="id" select="@id"></xsl:variable>
          <xsl:variable name="type" select="@type"></xsl:variable>
          <xsl:attribute name="id">
            <xsl:value-of select="@id" />
          </xsl:attribute>
          <xsl:attribute name="style">
            <xsl:value-of select="@style" />
          </xsl:attribute>
          <xsl:for-each select="option">
            <xsl:element name="input">
              <xsl:attribute name="name">
                <xsl:value-of select="$id" />
              </xsl:attribute>
              <xsl:attribute name="value">
                <xsl:value-of select="@value" />
              </xsl:attribute>
              <xsl:attribute name="type">
                <xsl:value-of select="$type" />
              </xsl:attribute>
              <xsl:if test="@selected!=''">
                <xsl:attribute name="checked">
                  <xsl:value-of select="@checked" />
                </xsl:attribute>
              </xsl:if>
            </xsl:element>
            <xsl:value-of select="." />
          </xsl:for-each>
        </xsl:element>
      </td>
    </tr>
  </xsl:template>
  <!-- end radio & checkbox-->

  <!-- define province & city template-->
  <xsl:template name="local">
    <tr>
      <td>
        <label>
          <xsl:value-of select="@title" />
        </label>
      </td>
      <td>
        <xsl:element name="select">
          <xsl:attribute name="id">
            <xsl:value-of select="@id" />
          </xsl:attribute>
          <xsl:attribute name="style">
            <xsl:value-of select="@style" />
          </xsl:attribute>
          <xsl:attribute name="name">
            <xsl:value-of select="@id" />
          </xsl:attribute>
          <xsl:attribute name="onchange">
            <xsl:if test="@type='province'">on_pro_select_change()</xsl:if>
            <xsl:if test="@type='city'"></xsl:if>
          </xsl:attribute>
        </xsl:element>
      </td>
    </tr>
  </xsl:template>
  <!-- end province & city-->

  <!-- merge-->
  <xsl:output method="html" encoding="gb2312" media-type="text/html"/>
  <xsl:template match="/">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <title>Untitled Page</title>
        <script type="text/javascript" language="javascript" src="../../Scripts/jquery-1.4.1.js">var a;</script>
        <script type="text/javascript" language="javascript" src="../../Scripts/jquery.form.js">var a;</script>
        <script type="text/javascript" language="javascript" src="../../Scripts/City.js">var a;</script>
        <script type="text/javascript" language="javascript" src="../../Scripts/CheckFormInput.js">var a;</script>
        <script type="text/javascript" language="javascript">
          $(function () {
          $('#myform').submit(function () {
          $('#myform').ajaxSubmit({
          beforeSubmit: ValidateInput,
          success: SuccessEvent
          });
          return false;
          });
          function SuccessEvent(responseText, statusText) {
          alert(responseText);
          }
          function ValidateInput() {
          check_form();
          return false;
          }
          })
        </script>
      </head>
      <body>
        <form action="../../SaveUserInformation.aspx" method="post" name="myform">
   <!-- path folder-->
   <xsl:for-each select="root/folder/node">
    <xsl:call-template name="folder"></xsl:call-template>
   </xsl:for-each>
   <!-- path end-->
        <table>
          <xsl:for-each select="root/info/node">
            <xsl:choose>
              <xsl:when test="@type='input'">
                <xsl:call-template name="input"></xsl:call-template>
              </xsl:when>
              <xsl:when test="@type='select'">
                <xsl:call-template name="select"></xsl:call-template>
              </xsl:when>
              <xsl:when test="@type='radio'or @type='checkbox'">
                <xsl:call-template name="choose"></xsl:call-template>
              </xsl:when>
              <xsl:when test="@type='province'or @type='city'">
                <xsl:call-template name="local"></xsl:call-template>
              </xsl:when>
              <xsl:otherwise>
                <xsl:call-template name="input"></xsl:call-template>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:for-each>
          <tr>
            <td colspan="4">
              <input type="submit" id="submit" value="submit" />
            </td>
          </tr>
        </table>
  <script type="text/javascript" language="javascript">
          <xsl:for-each select="root/script/node">
            <xsl:value-of select="."/>
          </xsl:for-each>
        </script>

        </form>
      </body>
    </html>
  </xsl:template>
  <!-- end merge-->
</xsl:stylesheet>

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
using System.IO;
using System.Collections;
using System.Web;

namespace RegistrationAndQuestionnaireGenerator.Common
{
    public class Generator
    {
        /// <summary>
        /// meger xml and xsl to the html
        /// </summary>
        /// <param name="xmlPath">xml file path</param>
        /// <param name="xslPath">xsl</param>
        /// <returns></returns>
        public static String MegerToHtml(string xmlPath, string xslPath)
        {
            XmlReader _xmlxml = XmlReader.Create(xmlPath);
            XmlReader _xmlxsl = XmlReader.Create(xslPath);
            XslCompiledTransform xslct = new XslCompiledTransform();
            xslct.Load(_xmlxsl);
            MemoryStream ms = new MemoryStream();
            XmlTextWriter xmltxtWr = new XmlTextWriter(ms, null);
            xslct.Transform(_xmlxml, xmltxtWr);
            StreamReader st = new StreamReader(ms);
            ms.Seek(0, SeekOrigin.Begin);
            return st.ReadToEnd();
        }

    }

}

*.asp.cs后台代码:

        protected void Button1_Click(object sender, EventArgs e)
        {
            //string str = Common.Generator.MegerToHtml(Server.MapPath("XMLFile1.xml"), Server.MapPath("XSLTFile2.xslt"));
            //string path = Server.MapPath("XMLFile1.xml").Replace("XMLFile1.xml", "demo.htm");
            //File.WriteAllText(path, str,Encoding.GetEncoding( "gb2312"));
            //Response.Redirect("demo.htm");
            string str = Common.Generator.MegerToHtml(Server.MapPath("testContent.xml"), Server.MapPath("XSLTFieldStyle.xslt"));
            string path = Server.MapPath("testContent.xml").Replace("testContent.xml", "demo.htm");
            File.WriteAllText(path, str, Encoding.GetEncoding("gb2312"));
            Response.Redirect("demo.htm");
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值