开发者

Shouldn't an XElement inherit the namespace from its parent Xelement?

I'm using Linq for XML in .NET 3.5 for the first time, and I'm having some trouble with namespaces. Namely, the XElement is printed like this : <opf:metadata> when I just want it to say <metadata>.

Here is the code:

    XNamespace opfNamespace = "http://www.idpf.org/2007/opf";
        XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
        XNamespace dcterms = "http://purl.org/dc/terms/";
        XNamespace dc = "http://purl.org/dc/elements/1.1/";

        opfRoot = new XElement(opfNamespace + "package",
                                    new XAttribute("version", "2.0"),
                                    new XAttribute("unique-identifier", "uuid_id"));
        XElement metadata = new XElement(opfNamespace + "metadata",
                                    new XAttribute(XNamespace.Xmlns + "xsi", xsi),
                                    new XAttribute(XNamespace.Xmlns + "opf", opfNamespace),
                                    new XAttribute(XNamespace.Xmlns + "dcterms", dcterms),
                                    new XAttribute(XNamespace.Xmlns + "dc", dc),
                                    new XElement(dc + "language", "pt-BR"));
        opfRoot.Add(metadata);

And here is the result:

<?xml version="1.0" encoding="utf-8"?>
<package ver开发者_如何学Gosion="2.0" unique-identifier="uuid_id" xmlns="http://www.idpf.org/2007/opf">
  <opf:metadata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:opf="http://www.idpf.org/2007/opf" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <dc:language>pt-BR</dc:language>
  </opf:metadata>
</package>

I expected it the metadata element to come without the opf: "opf:metadata"

If I remove the opfNameSpace from {opfNameSpace + "metadata"}, I get a blank xmlns namespace (xmlns="") in the metadata element.

I looked at this thread:

How can I write xml with a namespace and prefix with XElement?

and this one:

XElement default namespace on attributes provides unexpected behaviour

But they didn't solve my problem. Any ideas?


It's because you're redeclaring the opf namespace alias in the metadata element. Just remove this attribute:

new XAttribute(XNamespace.Xmlns + "opf", opfNamespace)

from the <metadata> constructor call, and it works fine:

<package version="2.0" unique-identifier="uuid_id"
         xmlns="http://www.idpf.org/2007/opf">
  <metadata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dcterms=
"http://purl.org/dc/terms/" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <dc:language>pt-BR</dc:language>
  </metadata>
</package>

Now you could argue that because you're redeclaring the namespace alias to have the same URL as before, it should be irrelevant - to be honest, I don't know what the spec says about that. But it seems somewhat reasonable.

On the other hand, I believe the two XML forms should end up being treated equivalently by anything parsing them, so if it's more convenient to create the metadata element with all the relevant declarations, then it shouldn't cause any problems... AFAIK :)


Remove new XAttribute(XNamespace.Xmlns + "opf", opfNamespace),.


Since the namespace formatting I needed was very specific, and it wasnt't being generated correctly even with the opf:metadata problem resolved, I found a better way. I just created a XmlDocument mannualy, and added the XML. From there I can edit it programatically. Like so:

            XmlDocumentopf = new XmlDocument();
            opf.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
                            "<package version=\"2.0\" unique-identifier=\"uuid_id\" xmlns=\"http://www.idpf.org/2007/opf\">" +
                            "<metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:opf=\"http://www.idpf.org/2007/opf\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:dcterms=\"http://purl.org/dc/terms/\">" +
                            "<dc:title>Title of this book</dc:title>" +
                            "<dc:language>pt-BR</dc:language>" +
                            "<dc:identifier id=\"uuid_id\" opf:scheme=\"uuid\">a335de4c-560f-4b75-bcba-1418b726f92c</dc:identifier>" +
                            "<dc:creator opf:role=\"aut\">Desconhecido(a)</dc:creator>" +
                            "<dc:date>2011-01-31T11:59:46.949000+00:00</dc:date>" +
                            "<meta name=\"cover\" content=\"book-cover\"/>" +
                            "</metadata>" +
                            "<manifest />" +
                            "<spine toc=\"ncx\" />" +
                            "<guide />" +
                            "</package>");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜