如何重新排序节点元素,使其与 xsd:sequence 顺序匹配?

发布于 2024-12-29 02:52:51 字数 246 浏览 1 评论 0原文

有没有办法对 Node 对象中的元素进行重新排序,使其与 xsd:sequence 顺序匹配?我已经阅读了 Node.normalize 的文档,但它似乎没有执行该功能。还有其他事情可以做吗?

我相信可以使用 XSLT 来完成,但使用 XSLT 和一个特殊的 java-xlst 解析器来执行操作听起来像是一个过于复杂的解决方案,而不是使用 java 中的本机东西。

或者我实际上需要编写一个复杂的函数来为我进行处理吗?

Is there any way to reorder elements in a Node objects such that they match an xsd:sequence order? I've read through the docs for Node.normalize, but it doesn't seem to perform that function. Is there something else that can be done?

I believe that it can be done with XSLT, but it would sound like an overly convoluted solution to use XSLT, and a special java-xlst parser to perform the manipulation as opposed to having something native in java.

Or would I actually need to code a complex function to do the processing for me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾听心声的旋律 2025-01-05 02:52:51

如果我正确地阅读了您的问题,那么您想要的是一种根据外部 XSD 重新排列节点的“神奇”(简单)方式,而不是使用某些 XML API 移动节点的方式。

如果属实,那么我不知道有任何 API 提供了实现此类操作的通用方法。鉴于 XSD 能够描述的内容模型的复杂性,这也不是一个简单的编程。

在非常简单的情况下,它实际上也是不可能解决的。想想这个非常简单的例子:想象一个由两个选择组成的内容模型,i) 序列 a - b - c,ii) 序列 b - c - a。 XML 内容的顺序为 c - b - a(根据您的要求,您希望接受无效的 XML,并使其有效)。在这个例子中,哪种方法是正确的?

当然,我并不是说这就是你的情况。我只是想指出,据我所知,为什么没有人提出解决这一问题的方法。

如果序列下的内容模型相当简单,因此允许采用明确的方法来解决它,而不管 XML 到达的顺序如何,或者如果 XML 内容始终以已知但错误的方式出现,那么您必须使用您首选的 XML 操作方法对其进行编码 - 但我建议,不要尝试使其通用,这只会陷入兔子洞;)....

If I read your question correctly, what you want is rather a "magical" (as in a simple) way of getting nodes re-arranged according to an external XSD, as opposed to a way to move nodes around using some XML API.

If true, then I am not aware of any API that provides a generic way to achieve such a thing. It is also not a trivial piece of programming, given the complexity of the content models XSD is able to describe.

It is also, under very simple circumstances, virtually impossible to solve. Think of this very simple example: imagine a content model made up of two choices, i) sequence a - b - c , ii) sequence b - c - a. The XML content comes in the order c - b - a (as per your requirement, you want to take in invalid XML, and make it valid). In this example, which is the right way to do it?

Of course, I am not saying this is your scenario. I am simply trying to point out why no one, as far as I know, has come up with a solution to such a problem.

If your content model under the sequence is rather trivial, thus allowing for an unambiguous way of solving it irrespective of the sequence in which your XML arrives, or if the XML content comes in a known, but wrong way, all the time, then you have to code it using your preferred approach to XML manipulation - but I would suggest, don't try to make it generic, it is just going down the rabbit hole ;)....

甜嗑 2025-01-05 02:52:51

如果您只需要担心 xs:sequence,而不是选择/重复,并且只要架构不是为了使用大量复杂的功能(例如命名模型组、替换组和通配符)而编写的,那么它就不应该太重要无论如何,如果您使用 XSLT 2.0 或 XQuery 并且忘记尝试用 Java 对其进行编码,那么无论如何都会很困难。如果变量 $seq 是 xs:sequence 元素,则名为 $E 的元素的排序键为“count($x/xs:element[(@name|@ref)=$E]/preceding-sibling:: *”,因此您可以将此表达式插入到 xsl:sort 中,因为

<xsl:for-each select="*">
  <xsl:sort select="count($seq/xs:element[(@name|@ref)=local-name(current())]/preceding-sibling::*"/>
  ...
</xsl:for-each>

可能需要对命名空间或您可能遇到的其他事情进行一些技巧,但您明白了。

If you only have xs:sequence to worry about, and not choice/repetition, and so long as the schema isn't written to use lots of complex features like named model groups and substitution groups and wildcards, then it shouldn't be too difficult, at any rate if you use XSLT 2.0 or XQuery and forget about trying to code it in Java. If the variable $seq is an xs:sequence element, then the sort key for an element named $E is "count($x/xs:element[(@name|@ref)=$E]/preceding-sibling::*", so you can just plug this expression into an xsl:sort as

<xsl:for-each select="*">
  <xsl:sort select="count($seq/xs:element[(@name|@ref)=local-name(current())]/preceding-sibling::*"/>
  ...
</xsl:for-each>

Probably needs a bit of finessing for namespaces or other things you might encounter, but you get the idea.

风启觞 2025-01-05 02:52:51

您可以使用以下包直接在 Java 中操作 XML:

import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;

您需要放置一个 XML 解析器,例如 saxon(请参阅 http: //saxon.sourceforge.net/ )在类路径上

您可以“手动”操作 XML,读取输入文档的位并创建所需的输出文档,或者您可以编写 xslt 样式表并使用它来直接转换文档从你的Java代码。

尝试从这里开始:

http://oreilly.com/catalog/javaxslt/chapter/ ch05.html

You can manipulate XML directly in Java using the following packages:

import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;

You'll need to put an XML parser such as saxon ( see http://saxon.sourceforge.net/ ) on your classpath

You can manipulate the XML 'by hand', reading bits of the input document and creating the required output document or you can write an xslt style sheet and use it to transform the document straight from your Java code.

Try here for a starting point:

http://oreilly.com/catalog/javaxslt/chapter/ch05.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文