How to mass replace 'inner text' in specific keys in XML file? (7)
Download: 20111223.Profiling.XML.MassReplace as zip package
;==========================================================================================================================================
;
; How to mass replace 'inner text' in specific keys in XML file? (7)
;
; The 'XML XSLT' Version.
;
; (c)Detlev Dalitz.20111223.
;==========================================================================================================================================

;-----------------------------------------------------
;   Ini file "Config.ini".
;
;   [IniFilePass]
;   C01_01=Pass
;   C01_02=Pass
;   C01_03=Pass
;   C01_04=Pass
;   D01_02=Pass
;   D01_03=Not Reporting
;   D01_04=Not Reporting
;   D01_05=Pass
;   E01_01=Pass
;   E01_02=Pass
;   E01_03=Confidential - Not Provided
;   E01_04=Pass
;   E01_05=Pass
;   E01_06=Pass
;   E01_07=Pass
;   E01_08=Not Reporting - Privileged Information
;   E01_09=Pass
;   E01_10=Pass
;-----------------------------------------------------

; Define working folder.
strFolderHome = DirScript ()
DirChange (strFolderHome)

; Define data file pathes.
strFileIni = strFolderHome : "Config.ini"
strFileXmlIn = strFolderHome : "Data.in.xml"
strFileXmlOut = strFolderHome : "Data.out.xml"
; strFileXsl = strFolderHome : "Transform.xsl" ; Disk file is not needed when using the XLST string from within this script.

; For sure, remove existing output file.
If 0 != FileExist (strFileXmlOut) Then FileDelete (strFileXmlOut)


; Prepare XSLT file.
strXslMask1 = `<?xml version="1.0" encoding="utf-8"?>`
strXslMask1 = strXslMask1 : @CRLF : `<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">`
strXslMask1 = strXslMask1 : @CRLF : `<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/>`
strXslMask1 = strXslMask1 : @CRLF : `<!-- Identical copy -->`
strXslMask1 = strXslMask1 : @CRLF : `<xsl:template match="@*|*">`
strXslMask1 = strXslMask1 : @CRLF : `<xsl:copy>`
strXslMask1 = strXslMask1 : @CRLF : `<xsl:apply-templates select="@*|node()"/>`
strXslMask1 = strXslMask1 : @CRLF : `</xsl:copy>`
strXslMask1 = strXslMask1 : @CRLF : `</xsl:template>`
strXslMask1 = strXslMask1 : @CRLF : `<!-- Privileged data -->{1}`
strXslMask1 = strXslMask1 : @CRLF : `</xsl:stylesheet>`
strXslMask2 = `<xsl:template match="{1}"><{1}>{2}</{1}>&#xD;&#xA;</xsl:template>`

; Open ini file and walk through the items, create XSLT stylesheet.
strXsl = ""
strIniSection = "IniFilePass"
strListItems = IniItemizePvt (strIniSection, strFileIni)
intItems = ItemCount (strListItems, @TAB)
For intI = 1 To intItems
   strKey = ItemExtract (intI, strListItems, @TAB)
   strValue = IniReadPvt (strIniSection, strKey, "Pass", strFileIni)
   If strValue == "Pass" Then Continue
   strXsl = strXsl : @CRLF : StrReplace (StrReplace (strXslMask2, "{1}", strKey), "{2}", strValue)
Next
strXsl = StrReplace (strXslMask1, "{1}", strXsl)
;intBytesWritten = FilePut(strFileXsl, strXsl) ; Disk file is not needed when using the XLST string from within this script.

; Load the Microsoft XML DOM Object Interface.
objDocXml = ObjectCreate ("Msxml2.DOMDocument.6.0", "")
objDocXsl = ObjectCreate ("Msxml2.DOMDocument.6.0", "")

; Read XML file into DOM buffer.
blnResult = objDocXml.load(strFileXmlIn)

; Read XSL file ... or ... string into DOM buffer.
;blnResult = objDocXsl.load(strFileXsl) ; Load file.
blnResult = objDocXsl.loadXML(strXsl)   ; Load string.

; Transform.
strTransform = objDocXml.transformNode(objDocXsl)

; Write transformed data to file.
FilePut (strFileXmlOut, strTransform)

; Close objects.
objDocXsl = 0
objDocXml = 0

; If this script has been called from another WB script, then return to the caller from here ...
If RtStatus () != 10 Then If IntControl (77, 80, 0, 0, 0) > 0 Then Return ; ... when not in WinBatch Studio debug mode.

; Display result.
ShellExecute (strFileXmlOut, "", "", @NORMAL, "")

:CANCEL
Exit