Skip to content

Instantly share code, notes, and snippets.

@martin-honnen
Created September 5, 2023 09:15
Show Gist options
  • Select an option

  • Save martin-honnen/c5070070d12ac4ba5c72d39afbdae65d to your computer and use it in GitHub Desktop.

Select an option

Save martin-honnen/c5070070d12ac4ba5c72d39afbdae65d to your computer and use it in GitHub Desktop.
MSXMLParseXmlExtensionExample1
<?xml version="1.0" encoding="UTF-8"?>
<result name="HostIntegrationRequest" id="0071" status="0">
<XML>
<![CDATA[<Document xmlns="urn:iso:std:iso:20022:tech:xsd:taco.097.001.90"><GrpHdr><MsgId>20230825066</MsgId></GrpHdr></Document>]]></XML>
<RefId><![CDATA[4]]></RefId>
</result>
var xmlDoc = new ActiveXObject('Msxml2.DOMDocument.6.0');
xmlDoc.load('embedded-xml-sample1.xml');
var xsltDoc = new ActiveXObject('Msxml2.FreeThreadedDOMDocument.6.0');
xsltDoc.load('parseXmlExample1.xsl');
var xsltTemplate = new ActiveXObject('Msxml2.XSLTemplate.6.0');
xsltTemplate.stylesheet = xsltDoc;
var xsltProcessor = xsltTemplate.createProcessor();
xsltProcessor.addObject({ "parseXml": function(xml) { var doc = new ActiveXObject('Msxml2.DOMDocument.6.0'); doc.loadXML(xml); return doc; }}, 'http://example.com/mf');
xsltProcessor.input = xmlDoc;
xsltProcessor.transform();
WScript.Echo(xsltProcessor.output);
xsltDoc = new ActiveXObject('Msxml2.FreeThreadedDOMDocument.6.0');
xsltDoc.load('parseXmlExample2.xsl');
var xsltTemplate = new ActiveXObject('Msxml2.XSLTemplate.6.0');
xsltTemplate.stylesheet = xsltDoc;
xsltProcessor = xsltTemplate.createProcessor();
xsltProcessor.addObject({ "parseXml": function(xml) { var doc = new ActiveXObject('Msxml2.DOMDocument.6.0'); doc.loadXML(xml); return doc; }}, 'http://example.com/mf');
xsltProcessor.input = xmlDoc;
xsltProcessor.transform();
WScript.Echo(xsltProcessor.output);
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:mf="http://example.com/mf">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="XML">
<xsl:copy>
<xsl:apply-templates select="mf:parseXml(string())"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:mf="http://example.com/mf"
xmlns:iso="urn:iso:std:iso:20022:tech:xsd:taco.097.001.90"
exclude-result-prefixes="mf iso">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="XML">
<element1>
<xsl:variable name="embeddedDoc" select="mf:parseXml(string())"/>
<xsl:value-of select="$embeddedDoc/iso:Document/iso:GrpHdr/iso:MsgId"/>
</element1>
</xsl:template>
</xsl:stylesheet>
using System.Xml.XPath;
using System.Xml.Xsl;
XslCompiledTransform xsltProcessor = new XslCompiledTransform();
xsltProcessor.Load("parseXmlExample1.xsl");
var xsltArguments = new XsltArgumentList();
xsltArguments.AddExtensionObject("http://example.com/mf", new XsltExtensions());
xsltProcessor.Transform("embedded-xml-sample1.xml", xsltArguments, Console.Out);
xsltProcessor = new XslCompiledTransform();
xsltProcessor.Load("parseXmlExample2.xsl");
xsltArguments = new XsltArgumentList();
xsltArguments.AddExtensionObject("http://example.com/mf", new XsltExtensions());
xsltProcessor.Transform("embedded-xml-sample1.xml", xsltArguments, Console.Out);
public class XsltExtensions
{
public static XPathNavigator parseXml(string xml)
{
var doc = new XPathDocument(new StringReader(xml));
return doc.CreateNavigator();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment