使用perl的XML :: LibXML如何使用XML前缀而不是xmlns属性? [英] Using perl's XML::LibXML how do you use XML Prefixes and not xmlns attributes?

查看:58
本文介绍了使用perl的XML :: LibXML如何使用XML前缀而不是xmlns属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信这个问题以前曾尝试过2006年在另一个网站上.但是,我当前的XML/RDF编写器(XML::LibXML 1.70)以xmlns属性的形式输出元素名称空间.这将排除使用不了解名称空间的解析器的人,他们只是对foaf:Person做look_down.我想知道是否有人首先使用XML::LibXML在perl中实现这一目标的简单方法.或通过其他方式.

I believe this question might have been previously attempted in 2006 on a different site. But, my current XML/RDF writer (XML::LibXML 1.70) outputs element namespaces in the form of xmlns attributes. This will exclude people using non-namespace aware parsers who just do a look_down for foaf:Person. I'm wondering if anyone knows of an easy way in perl to achive this, firstly, with XML::LibXML. Or through a different means.

这样的节点:

  <Person xmlns="http://xmlns.com/foaf/0.1/" rdf:ID="me"/>

而且,这个:

  <name xmlns="http://xmlns.com/foaf/0.1/">Evan Carroll</name>

真的应该像这样:

  <foaf:Person rdf:ID="me"/>
  <foaf:name>Evan Carroll</name>

有什么想法吗?我相信这两种方法在技术上都是正确的,但我宁愿不依赖其他人知道这一点.昨天我自己不知道.

Any ideas? I believe it is technically correct either way, but I'd much rather not depend on other people knowing this. I didn't know it myself yesterday.

推荐答案

简短的答案是,如果您已经有一个namespaceURI和声明的前缀,则可以指定限定名称(即prefix:localName)作为元素名称,这将使XML :: LibXML避免重新声明名称空间.因此,修改最后一个问题中的代码将得到以下内容,该代码确实使用了所需的名称空间前缀:

The short answer is that if you already have a namespaceURI and prefix declared you can specify the qualified name (i.e. prefix:localName) as an element name and this will make XML::LibXML avoid redeclaring the namespace. So, modifying the code from the last question gives the following, which does use the desired namespace prefixes:

#! /usr/bin/perl 
use warnings;
use strict;
use XML::LibXML;
my $doc = XML::LibXML::Document->new( '1.0', 'UTF-8' );
my $foaf = $doc->createElementNS( 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'RDF' );
$doc->setDocumentElement( $foaf );
$foaf->setNamespace( 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' , 'rdf', 1 );
$foaf->setNamespace( 'http://www.w3.org/2000/01/rdf-schema#' , 'rdfs', 0 );
$foaf->setNamespace( 'http://xmlns.com/foaf/0.1/' , 'foaf', 0 );
$foaf->setNamespace( 'http://webns.net/mvcb/' , 'admin', 0 );
my $node = $doc->createElementNS( 'http://xmlns.com/foaf/0.1/', 'foaf:Person');
$foaf->appendChild($node);
$node->setAttributeNS( 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'ID', 'me');
my $node2 = $doc->createElementNS( 'http://xmlns.com/foaf/0.1/', 'foaf:name');
$node2->appendTextNode('Evan Carroll');
$node->appendChild($node2);
print $doc->toString;


也许值得尝试回顾一下正在发生的事情. XML命名空间可以在同一条件下一起使用多个词汇表XML文件.为了实现这一点,引入了namespaceURI(nsURI)的概念,并指出了将哪个nsURI与XML文档中的哪些元素和属性相关的机制改型为XML.为此,可以使用以下事实:属性名称以'xml'开头保留,允许使用特殊的属性名称(xmlns),而不会发生冲突.


It is perhaps worth trying to review what's going on though. XML Namespaces exist to allow multiple vocabularies to be used together, in the same XML document. To achieve this the concept of a namespaceURI (nsURI) is introduced and a mechanism of indicating which nsURI relates to which elements and attributes in an XML document is retrofitted onto XML. To do this use is made of the fact that attribute names starting 'xml' are reserved allowing a special attribute name (xmlns) to be used without the risk of collision.

通常的想法是,可以将XML文档中使用的每个词汇表与唯一的nsURI(被视为不透明字符串)链接起来. XHTML词汇表中的head元素由{' http://www.w3.org/1999完全定义/xhtml ':'head'},这与(假设的)解剖学ML {'my-made-up-URI':'head'}中的头部明显不同.问题是如何将nsURI嵌入到XML文档中以及如何将它们链接到元素名称.

The general idea is that it is possible to link each vocabulary used in an XML document with a unique nsURI (which is treated as an opaque string). The head element in the XHTML vocabulary is fully defined by {'http://www.w3.org/1999/xhtml':'head'}, and this is clearly different from the head in a (hypothetical) anatomy-ML {'my-made-up-URI':'head'}. The issue is how to embed the nsURI(s) in an XML document and how to link these to the element names.

在nsURI和元素名称之间建立链接的一种方法是将xmlns属性添加到元素.例如:

One way to make the link between a nsURI and an element name is to add the xmlns attribute to the element. For example:

<name xmlns="http://xmlns.com/foaf/0.1/">Evan Carroll</name>

表示名称"在" http://xmlns.com/foaf/0.1/'名称空间.命名空间声明由子代继承,因此'age'在同一命名空间中:

says that 'name' is in the 'http://xmlns.com/foaf/0.1/' namespace. Namespace declarations are inherited by children, so 'age' is in the same namespace:

<name xmlns="http://xmlns.com/foaf/0.1/">Evan Carroll<age years='21'/></name>

这可以很好地工作并且非常紧凑.但是,它不适用于属性,如果许多同级节点需要从其公共父级更改名称空间,则可能会变得混乱.为了解决这两个问题,引入了NamespacePrefix(nsPrefix).这使冒号具有特殊含义.这个想法是将nsURI链接到当前文档中使用的字符串.这在文档之外没有任何特殊含义,不应由词汇表指定(但有时会在其他地方进行讨论).在根元素上声明所有nsURI尤其常见.语法是这样声明命名空间的:

This can work well and be quite compact. However, it doesn't work for attributes and can get messy if lots of sibling nodes need to change namespace from their common parent. To deal with both of these problem the NamespacePrefix (nsPrefix) is introduced. This gives the colon special meaning. The idea is to link the nsURI to a string that is used in the current document. This doesn't have any special meaning outside the document and shouldn't be specified by the vocabulary (but it sometimes is, discussion for elsewhere). It's particularly common for all nsURI's to be declared on the root element. The syntax is to declare the namespace thus:

xmlns:prefix="http://xmlns.com/foaf/0.1/"

并通过在nsPrefix名称之前添加它来在属性和元素名称中使用它:

and use it in attribute and element names by prepending the nsPrefix to the name:

<prefix:name prefix:attribute='value'/>

由于nsPrefixes的确切值无关紧要,因此API通常不会使访问/设置它们非常容易(Xpath是一个很好的例子).使用名称空间会导致对文档的一些约束,这些约束应视为错误,使用未定义的前缀就是一个例子.但是,根据XML规范,这样的文档可以采用正确的格式(记住要对名称空间进行改型).您可以将此类文档描述为名称空间格式不正确".

Because the exact value of nsPrefixes are not supposed to matter, API's generally don't make accessing / setting them very easy (Xpath is a good example). Having namespaces leads to some constraints on the document that should be treated as errors, using a prefix that isn't defined is an example. But such a document can be well-formed according to the XML specification (remember namespaces are retrofitted). You can describe such a document as 'not being namespace well-formed'.

如果您事先知道使用的名称空间前缀,那么使用不了解名称空间的解析器来解析使用名称空间的文档显然会更容易.但这是一个非常脆弱的解决方案,因为在重复处理XML文档时,名称空间前缀可能会在奇怪的地方发生变化.大多数解析器都知道名称空间.

Parsing a document that uses namespaces with a parser that dosn't know anything about namespaces is obviously easier if you know the namespace prefixes used in advance. But this is quite a brittle solution as namespace prefixes can change in odd places as an XML document is repeatedly processed. Most parsers are namespace aware.

这篇关于使用perl的XML :: LibXML如何使用XML前缀而不是xmlns属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆