I have a XML and XSL like this:
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>
I transform XML with this XSL (XSLT 1.0).
Output
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;
}
}
}
Online output:http://xsltransform.net/pPJ8LUY/1
I try to get full html content in fields tag as string format. I need to this content for fill Literal in web page.
<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>
But in output, I have only value of h2 tag.
string formContent = " textbox textbox button ";
It would be very helpful if someone could explain solution for this problem.
inputelements to show up then you need to use thexsl:output method="html"orxml, the text output method you use is supposed to output the plain text in text nodes, but not any markup of element nodes.". In string, it makes error. I can't use \ before and after them inxsl:value-of.serializefunction to serialize and then finally usereplaceto make sure any quotes"are escaped as\"for the C# string literal.value-ofthat creates the nested, unescaped quotes, you are outputting literal text with double quotes and the serialization of result elements with attributes, and these are serialized with double quotes. As said, there is no easy and nice way out, unless you can use XSLT 3.0. Or you go the way proposed by Tim in his answer where you use output methodtextand then write code to construct the HTML markup with all necessary escaping of quotes done by hand to allow you to include the HTML markup inside of a C# string literal.