使用XSLT

时间:2016-08-31 09:54:02

标签: xml xslt xslt-1.0

我有这样的XML和XSL:

XML

<definitions>
  <process id="Process_1">
    <Task id="Task_1yh7nak" name="Add">
      <Steps>
        <Step id="Step_1" Order="1">
          <Form>
            <form-template>
              <fields>
                <field type="text" label="start" id="text-14708410685"></field>
                <field type="text" label="end"  id="text-5651568987"></field>
                <field type="button" subtype="button" label="Button" id="button-1470841070721"></field>
              </fields>
            </form-template>
          </Form>
        </Step>        
      </Steps>
    </Task>
  </process>
</definitions>

XSL

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"                              
                              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                              xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:output method = "text" omit-xml-declaration = "no" indent = "no" encoding="Windows-1252"/> 

 <!-- Start the code generation here. -->
  <xsl:template match ="*">

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using System.Windows.Forms;

namespace Core
{
  public static class DynamicCode
  {
  <xsl:for-each select="/definitions/process/Task">
    public static string <xsl:value-of select="@id"/>_GetStartForm()
    {
     string formContent = "<xsl:for-each select="Steps/Step[@Order='1']/Form/form-template/fields/field">
      <!--Switch Case for type of controls -->
      <xsl:choose>          
        <xsl:when test="@type='text'">
          <h2> textbox </h2>
          <input>
            <xsl:attribute name="value">
              <xsl:value-of select="@label"/>
            </xsl:attribute>
            <xsl:attribute name="id">
              <xsl:value-of select="@id"/>
            </xsl:attribute>
          </input>
          <br/>
        </xsl:when>

        <xsl:when test="@type='button'">
          <h2> button </h2>
          <input type="button">
            <xsl:attribute name="value">
              <xsl:value-of select="@label"/>
            </xsl:attribute>
            <xsl:attribute name="id">
              <xsl:value-of select="@id"/>
            </xsl:attribute>
          </input>
          <br/>
        </xsl:when>
      </xsl:choose>

  </xsl:for-each>";
     return formContent;
    }   
   </xsl:for-each>
  }
}

</xsl:template>
</xsl:stylesheet>

我用这个XSL(XSLT 1.0)转换XML。

输出

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Windows.Forms;

namespace Core
{
  public static class DynamicCode
  {

    public static string Task_1yh7nak_GetStartForm()
    {
     string formContent = " textbox  textbox  button ";
     return formContent;
    }   

  }
}

在线输出:http://xsltransform.net/pPJ8LUY/1

我尝试将fields标记中的完整html内容作为字符串格式。我需要在网页中填写Literal这个内容。

<h2> textbox </h2>
<input id="text-14708410685" value="start"></input>
<h2> textbox </h2>
<input id="text-5651568987" value="end"></input>
<h2> button </h2>
<input type="button" id="button-1470841070721" value="submit"></input>

但是在输出中,我只有h2标记的值。

string formContent = " textbox  textbox  button ";

如果有人可以解释这个问题的解决方案,那将非常有用。

1 个答案:

答案 0 :(得分:1)

It's not that pleasant to look at, but in XSLT 1.0 you will have to manually escape all the HTML tags you wish to output.

Try this XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"                              
                              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                              xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:output method = "text" omit-xml-declaration = "no" indent = "no" encoding="Windows-1252"/> 

 <!-- Start the code generation here. -->
  <xsl:template match ="*">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Windows.Forms;

namespace Core
{
  public static class DynamicCode
  {
  <xsl:for-each select="/definitions/process/Task">
    public static string <xsl:value-of select="@id"/>_GetStartForm()
    {
     string formContent = "<xsl:for-each select="Steps/Step[@Order='1']/Form/form-template/fields/field">
      <!--Switch Case for type of controls -->
      <xsl:choose>          
        <xsl:when test="@type='text'">
          <xsl:text>&lt;h2&gt;textbox&lt;/h2&gt;</xsl:text>
          <xsl:text>&lt;input</xsl:text>
        </xsl:when>
        <xsl:when test="@type='button'">
          <xsl:text>&lt;h2&gt;button&lt;/h2&gt;</xsl:text>
          <xsl:text>&lt;input type=\"button\"</xsl:text>
        </xsl:when>
      </xsl:choose>
      <xsl:text> id=\"</xsl:text>
      <xsl:value-of select="@id" />
      <xsl:text>\"</xsl:text>
      <xsl:text> value=\"</xsl:text>
      <xsl:value-of select="@label" />
      <xsl:text>\"</xsl:text>
      <xsl:text>&gt;&lt;/input&gt;</xsl:text>

  </xsl:for-each>";
     return formContent;
    }   
   </xsl:for-each>
  }
}

</xsl:template>
</xsl:stylesheet>

This outputs the relevant line as follows:

 string formContent = "<h2>textbox</h2><input id=\"text-14708410685\" value=\"start\"></input><h2>textbox</h2><input id=\"text-5651568987\" value=\"end\"></input><h2>button</h2><input type=\"button\" id=\"button-1470841070721\" value=\"Button\"></input>";

See it in action at http://xsltransform.net/3NSSEuH