如何在XSD文件中拥有两个或更多的目标命名空间

huangapple go评论61阅读模式
英文:

How to have two or more target namespaces in an XSD file

问题

I understand your request. Here is the translated content you provided:

我对Python和XML都相对新手,目前正在为我的公司开发一个XML验证器。我想要检查传入的XML文件,但是这些文件的命名不遵循正常的命名空间规则,所以我遇到了很多麻烦。我已经找到了一个解决方法,但现在我发现,根据发送方使用的软件版本不同,命名空间会改变。所以现在我的程序又不工作了。

无法更改传入文件中的任何内容,我正在使用Python模块XMLSchema进行验证,不知道这是否会有影响。

我是否可以在我的XSD文件中设置2个或更多不同的目标命名空间来解决这个问题?或者是否有其他解决方案?

传入XML文件的顶部示例:

<?xml version="1.0" encoding="utf-8"?>
<KnudeGroup xmlns="http://www.XXXXX.dk/xml/schemas/XXXXXX/20120102">
  <Referencesys>

另一版本的传入XML文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<LedningGroup xmlns="http://www.XXXXX.dk/xml/schemas/XXXXX/20140701">
<Referencesys>

我的当前解决方法:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  targetNamespace="http://www.XXXXX.dk/xml/schemas/XXXXX/20140701">
  <xs:element name="LedningGroup">

我已经尝试阅读不同的问题和Google这个问题,但大多数解决方案都是修改XML文件,但在我这种情况下这不是可能的。

英文:

I'm fairly new to python and XML and I'm currently working on an XML validator for my firm. I want to check incomming XML files, however, these files do not follow the normal naming with namespaces, so I had a lot of trouble with it. I have found a workaorund, however, I now see that dependent on which version of the software the sender has, the namespace will change. So now my program is not working again.

It is not possible to change anything in the incomming file, and I'm using the python module XMLSchema to validate if that makes a difference.

Is is possible for me to set 2 or more different targetnamespace in my XSD file to workaround the problem?
Or is there another solution?

Example of top of incomming XML file:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;KnudeGroup xmlns=&quot;http://www.XXXXX.dk/xml/schemas/XXXXXX/20120102&quot;&gt;
  &lt;Referencesys&gt;

And the other version of incomming XMl file:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;
&lt;LedningGroup xmlns=&quot;http://www.XXXXX.dk/xml/schemas/XXXXX/20140701&quot;&gt;
&lt;Referencesys&gt;

My current workaround:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;xs:schema attributeFormDefault=&quot;unqualified&quot; elementFormDefault=&quot;qualified&quot; xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot; 
  targetNamespace=&quot;http://www.XXXXX.dk/xml/schemas/XXXXX/20140701&quot;&gt;
  &lt;xs:element name=&quot;LedningGroup&quot;&gt;

I have tried to read the different question here and google the problem, but most solutions are to change the XML file, and that is not a possibility in my case.

答案1

得分: 2

更改模式时更改命名空间通常是我们建议不要做的事情,正是因为这个原因,但人们还是这样做了,所以你必须接受它。

一种可能的方法是“变色龙”命名空间。如果模式文档(模块)M没有目标命名空间,您可以将其xs:include到具有目标命名空间的模式文档中,并且它将“获取”该命名空间;因此,您可以将一个模式文档变色龙式地包含到两个不同的目标命名空间中。

我倾向于采用的另一种方法是,在处理管道的起始阶段通过转换所有传入文档(使用XSLT)来使用单一命名空间,这样管道中的第一步就只需担心命名空间变体。管道的第二步可以进行XSD验证。

英文:

Changing the namespace when the schema changes is something we usually advise against, for exactly this reason, but people do it anyway so you have to live with it.

One possible approach is "chameleon" namespaces. If a schema document (module) M has no target namespace, you can xs:include it into a schema document that does have a target namespace, and it will "acquire" that namespace; so you can chameleon-include one schema document into two different target namespaces.

The other approach, which I tend to prefer, is to start your processing pipeline by transforming all incoming documents (using XSLT) to use a single namespace, so only the first step in the pipeline needs to worry about namespace variants. The second step in the pipeline can do XSD validation.

huangapple
  • 本文由 发表于 2023年5月10日 15:37:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215987.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定