Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. Generating multiple files using XSLT 1.0

Generating multiple files using XSLT 1.0

Scheduled Pinned Locked Moved XML / XSL
csharpai-codingxmlquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fadi Yoosuf
    wrote on last edited by
    #1

    Hi I am doing a code generation app. using .Net 3.5. I wish to know if it is possible to generate multiple code files from a single xslt file. I know that it can be accomplished in XSLT 2.0 using result-document tag. Is it possible using XSLT 1.0 ? Thanks Fadi

    S 1 Reply Last reply
    0
    • F Fadi Yoosuf

      Hi I am doing a code generation app. using .Net 3.5. I wish to know if it is possible to generate multiple code files from a single xslt file. I know that it can be accomplished in XSLT 2.0 using result-document tag. Is it possible using XSLT 1.0 ? Thanks Fadi

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      Exslt[^] provides the exsl:document[^] element - but that only seems to be implemented in libxslt. As you're using .NET, though, you could implement something like exsl:document using an Extension Object[^].

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      F 1 Reply Last reply
      0
      • S Stuart Dootson

        Exslt[^] provides the exsl:document[^] element - but that only seems to be implemented in libxslt. As you're using .NET, though, you could implement something like exsl:document using an Extension Object[^].

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        F Offline
        F Offline
        Fadi Yoosuf
        wrote on last edited by
        #3

        Thank you Stuart But can you clarify some more things? My xml file is

        <?xml version="1.0" encoding="utf-8" ?>
        <bookstore>
        <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
        <title>The Autobiography of Benjamin Franklin</title>
        <author>
        <first-name>Benjamin</first-name>
        <last-name>Franklin</last-name>
        </author>
        <price>8.99</price>
        </book>
        <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
        <title>The Confidence Man</title>
        <author>
        <first-name>Herman</first-name>
        <last-name>Melville</last-name>
        </author>
        <price>11.99</price>
        </book>
        <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
        <title>The Gorgias</title>
        <author>
        <name>Plato</name>
        </author>
        <price>9.99</price>
        </book>
        </bookstore>

        My xslt file is

        <?xml version="1.0" encoding="utf-8"?>
        <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:output="urn:output">

        <xsl:template match="bookstore">
        <bookstore>
        <xsl:for-each select="book">
        <book>
        <xsl:copy-of select="node()"/>
        <new-price>
        <xsl:value-of select="output:SaveOutput(@genre, node())"/>
        </new-price>
        </book>
        </xsl:for-each>
        </bookstore>
        </xsl:template>
        </xsl:stylesheet>

        C# code goes like

        private void ExtensionTest()
        {
        XslCompiledTransform xslt = new XslCompiledTransform();
        xslt.Load("extension.xslt");
        XsltArgumentList xslArg = new XsltArgumentList();

                OutputSaver obj = new OutputSaver();
                xslArg.AddExtensionObject("urn:output", obj);
                using (XmlWriter w = XmlWriter.Create("output.xml"))
                {
                    xslt.Transform("books.xml", xslArg, w);
                }
        

        }
        public class OutputSaver
        {
        public void SaveOutput(string fileName, string node)
        {
        StreamWriter s = new StreamWriter(fileName);
        s.Write(node);
        s.Flush();
        }
        }

        Now I have 2 issues * How to pass the xslt node content to SaveOutput

        F 1 Reply Last reply
        0
        • F Fadi Yoosuf

          Thank you Stuart But can you clarify some more things? My xml file is

          <?xml version="1.0" encoding="utf-8" ?>
          <bookstore>
          <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
          <title>The Autobiography of Benjamin Franklin</title>
          <author>
          <first-name>Benjamin</first-name>
          <last-name>Franklin</last-name>
          </author>
          <price>8.99</price>
          </book>
          <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
          <title>The Confidence Man</title>
          <author>
          <first-name>Herman</first-name>
          <last-name>Melville</last-name>
          </author>
          <price>11.99</price>
          </book>
          <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
          <title>The Gorgias</title>
          <author>
          <name>Plato</name>
          </author>
          <price>9.99</price>
          </book>
          </bookstore>

          My xslt file is

          <?xml version="1.0" encoding="utf-8"?>
          <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:output="urn:output">

          <xsl:template match="bookstore">
          <bookstore>
          <xsl:for-each select="book">
          <book>
          <xsl:copy-of select="node()"/>
          <new-price>
          <xsl:value-of select="output:SaveOutput(@genre, node())"/>
          </new-price>
          </book>
          </xsl:for-each>
          </bookstore>
          </xsl:template>
          </xsl:stylesheet>

          C# code goes like

          private void ExtensionTest()
          {
          XslCompiledTransform xslt = new XslCompiledTransform();
          xslt.Load("extension.xslt");
          XsltArgumentList xslArg = new XsltArgumentList();

                  OutputSaver obj = new OutputSaver();
                  xslArg.AddExtensionObject("urn:output", obj);
                  using (XmlWriter w = XmlWriter.Create("output.xml"))
                  {
                      xslt.Transform("books.xml", xslArg, w);
                  }
          

          }
          public class OutputSaver
          {
          public void SaveOutput(string fileName, string node)
          {
          StreamWriter s = new StreamWriter(fileName);
          s.Write(node);
          s.Flush();
          }
          }

          Now I have 2 issues * How to pass the xslt node content to SaveOutput

          F Offline
          F Offline
          Fadi Yoosuf
          wrote on last edited by
          #4

          I got one more query. Will it be possible by embedding script in xslt? I tried the following way Xslt file

          ?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"
          xmlns:user="urn:myscripts"

          <xsl:output method="xml" indent="yes"/>
          

          <msxsl:script language="C#" implements-prefix="user">
          <![CDATA[
          public void SaveOutput(string fileName)
          {

            System.IO.TextWriter t = new System.IO.StreamWriter(fileName);
            t.Write("got it");
            t.Flush();
          }
          \]\]>
          

          </msxsl:script>

          <xsl:template match="@* | node()">
          <xsl:value-of select="user:SaveOutput('d:\testing.txt')"/>

          </xsl:template>
          

          </xsl:stylesheet>

          My C# code is

              private void ScriptTest()
              {
                  XsltSettings settings = new XsltSettings();
                  settings.EnableScript = true;
          
                  XslCompiledTransform trs = new XslCompiledTransform();
                  trs.Load("Custom.xslt", settings, new XmlUrlResolver());
                  StreamWriter writer = new StreamWriter(@"e:\\samples.txt");
                  trs.Transform(@"E:\\\\9.xml", null, writer);
          
                  writer.Flush();
                  writer.Close();
              }
          

          But when I run the code, I get en error like IOException unhandled - The device is not ready Thank you Fadi

          S 1 Reply Last reply
          0
          • F Fadi Yoosuf

            I got one more query. Will it be possible by embedding script in xslt? I tried the following way Xslt file

            ?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"
            xmlns:user="urn:myscripts"

            <xsl:output method="xml" indent="yes"/>
            

            <msxsl:script language="C#" implements-prefix="user">
            <![CDATA[
            public void SaveOutput(string fileName)
            {

              System.IO.TextWriter t = new System.IO.StreamWriter(fileName);
              t.Write("got it");
              t.Flush();
            }
            \]\]>
            

            </msxsl:script>

            <xsl:template match="@* | node()">
            <xsl:value-of select="user:SaveOutput('d:\testing.txt')"/>

            </xsl:template>
            

            </xsl:stylesheet>

            My C# code is

                private void ScriptTest()
                {
                    XsltSettings settings = new XsltSettings();
                    settings.EnableScript = true;
            
                    XslCompiledTransform trs = new XslCompiledTransform();
                    trs.Load("Custom.xslt", settings, new XmlUrlResolver());
                    StreamWriter writer = new StreamWriter(@"e:\\samples.txt");
                    trs.Transform(@"E:\\\\9.xml", null, writer);
            
                    writer.Flush();
                    writer.Close();
                }
            

            But when I run the code, I get en error like IOException unhandled - The device is not ready Thank you Fadi

            S Offline
            S Offline
            Stuart Dootson
            wrote on last edited by
            #5

            I remembered that there's an Exslt implementation for .NET[^] - you can use the exsl:document() support it contains[^]. Easier than trying to write your own...

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups