Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wujwmail opened this issue May 11, 2024 · 6 comments

Comments

@wujwmail
Copy link

wujwmail commented May 11, 2024

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
Copy link
Author

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
Copy link
Author

wujwmail commented May 13, 2024

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
Copy link
Member

XmlTextWriter seems to work ok.

Do you think it's compatible with XmlWriter?

@wujwmail
Copy link
Author

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
Copy link
Member

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

@wujwmail
Copy link
Author

This problem has been solved using XmlTextWriter! For details, see: #1140

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants