peachpiecompiler / peachpie

PeachPie - the PHP compiler and runtime for .NET and .NET Core
https://www.peachpie.io
Apache License 2.0
2.31k stars 201 forks source link

Peachpie.Library.XmlDom.XMLWriter: Error writing namespace attribute 'xmlns' #1142

Open wujwmail opened 1 month ago

wujwmail commented 1 month ago

Running error message:

        Unhandled exception. System.Xml.XmlException: The prefix '' cannot be redefined from '' to 'http://schemas.openxmlformats.org/package/2006/content-types' within the same start element tag.
           at System.Xml.XmlWellFormedWriter.PushNamespaceExplicit(String prefix, String ns)
           at System.Xml.XmlWellFormedWriter.WriteEndAttribute()
           at Peachpie.Library.XmlDom.XMLWriter.<>c__DisplayClass41_0.<writeAttribute>b__2()
           at Peachpie.Library.XmlDom.XMLWriter.CheckedCall(Action operation)
           at Peachpie.Library.XmlDom.XMLWriter.writeAttribute(String name, String content)
           ....

PHPExcel source code (PHPExcel-1.8.1): https://github.com/PHPOffice/PHPExcel

\PHPExcelPhpCsLib\PHPExcel\Writer\Excel2007\ContentTypes.php:line 61 This statement seems to be wrong:

     $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types');

demo download -> https://github.com/wujwmail/PHPExcel_test/tree/master


A php example for testing writeAttribute

<?php

  $xml = new XMLWriter();
  $xml->openMemory();

  $xml->startElement('element');
  /*
    An error occurs when using xmlns attribute names
  */
  $xml->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types');

  /*
    It is normal to use attribute names other than xmlns attribute names.
  */
  $xml->writeAttribute('not_xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types');

  $xml->endElement();

  echo $xml->flush();
wujwmail commented 1 month ago

C# test Peachpie.Library.XmlDom.XMLWriter

       using System;
       using Pchp.Core;
       using Peachpie.Library.XmlDom;
       ......
        static void Main(string[] args)
        {
            Context ctx = Context.CreateConsole(null, args);
            XMLWriter xw = new XMLWriter();
            xw.openMemory(ctx);
            xw.startElement("element");
            xw.writeAttribute("xmlns", "http://schemas.openxmlformats.org/package/2006/content-types");
           // xw.writeAttribute("not_xmlns", "http://schemas.openxmlformats.org/package/2006/content-types");
            xw.endElement();
            Console.WriteLine(xw.flush());
            Console.ReadLine();
        }
wujwmail commented 1 month ago

Solve the program segment with special attribute name "xmlns" in C

            System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings();
            settings.Indent = true;

            using (var writer = System.Xml.XmlWriter.Create(Console.Out, settings))
            {
                writer.WriteStartElement("element", "http://schemas.openxmlformats.org/package/2006/content-types");
                // Write other elements and attributes
                writer.WriteAttributeString("other", "123");
                writer.WriteEndElement();
            }

The result of running the program is:

    <element other="123" xmlns="http://schemas.openxmlformats.org/package/2006/content-types" />

Or use XmlTextWriter to solve this problem

            XmlTextWriter writer = new XmlTextWriter(Console.Out);
            writer.Formatting = Formatting.Indented;
            writer.WriteStartElement("Items");
            writer.WriteAttributeString("xmlns", "http://schemas.openxmlformats.org/package/2006/content-types");

            writer.WriteEndElement();
            writer.Close();
jakubmisek commented 1 month ago

XmlTextWriter seems to work ok.

Do you think it's compatible with XmlWriter?

wujwmail commented 1 month ago

XmlTextWriter inherits from XmlWriter and can be replaced by XmlTextWriter. However, there is a small problem that the XmlWriterSettings class cannot be injected. Let me try and see if there is a way to solve it?

jakubmisek commented 1 month ago

I see! the missing XmlSettings might be a problem :/

wujwmail commented 1 month ago

This problem has been solved using XmlTextWriter! For details, see: https://github.com/peachpiecompiler/peachpie/pull/1140

mindfocus commented 3 days ago

1140 can also solve #761? @wujwmail