I have two pieces of XML that I need to transform into one
piece of XML using XSLT and C#. Each piece of XML has a
completely different structure, so the merge would be more
of a concatanation.
I have found examples of merging XML files, but none in
C#. I would like to do it in one pass, but it maybe a
case of doing two merges...I just don't know.
Is there a way of doing it in one pass? A small code
snippet would be appreciated.
Thanks in advance,
James.
> I have two pieces of XML that I need to transform into one
> piece of XML using XSLT and C#. Each piece of XML has a
> completely different structure, so the merge would be more
> of a concatanation.
>
> I have found examples of merging XML files, but none in
> C#. I would like to do it in one pass, but it maybe a
> case of doing two merges...I just don't know.
So you wanna C# or XSLT?
btw, check out recently published article at xml.com on the topic:
http://www.xml.com/pub/a/2003/11/26/q-and-a.html
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog
Thanks
You can run XSLT in C# actually. Input documents might be string,
stream, XmlReader, files, whatever. The same goes for result.
>.
>
> I know you can do it, I already have C# code that does
> it...I refer back to my original question: I have 2
> different XML files, that I want to transform using a
> single XSLT in ONE pass. Is this possible? And how?
Well, that's exactly what xml.com article I mentioned shows. Just open
both documents via document() function and apply your merge logic.
>.
>
<root document="Two documents to merge">
<document1>
'Contains document 1
</document1>
<document2>
'Contains document 2
</document2>
</root>
and apply your XSLT.
Alternatively pass in the string data as a parameter of the transform? .....
yaaargh - I have no idea if that can be used at all - maybe Oleg, Dimitre,
Marrow can make a suggestion about that.
Chris.
<anon...@discussions.microsoft.com> wrote in message
news:00a801c3b8ed$691994f0$a101...@phx.gbl...
I have two XML streams, and one XSLT in C#. I need to
merge/concatanate and transform the two XML streams into a
single XML stream. None of these streams can be persisted
to disk. Therefore the xsl:document function would not
appear to be relevant.
Is it possible to do this in C# - in one pass - using a
XslTransform object?
Thanks,
James
> I have two XML streams, and one XSLT in C#. I need to
> merge/concatanate and transform the two XML streams into a
> single XML stream. None of these streams can be persisted
> to disk. Therefore the xsl:document function would not
> appear to be relevant.
That makes difference. I see couple of approaches here. First, standard
facility for custom XML input sources is XmlResolver. You can implement
custom XmlResolver class, which returns Stream object and call it in
document() function. XslTransform supports only Stream and
XPathNaigator, no XmlReader unfortunately.
Alternatively you can represent both streams as single XML somehow, say
via custom XmlReaderReader, that's going to be more effective if you
have XmlReader, not Stream objects.
No. You can't pass two streams to an XslTransform. I believe that it is
also not possible to pass a node-set parameter to an XslTransform, which
would be the other way to make the XSLT transform process both streams in a
single pass.
I believe that the fastest way to do what you're trying to do would be to
pull the streams into two XmlDocument objects, and use ImportNode to combine
them into one:
XmlNode newNode = doc1.ImportNode(doc2.DocumentElement);
doc1.DocumentElement.AppendChild(newNode);
Then apply the transform to the resulting document.
Bob Rossney
r...@well.com
While I'm right that you can't pass a node-set parameter to an XslTransform,
you *can* pass a result-tree-fragment parameter, and then use the extension
function msxsl:node-set() to convert it to a node set. I'd bet that this is
no faster than pulling both streams into a single source document (and it's
certainly going to be harder to code), but it's probably worth checking out
and benchmarking if performance is critical.
Bob Rossney
r...@well.com
> No. You can't pass two streams to an XslTransform. I believe that it is
> also not possible to pass a node-set parameter to an XslTransform, which
> would be the other way to make the XSLT transform process both streams in a
> single pass.
Why? Take a look at XsltArgumentList class. It allows to pass strings,
booleans, numbers, rtfs and nodesets parameters. So James can load
second stream into XPathDocument and then pass it as parameter (in
XPathNodeIterator form though) to the transformation.
I will be back if I get stuck!
James