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

Possible to set xsi:schemalocation? #505

Closed
netsensei opened this issue Oct 10, 2015 · 2 comments
Closed

Possible to set xsi:schemalocation? #505

netsensei opened this issue Oct 10, 2015 · 2 comments

Comments

@netsensei
Copy link

While serializing a set of PHP objects into XML: I'm failing to set the xsi:schemalocation attribute on the root element.

I'm using YML annotations for the root level element.

I've tried configuring it like this:

myRootElement:
    xml_namespaces:
        "example": "http://www.example-schema.org"
        "xsi": "http://www.w3.org/2001/XMLSchema-instance"
        "xsi:location": "http//example.com/schema.xsd"

This will throw an error DOMException: Namespace Error in XmlSerializationVisitor.php on line 203. The addNamespaceAttributes internally always prefixes xmlns to the prefix, so you end up with something like: xmlns:xsi:location. The setAttributeNS call is not accepting that as a qualified attribute name.

Is there a way to work around this?

@netsensei
Copy link
Author

Okay, looks like #433 provides a workaround:

The idea is that I add an extra property to my root class and add an XmlAttributeMap property which defines the xsi:location namespace instead of using xml_namespaces:

Something along these lines:

YAML:

myRootElement:
    xml_namespaces:
        "example": "http://www.example-schema.org"
        "xsi": "http://www.w3.org/2001/XMLSchema-instance"
   properties:
       xsiLocation:
          xml_attribute_map: true

PHP

class myRootElement {
   private $xsiLocation;

   public function __construct()
   {
      $this->xsiLocation = ["xsi:location" => "http//example.com/schema.xsd"];
   }
}

This feels a it counterintuitive: are there better solutions / alternatives where I can avoid hard coding the xsd location in a class?

@netsensei
Copy link
Author

Okay. I tried the above in my own project: this works for me. Gave it some more thought: xsi:location is a property within the xsi namespace. So it's not a namespace in it's own right: It's an attribute. The only confusing thing is that it's an attribute to the same element which defines the xsi namespace.

So, putting it in xml_attribute_map instead of xml_namespaces is perfectly sound.

I'll close this.

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

1 participant