XML Formatting << Back



You can use XSLT to format your existing XML document when saving it to disk. You can now call "SavePrettyXMLDocument" instead of "SaveXMLDocument"

Use Flexml

{ Visibility=Private }	
Function StyleSheetXML For cXMLDomDocument Returns Handle
	Handle hXSL
	String sXSL
	Boolean bSuccess
	Move "" to sXSL
	Append sXSL '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">'
	Append sXSL 	'<xsl:output method="xml" indent="no" omit-xml-declaration="no" />'
	Append sXSL 	'<xsl:template match="node() | @*">'
	Append sXSL 		'<xsl:call-template name="indent">'
	Append sXSL 			'<xsl:with-param name="level" select="count(ancestor::*)"/>'
	Append sXSL 			'<xsl:with-param name="afterFirst" select="' "'" 'new' "'" '"/>'
	Append sXSL 		'</xsl:call-template>'
	Append sXSL 		'<xsl:copy>'
	Append sXSL 			'<xsl:apply-templates select="node() | @*"/>'
	Append sXSL 			'<xsl:if test="*">'
	Append sXSL 				'<xsl:call-template name="indent">'
	Append sXSL 					'<xsl:with-param name="level" select="count(ancestor::*)"/>'
	Append sXSL 				'</xsl:call-template>'
	Append sXSL 			'</xsl:if>'
	Append sXSL 		'</xsl:copy>'
	Append sXSL 	'</xsl:template>'
	Append sXSL 	'<xsl:template name="indent">'
	Append sXSL 		'<xsl:param name="level"/>'
	Append sXSL 		'<xsl:param name="afterFirst">no</xsl:param>'
	Append sXSL 		'<xsl:if test="$level >= 0">'
	Append sXSL 			'<xsl:if test="( ($afterFirst =' "'" 'no' "'" ') or ($afterFirst =' "'" 'new' "'" ' and ($level >0)) ) and (@* or * or text())">'
	Append sXSL 				'<xsl:text>
</xsl:text>'
	Append sXSL 			'</xsl:if>'
	Append sXSL 			'<xsl:if test="$level >0 and (@* or * or text())">'
	Append sXSL 				'<xsl:text>	</xsl:text>'
	Append sXSL 			'</xsl:if>'
	Append sXSL 			'<xsl:call-template name="indent">'
	Append sXSL 				'<xsl:with-param name="level" select="$level - 1"/>'
	Append sXSL 				'<xsl:with-param name="afterFirst" select="' "'" 'yes' "'" '"/>'
	Append sXSL 			'</xsl:call-template>'
	Append sXSL 		'</xsl:if>'
	Append sXSL 	'</xsl:template>'
	Append sXSL '</xsl:stylesheet>'
	Get Create (RefClass(cXMLDOMDocument)) to hXSL
	Get LoadXML of hXSL sXSL to bSuccess
	Function_Return hXSL
End_Function // StyleSheetXML

Function SavePrettyXMLDocument For cXMLDomDocument Returns Integer
	Handle hXSL hXML
	Boolean bSuccess
	Integer iResult
	Address aXML
	String sDocumentName
	Move 0 to iResult
	Get StyleSheetXML to hXSL
	Get XSLTransformationToAddress hXSL to aXML
	Send Destroy of hXSL
	Get Create (RefClass(cXMLDOMDocument)) to hXML
	Get psDocumentName to sDocumentName
	Set psDocumentName of hXML to sDocumentName
	Set pbPreserveWhiteSpace of hXML to True
	Get LoadXMLFromAddress of hXML aXML to bSuccess
	If (bSuccess) Get SaveXMLDocument of hXML to iResult
	Else Move 1 to iResult
	Send Destroy of hXML
	Function_Return iResult
End_Function // SavePrettyXMLDocument






Free Web Hosting