Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSL: Convert Escaped XML into DOM

Status
Not open for further replies.

tleish

Programmer
Jan 17, 2001
619
0
0
US
I'm using xsl to parse an xml document. It's a bit rediculous, but part of the xml is escaped and there's currently nothing I can do about it.
Code:
<parent>
	<child>
		&lt;id&gt;1&lt;/id&gt;
		&lt;name&gt;Test&lt;/name&gt;
	</child>
</parent>
I want to be able to read it as:
Code:
<parent>
	<child>
		<id>1</id>
		<name>Test</name>
	</child>
</parent>
And be able to parse the nodes id and name. Any ideas on how to do this?

I tried the following, but it didn't work:
Code:
<xsl:variable name="test">
   <xsl:value-of select="child" disable-output-escaping="yes" />
</xsl:variable>
When I try to treat the variable $test as a node, I get the error:
"Reference to variable or parameter 'test' must evaluate to a node list."

Any ideas?

- tleish
 
That is really _very_ awkward to parse back CDATA section to integrate with the current document. Always better to avoid it from the source, viz, to coordinate with the user's application to produce a proper xml stream.

Having said, to do it at the receiving end, with xsl, it cannot be done without extension functionality. A completely separated pre-processing (with language one is familar) may be cleaner before passing it to xsl. But this may not be convenient for some situation.

From within xsl, I can suggest do it with what I am familiar, jscript/vbscript. I can show you how it can be done. It is very awkward for those who do not appreciate ms jscript/vbscript. I would suggest similar thing can be done with other languages. I guess saxon has saxon::parse() to do something similar.

I output to a "text" for illustration to prove you can get those data using methods falling inline with the approach of "parsing." You can store data to variables within the xsl itself for further processing---that's the idea.
[tt]
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl=" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:txjs="urn:tsuji:js">
<xsl:eek:utput method="text" encoding="utf-8" />
<msxsl:script language="jscript" implements-prefix="txjs">
<![CDATA[
function parseout (sfrag,stag) {

//sfrag may just be a well-formed doc fragment stream, string-type
//an artifact testroot is appended as root
//return: error message or stag's text data
//further simplified version

var oparser=new ActiveXObject("msxml2.domdocument");
oparser.async=false;
oparser.validateOnParse=false;
var sxml="<testroot>"+sfrag+"</testroot>";
oparser.loadXML(sxml);
if (oparser.parseError.errorCode==0) {
oparser.setProperty("SelectionLanguage","XPath");
var onode=oparser.selectSingleNode("//"+stag);
if (onode) {
return onode.text;
} else {
return "engine message: inexist"
}
} else {
return "engine message: not well-formed doc fragment"
}
}
]]>
</msxsl:script>
<xsl:variable name="scdata_to_markup">
<xsl:value-of select="normalize-space(/parent/child)" />
</xsl:variable>
<xsl:template match="/">
<xsl:value-of select="txjs:parseout(string($scdata_to_markup),'id')" />
<xsl:text>&#x0d;&#x0a;</xsl:text>
<xsl:value-of select="txjs:parseout(string($scdata_to_markup),'name')" />
<xsl:text>&#x0d;&#x0a;</xsl:text>
<xsl:value-of select="txjs:parseout(string($scdata_to_markup),'inexist')" />
<xsl:text>&#x0d;&#x0a;</xsl:text>
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top