none
How to specify or change XML encoding after or before XSL Transform using MSXML RRS feed

  • Question

  • Hi to everyone!

    I'm using MSXML importing "MSXML.dll" to transform a xml file; by default, MSXML specify encoding using:
    <?xml version=\"1.0\" encoding=\"UTF-16\"?">, but i need to use ISO-8859-1. I'm actually doing it using createProcessingInstruction and insertBefore, but finally in the file stay both enconding tags...

    I wanna know if there is a way to specify the encoding before the xsl transformation, or where MSXML finds the encoding to use...

    I'm using c++ under VStudio 2005

    Thanx a lot.
    Tuesday, March 18, 2008 3:41 PM

All replies


  • hello
    i'm receive this error too, so i use diffrent method for this task (thx to Jerry.Wang for base idea)
    assume   MSXML2::IXMLDOMDocument2Ptr m_pDoc;

         try using

    Code Snippet

    Transform2("<?xml version=\"1.0\"?><xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\"><xsl:output method=\"xml\" encoding=\"ISO-8859-1\"/><xsl:template match=\"*\"><xsl:copy><xsl:apply-templates /></xsl:copy></xsl:template></xsl:stylesheet>",szDestFile).


      
        where Transform2 is

       
    Code Snippet

    BOOL Transform2( LPCTSTR strXslTransform, CString strDestFile)
        {
            ASSERT( m_pDoc != NULL );
            HRESULT hr = S_OK;

            short sResult = FALSE;
            MSXML2::IXMLDOMDocument2Ptr pStyleSheet;
            MSXML2::IXSLTemplatePtr pIXSLTemplate;
            MSXML2::IXSLProcessorPtr pIXSLProcessor;
            IStream * pOutStream    = NULL;
            LPVOID pOutput            = NULL;

            try
            {
                hr = pIXSLTemplate.CreateInstance( __uuidof(MSXML2::XSLTemplate) );
                ASSERT( SUCCEEDED(hr) );

                hr = pStyleSheet.CreateInstance( __uuidof(MSXML2::FreeThreadedDOMDocument) );
                ASSERT( SUCCEEDED(hr) );

                VARIANT_BOOL vbSuccessful = VARIANT_TRUE;
                vbSuccessful = pStyleSheet->loadXML( _bstr_t(strXslTransform) );

                ASSERT( vbSuccessful == VARIANT_TRUE );

                hr = pIXSLTemplate->putref_stylesheet(pStyleSheet);
                if( !SUCCEEDED(hr) )
                {
                    AfxMessageBox( _T("Error occurs when transform xsl!") );
                    return FALSE;
                }

                pIXSLProcessor = pIXSLTemplate->createProcessor();
                ASSERT( pIXSLProcessor != NULL );

                CreateStreamOnHGlobal( NULL, TRUE, &pOutStream);
                pIXSLProcessor->put_output(_variant_t(pOutStream));

                hr = pIXSLProcessor->put_input( _variant_t((IUnknown*)m_pDoc) );
                ASSERT( SUCCEEDED(hr) );

                vbSuccessful = pIXSLProcessor->transform();
                ASSERT( vbSuccessful == VARIANT_TRUE );



                HGLOBAL hg = NULL;
                hr = pOutStream->Write( (void const*)_T("\0"), 1, 0);
                GetHGlobalFromStream( pOutStream, &hg);
                GlobalUnlock(hg);

                LARGE_INTEGER start = {0, 0};
                ULARGE_INTEGER cb = {0, 0};
                pOutStream->Seek( start, STREAM_SEEK_CUR, &cb);

                pOutput = GlobalLock(hg);
                CFile file;
                file.Open( strDestFile, CFile::modeCreate | CFile::modeWrite, NULL);
                file.Write( pOutput, (UINT)cb.QuadPart);
                file.Flush();
                file.Close();
                GlobalUnlock(hg);

                RELEASE_PTR(pIXSLProcessor);
                RELEASE_PTR(pStyleSheet);
                RELEASE_PTR(pIXSLTemplate);


                return TRUE;
            }
            catch( _com_error e)
            {
                TRACE( _T("Transform failed:%s\n"), e.ErrorMessage());
                ASSERT( FALSE );
                return FALSE;
            }

        }


     




    • Proposed as answer by bluish5 Tuesday, December 13, 2011 2:26 PM
    Wednesday, April 9, 2008 2:45 PM
  • The documenation says that if we try to read the output property, then it's converted into UTF-16.

    So I do it in this way:

    /*! Transforms XML file at asXMLSource
        into a new XML file at asDest
        using the XSLT file at asXSLSource
    */
    void __fastcall Transform(
      AnsiString asXMLSource,
      AnsiString asXSLSource,
      AnsiString asDest )
    {
      // Load source XML
      IXMLDOMDocumentPtr pXml = CoFreeThreadedDOMDocument::Create();
      HRESULT  rslt = (int) pXml->load( WideString(asXMLSource) );
      if ( rslt == 0 ){
        ShowMessage("Document not loaded: "+String(asXMLSource) );
        return;
      }
    
      // Load XSLT
      IXMLDOMDocumentPtr pXsl = CoFreeThreadedDOMDocument::Create();
      rslt = (int) pXsl->load( WideString(asXSLSource) );
      if( rslt == 0){
        ShowMessage("Document not loaded: "+String(asXSLSource)
        return;
      }
      IXSLTemplatePtr   pXslTemplate = CoXSLTemplate::Create();
      pXslTemplate->stylesheet = pXsl;
      IXSLProcessorPtr proc = pXslTemplate->createProcessor();
     
      // Prepare output XML
      IXMLDOMDocumentPtr pXmlOutput = CoFreeThreadedDOMDocument::Create();
    
      proc->set_input ( (IUnknown*) pXml       );
      proc->set_output( (IUnknown*) pXmlOutput );
      // XSLT transformation
      proc->transform();
      // Save to file
      pXmlOutput->save(asDest);
    }
    

    • Proposed as answer by bluish5 Friday, December 16, 2011 1:37 PM
    • Edited by bluish5 Friday, December 16, 2011 1:38 PM
    Friday, December 16, 2011 1:37 PM