Solved

Pass Raw XML to XSLT Transformer

Posted on 2009-05-13
11
931 Views
Last Modified: 2013-11-18
Good morning!

I am trying to pass raw XML to a XslCompiledTransform object in C# (to Experts: the XSLT processor *may* not be relevant). I want to accept this raw XML for parsing within my XSLT file. I have been trying to use the document() function to get the nodes of the passed-in XML, but to no avail. I am attaching pseudo-ish code to hopefully better explain. Is what is shown in the code box possible? Or am I going to need to write out my "temporary" XML to disk in order to use document().

As a note, I am not bent on using document(); I merely want to be able to "for-each" through the "file" nodes of the passed in XML.
// Raw XML sample

<file>C:\some\path\and\file.txt</file><file>C:\another\path\and\file.dat</file>
 
 

// Looking to do something like

<xsl:param name="incomingXML" />

<xsl:variable name="xFormCopy" select="document($incomingXML/>
 
 

<xsl:for-each select="$xFormCopy/file">

// some work here...

</xsl:for-each>

Open in new window

0
Comment
Question by:käµfm³d   👽
  • 7
  • 4
11 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24374262
Well, basically, you do not need to use document() at all. You mention that you use C#. That means that all you need to do is pass the XML file onto the parameters of the XslCompiledTransform object:

xslt.Transform("Xml/input.xml", xsltArgs, xmlOutputWriter);
0
 
LVL 39

Expert Comment

by:abel
ID: 24374281
Suppose if, for another reason, you want to send the filenames of the XML as parameters to the transformation, you can do that as follows, after which you can use document() the way you are saying you are using it in your example XSLT.

XsltArgumentList xsltArgs = new XsltArgumentList();

xsltArgs.AddParam("incomingXMLName", "", "pathToXml.XML");

xslt.Transform("someDefault.xml", xsltArgs, xmlOutputWriter);

Open in new window

0
 
LVL 75

Author Comment

by:käµfm³d 👽
ID: 24374379
abel,

I do not need to process the files listed in "<file></file>" tags. I am going to list them in my output. So for something like

    <file>C:\some\path\and\file.txt</file><file>C:\another\path\and\file.dat</file>

Disregarding the other formatting that will take place (various HTML formatting), my output will be

    C:\some\path\and\file.txt
    C:\another\path\and\file.dat

I have a main XSLT document and within that document, I want to be able to accept a parameter that is the raw XML listed above and (unless there is a better way) do a "for-each" like operation on each "file" to display it in the output.

I am basically listing the files in a d irectory and I do not know how many files will be in the directory. Let me know if I can clarify further :)
0
 
LVL 39

Expert Comment

by:abel
ID: 24374446
> unless there is a better way

my first comment was about that better way. The input document needs to be specified anyway, why would you want to specify it through a parameter? Unless you have a different input document that you are already processing and you want this one next to it, which was my second comment: using a parameter.

The XSLT is pretty much OK already, use the following, where I slightly amended your code, with my code of my second comment:

<xsl:param name="incomingXMLName" />
 

<xsl:template match="/">

   <xsl:apply-templates select="document($incomingXMLName)/file" />

</xsl:template>
 

<xsl:template match="file">

    <xsl:value-of select="text()" />

    <xsl:text>&#xA;</xsl;text>

</xsl:template>

Open in new window

0
 
LVL 75

Author Comment

by:käµfm³d 👽
ID: 24374717
Ok, forgive me if I'm just not understanding, but here is the code I am dealing with.
//////////////////////////////////////////////////////

// C# side

//////////////////////////////////////////////////////
 

private string getIrsFileName(string sSubPath) {

    List<string> xmlFileList = new List<string>();

    StringBuilder files = new StringBuilder("<?xml version=\"1.0\" ?>");

    sSubPath = sSubPath + @"\irs\xml\";

    string[] dirFiles = Directory.GetFiles(sSubPath, "*.xml", SearchOption.TopDirectoryOnly);

    

    foreach (string file in dirFiles)

    {

        files.AppendFormat("<ts:file>{0}</ts:file>\n", file);

    }
 

    return files.ToString();  // Returns an XML string like "<ts:file>C:\some\file.txt</ts:file><ts:file>C:\some\morefiles.txt</ts:text>"

}
 

//////////////////////////////////////////////////////

// XSLT code

//////////////////////////////////////////////////////
 

xsltParams.AddParam("irsdataxmlPath", string.Empty, getIrsFileName("C:\some\parent\dir"));
 

//////////////////////////////////////////////////////

//////////////////////////////////////////////////////
 

//////////////////////////////////////////////////////

// In XSLT document

//////////////////////////////////////////////////////

<xsl:param name="irsdataxmlPath" />  <!-- Contains textual data of the form "<ts:file>C:\some\file.txt</ts:file><ts:file>C:\some\morefiles.txt</ts:text>"
 

<!-- I want to treat the above as a node set rather than just a variable containing text -->

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 75

Author Comment

by:käµfm³d 👽
ID: 24374736
As far as I've tested, document() won't work on the xsl:param "irsdataxmlPath" because it is not a valid file path; rather it is XML data.
0
 
LVL 39

Expert Comment

by:abel
ID: 24375420
> because it is not a valid file path; rather it is XML data.

exactly. Which is why I tried to explain somehow that you should not use this semantics. But I thought that the XML document was on the disk, which is why I explained the way I did.

Using the method above, you won't succeed without resorting to extension methods. However, what you can do, is create the document, that you now create by hand, by the XML DOM classes (which is always much preferred over doing it by hand, to prevent coding mistakes). The, the resulting document can be given as an argument, without having to resort to placing it on disk first.

I can try to workout an example if you want that...
0
 
LVL 39

Accepted Solution

by:
abel earned 500 total points
ID: 24376008
Apparently, that approach works, and better than I expected. Here you go:

// Create the document, change this such that you load your file list here

XmlDocument doc = new XmlDocument();

doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", "no"));
 

// create root node <files>

XmlElement files = doc.CreateElement("files");
 

// <file>...</file>

XmlElement file = doc.CreateElement("file");

file.AppendChild(doc.CreateTextNode("c:/temp/text1.txt"));

files.AppendChild(file);
 

// other way for <file>...</file>

file = (XmlElement) file.Clone();

file.InnerText = "c:/temp/text2.txt";

files.AppendChild(file);
 

// create xml document by adding the root node <files>

doc.AppendChild(files);
 

// create arguments and add params

XsltArgumentList xsltArgs = new XsltArgumentList();

xsltArgs.AddParam("externalDocument", "", doc);

xslt.Transform("someDefault.xml", xsltArgs, xmlOutputWriter);

Open in new window

0
 
LVL 39

Expert Comment

by:abel
ID: 24376038
I kept the XSLT document extraordinarily simple, it is just a proof of concept. Below is the XSLT and the output XML from the above code:



<!-- xslt -->

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 

    <xsl:param name="externalDocument" />
 

    <xsl:template match="/">

        <xsl:copy>

            <xsl:copy-of select="$externalDocument/*" />

        </xsl:copy>

    </xsl:template>

</xsl:stylesheeet>
 

<!-- output xml -->

<?xml version="1.0" encoding="utf-8"?>

<files>

  <file>c:/temp/text1.txt</file>

  <file>c:/temp/text2.txt</file>

</files>

Open in new window

0
 
LVL 75

Author Closing Comment

by:käµfm³d 👽
ID: 31580942
Thanks abel. I was not aware that you could pass an XMLDocument object to the XSLT processor in C#--although I guess in hindsight it makes sense! Sorry for the delay in closing the question, and thanks again for your help.
0
 
LVL 39

Expert Comment

by:abel
ID: 24398635
You're welcome! :)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now