Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to extend this XSLT to include XML fields dynamically?
    text
    copied!<p>We need to parse an XML file with XSLT into a CVS file. The code below works <em>but only if</em> the fields in the XML are always constant.</p> <p>The fields in the our XML file will always vary. <strong>How can I change my XSLT file so that it dynamically includes in the CSV file <em>all the fields</em> from the XML file?</strong></p> <p>XML:</p> <pre><code>&lt;?xml version="1.0" encoding="iso-8859-1"?&gt; &lt;data&gt; &lt;row&gt; &lt;customerID&gt;06104539-463E-4B1A-231-34342343434&lt;/customerID&gt; &lt;contactID&gt;23434-99F2-4325-B228-6F343483469389FB&lt;/contactID&gt; &lt;firstName&gt;Jim&lt;/firstName&gt; &lt;lastName&gt;Smith&lt;/lastName&gt; &lt;/row&gt; &lt;row&gt; &lt;customerID&gt;223434-463E-4B1A-231-A1E7EA248796&lt;/customerID&gt; &lt;contactID&gt;6675767-99F2-4325-B234328-6F83469389FB&lt;/contactID&gt; &lt;specialID&gt;112332&lt;/specialID&gt; &lt;firstName&gt;John&lt;/firstName&gt; &lt;middleName&gt;S.&lt;/middleName&gt; &lt;lastName&gt;Jones&lt;/lastName&gt; &lt;/row&gt; &lt;/data&gt; </code></pre> <p>XSLT:</p> <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt; &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:template match="/"&gt; customerID,contactID&lt;br/&gt; &lt;xsl:for-each select="data/row"&gt; &lt;xsl:value-of select="customerID"/&gt;,&lt;xsl:value-of select="contactID"/&gt;&lt;br/&gt; &lt;/xsl:for-each&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>Here is the ASP.NET file that parses the files above:</p> <pre><code>&lt;%@ Page Language="c#" %&gt; &lt;%@ import Namespace="System.Xml" %&gt; &lt;%@ import Namespace="System.Xml.Xsl" %&gt; &lt;%@ import Namespace="System.Xml.XPath" %&gt; &lt;%@ import Namespace="System.IO" %&gt; &lt;%@ import Namespace="System.Text" %&gt; &lt;script runat="server"&gt; public void Page_Load(Object sender, EventArgs E) { string xmlPath = Server.MapPath("test2.xml"); string xslPath = Server.MapPath("test2.xsl"); StreamReader reader = null;; XmlTextReader xmlReader = null; FileStream fs = new FileStream(xmlPath, FileMode.Open, FileAccess.Read); reader = new StreamReader(fs,Encoding.UTF7); xmlReader = new XmlTextReader(reader); XPathDocument doc = new XPathDocument(xmlReader); XslTransform xslDoc = new XslTransform(); xslDoc.Load(xslPath); xslDoc.Transform(doc,null, Response.Output); reader.Close(); xmlReader.Close(); } &lt;/script&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload