Solved

Pass Raw XML to XSLT Transformer

Posted on 2009-05-13
11
922 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
Comment Utility
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
Comment Utility
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 74

Author Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
> 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 74

Author Comment

by:käµfm³d 👽
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 74

Author Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
> 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
Comment Utility
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
Comment Utility
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 74

Author Closing Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
You're welcome! :)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to dynamically set the form action using jQuery.

762 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

9 Experts available now in Live!

Get 1:1 Help Now