我有一个这样的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 ";
如果有人能解释一下这个问题的解决方法,那就太有帮助了。
这看起来并不那么令人愉快,但是在XSLT 1.0中,您必须手动转义希望输出的所有HTML标记。
试试这个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><h2>textbox</h2></xsl:text>
<xsl:text><input</xsl:text>
</xsl:when>
<xsl:when test="@type='button'">
<xsl:text><h2>button</h2></xsl:text>
<xsl:text><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>></input></xsl:text>
</xsl:for-each>";
return formContent;
}
</xsl:for-each>
}
}
</xsl:template>
</xsl:stylesheet>
输出相关行如下:
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>";
参见http://xsltransform.net/3NSSEuH