I have received assistance in other posts in this forum to achieve each of the subject goals as separate processes, but I just can't work out how to combine those into a single process.
The following code defines two XSLT templates and contains two functions. The first function returns the desired output. I first use the SelectSingleNode method to isolate the node of interest and then apply the XSLT
to purge the unwanted attributes. I am left with the XML string I need for a future comparison.
The node of interest is defined by the XPath:
"/pkg:package[1]/pkg:part[3]/pkg:xmlData[1]/w:document[1]/w:body[1]"
In the second template I am trying to first isolate and extract the XML of that node from the complete XML and then purge the unwanted attributes using only XSLT. I'm failing miserably. Thanks for any assistance offered.
To set up the code just copy and paste it in a new blank Word document.
Option Explicit 'This works to purge the unwanted rsid attributes. Const strXSLTI As String = "<xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"">" _& "<xsl:output indent=""yes""/><xsl:template match=""*""><xsl:copy>" _& "<xsl:copy-of select=""@*[not(contains(name(),'w:rsid'))]""/>" _& "<xsl:apply-templates/>" _& "</xsl:copy></xsl:template></xsl:stylesheet>" 'This doesn't work. Trying to get only one specific node and then purge the unwanted rsid attributes. Const strXSLTII As String = "<xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"" " _& "xmlns:pkg=""http://schemas.microsoft.com/office/2006/xmlPackage"" " _& "xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">" _& "<xsl:output indent=""yes""/><xsl:template match=""*""><xsl:copy>" _& "<xsl:copy-of select=""/pkg:package[1]/pkg:part[3]/pkg:xmlData[1]/w:document[1]/node() [name() = 'w:body']""/>" _& "<xsl:copy-of select=""@*[not(contains(name(),'w:rsid'))]""/>" _& "<xsl:apply-templates/>" _& "</xsl:copy></xsl:template></xsl:stylesheet>" Sub Test() With ActiveDocument.Range .Delete .ContentControls.Add wdContentControlRichText End With ActiveDocument.ContentControls(1).Range.Text = "Test" 'This works to return the desire output. Debug.Print fcnXMLStringI(ActiveDocument.ContentControls(1)) 'This isn't working :-( Debug.Print fcnXMLStringII(ActiveDocument.ContentControls(1)) lbl_Exit: Exit Sub End Sub Function fcnXMLStringI(oCC As ContentControl) As String Dim oXDoc As Object Dim oNode As Object Dim oXDocClipped As Object Dim oXSLT As Object 'This works Set oXDoc = CreateObject("MSXML2.DOMDocument") oXDoc.LoadXML (oCC.Range.WordOpenXML) oXDoc.setProperty "SelectionLanguage", "XPath" oXDoc.setProperty "SelectionNamespaces", _"xmlns:pkg='http://schemas.microsoft.com/office/2006/xmlPackage'" & _" xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'" 'Isolate and define the single node of interest. Set oNode = oXDoc.SelectSingleNode("/pkg:package[1]/pkg:part[3]/pkg:xmlData[1]/w:document[1]/w:body[1]") Set oXSLT = CreateObject("MSXML2.DOMDocument") Set oXDocClipped = CreateObject("MSXML2.DOMDocument") oXDocClipped.LoadXML oNode.XML oXSLT.LoadXML strXSLTI 'Strip the unwanted attribute nodes. fcnXMLStringI = oXDocClipped.transformNode(oXSLT) End Function Function fcnXMLStringII(oCC As ContentControl) As String Dim oXDoc As Object Dim oXSLT As Object 'This does not work Set oXDoc = CreateObject("MSXML2.DOMDocument") oXDoc.LoadXML (oCC.Range.WordOpenXML) Set oXSLT = CreateObject("MSXML2.DOMDocument") oXSLT.LoadXML strXSLTII fcnXMLStringII = oXDoc.transformNode(oXSLT) End Function
Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm