I have extracted some code from codeproject to reindent an XML document. Does anyone know how I can modify the stylesheet to make it so that the transform of an XML file will result in empty tags showing up as <tag /> instead of <tag></tag>?
// http://www.codeproject.com/Articles/43309/How-to-create-a-simple-XML-file-using-MSXML-in-C MSXML2::IXMLDOMDocumentPtr FormatDOMDocument(MSXML2::IXMLDOMDocumentPtr pDoc) { LPCSTR const static szStyleSheet = R"!(<?xml version="1.0" encoding="utf-8"?>)!" R"!(<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">)!" R"!( <xsl:output method="xml" indent="yes"/>)!" R"!( <xsl:template match="@* | node()">)!" R"!( <xsl:copy>)!" R"!( <xsl:apply-templates select="@* | node()"/>)!" R"!( </xsl:copy>)!" R"!( </xsl:template>)!" R"!(</xsl:stylesheet>)!"; MSXML2::IXMLDOMDocumentPtr pXmlStyleSheet; pXmlStyleSheet.CreateInstance(__uuidof(MSXML2::DOMDocument60)); pXmlStyleSheet->loadXML(szStyleSheet); MSXML2::IXMLDOMDocumentPtr pXmlFormattedDoc; pXmlFormattedDoc.CreateInstance(__uuidof(MSXML2::DOMDocument60)); CComPtr<IDispatch> pDispatch; HRESULT hr = pXmlFormattedDoc->QueryInterface(IID_IDispatch, (void**)&pDispatch); if (SUCCEEDED(hr)) { _variant_t vtOutObject; vtOutObject.vt = VT_DISPATCH; vtOutObject.pdispVal = pDispatch; vtOutObject.pdispVal->AddRef(); hr = pDoc->transformNodeToObject(pXmlStyleSheet, vtOutObject); } //By default it is writing the encoding = UTF-16. Let us change the encoding to UTF-8 // <?xml version="1.0" encoding="UTF-8"?> MSXML2::IXMLDOMNodePtr pXMLFirstChild = pXmlFormattedDoc->GetfirstChild(); // A map of the a attributes (vesrsion, encoding) values (1.0, UTF-8) pair MSXML2::IXMLDOMNamedNodeMapPtr pXMLAttributeMap = pXMLFirstChild->Getattributes(); MSXML2::IXMLDOMNodePtr pXMLEncodNode = pXMLAttributeMap->getNamedItem(_T("encoding")); pXMLEncodNode->PutnodeValue(_T("UTF-8")); //encoding = UTF-8 return pXmlFormattedDoc; }
Or, if there is some other method for reindenting a MSXML2::IXMLDOMDocumentPtr object where I can specify how I want empty tags to be stored, that would be great too. However, I don't want it to lose its status of an MSXML2::IXMLDOMDocumentPtr object. I.e. I would like to still perform operations on the result as if it was still an MSXML2::IXMLDOMDocumentPtr object.
Thanks,
A
Adrian