tags:

views:

132

answers:

2

Hello SO;

I've been fighting with this problem all day and am just about at my wit's end.

I have an XML file in which certain portions of data are stored as escaped text but are themselves well-formed XML. I want to convert the whole hierarchy in this text node to a node-set and extract the data therein. No combination of variables and functions I can think of works.

The way I'd expect it to work would be:

<xsl:variable name="a" select="InnerXML">
<xsl:for-each select="exsl:node-set($a)/*">
    'do something
</xsl:for-each>

The input element InnerXML contains text of the form

<root><elementa>text</elementa><elementb><elementc/><elementd>text</elementd></elementb></root>

but that doesn't really matter. I just want to navigate the xml like a normal node-set.

Where am I going wrong?

+1  A: 

what I've done is had a msxsl script in the xslt ( this is in a windows .NET environment):

  <msxsl:script implements-prefix="cs" language="C#" >
    <![CDATA[
    public XPathNodeIterator parse(String strXML)
    {
      System.IO.StringReader rdr = new System.IO.StringReader(strXML);
      XPathDocument doc = new XPathDocument(rdr);
      XPathNavigator nav = doc.CreateNavigator();

      XPathExpression expr;
      expr = nav.Compile("/");

      XPathNodeIterator iterator = nav.Select(expr);

      return iterator;
    }
    ]]>
  </msxsl:script>

then you can call it like this:

<xsl:variable name="itemHtml" select="cs:parse(EscapedNode)" />

and that variable now contains xml you can iterate through

derek
Thanks derek. I'm leaning toward that resolution out of desperation, but surely this is something that XSLT should be able to do? The problem is an almost exact match to various examples given on the web. I'd like to figure out why it won't do what it's supposed to do for me.
Tom W
+1  A: 

In case you can use Saxon 9.x, it provides the saxon:parse() extension function exactly for solving this task.

Dimitre Novatchev
Thanks for the suggestion. Unfortunately I'm stuck entirely with .Net 2.0.
Tom W
I'm going to mark this as the answer, because technically it's what prompted me to realise that the actual answer to my question is "You can't"; that is, there is no native parsing function in MSXML. Sorry derek, your answer was useful and it's the solution I ended up implementing. Dimitre answered the question, and derek solved my problem. They're often distinct.
Tom W