将 XML 导入 JCR 存储库 - 多值属性

发布于 2024-11-14 23:52:44 字数 1314 浏览 3 评论 0原文

我正在使用 JCR 存储库(特别是 Jackrabbit)来存储我当前项目的数据。一切都很顺利,尽管不幸的是有时文档有点难找到。我现在正在努力解决的是为某些测试设置单元测试数据。我已经运行了一个内存中的 Jackrabbit,并且正在将 Nodetypes.cnd 和 data.xml 文件正确导入其中,以便我有种子数据可供测试。然而,我无法弄清楚如何从 data.xml 文件导入,其中节点具有多个同名属性。

具体来说,由于数据的构造方式,我有一个链接到其他规则节点的规则节点 - 其中一个规则实际上根据其他规则起作用。我通过在规则节点上设置一个属性“rule (PATH) multiple”来实现这一点,因此只是链接的规则节点的节点路径列表。我可以轻松地在我的 xml 中添加一个链接,

<rule1 name="Rule #1" description="This is Rule number 1" rule="/rules/rule2" />

方法是正确创建一个名为“rule1”的规则,该规则具有指向“rule2”的链接。我希望能够使用 session.importXML() 方法创建此规则,其中它有多个链接...我已经尝试过了

<rule1 name="Rule #1" description="This is Rule number 1" rule="/rules/rule2,/rules/rule3" />
<rule1 name="Rule #1" description="This is Rule number 1" rule="/rules/rule2" rule="/rules/rule3" />
<rule1 Rule #1" description="This is Rule number 1">
    <@rule>/rules/rule2</@rule>
    <@rule>/rules/rule3</@rule>
</rule1>

,但这些都不起作用。事实上 - 很明显 - 第二个和第三个甚至不解析为 XML。

有办法做到这一点吗?或者我是否需要更改我的导入流程以使其在此处以不同方式工作?

导入实际上是用所以完成的,

    InputStream xml = getClass().getResourceAsStream("/jcr/data.xml");
    session.importXML(session.getRootNode().getPath(), xml, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);

所以绝对没有什么特别的。

I'm playing with a JCR repository - Jackrabbit specifically - for storing data for my current project. Everything is going fine, though documentation is unfortunately a bit hard to find at times. What I'm struggling with right now is setting up unit-test data for some tests. I've gotten an in-memory Jackrabbit running, and am importing my Nodetypes.cnd and data.xml files into it correctly so that I have seed data to test against. I can't however work out how to import from the data.xml file where the node has multiple properties with the same name.

Specifically, because of the way the data is structured I have a Rules node that links to other Rules nodes - where one rule actually works in terms of other rules. I've implemented this by having a property on the Rules node that is "rule (PATH) multiple", so simply a list of the node paths to the rules nodes that are linked. I can easily add one link in my xml by writing

<rule1 name="Rule #1" description="This is Rule number 1" rule="/rules/rule2" />

which correctly creates a rule called "rule1" that has a link to "rule2". What I want to to be able to create this rule using the session.importXML() method where it has more than one link... I've tried

<rule1 name="Rule #1" description="This is Rule number 1" rule="/rules/rule2,/rules/rule3" />
<rule1 name="Rule #1" description="This is Rule number 1" rule="/rules/rule2" rule="/rules/rule3" />
<rule1 Rule #1" description="This is Rule number 1">
    <@rule>/rules/rule2</@rule>
    <@rule>/rules/rule3</@rule>
</rule1>

And none of these have worked. In fact - obviously enough - the second and third ones don't even parse as XML.

Is there a way to do this? Or do I need to change my import process to work differently here?

The import is literally done with

    InputStream xml = getClass().getResourceAsStream("/jcr/data.xml");
    session.importXML(session.getRootNode().getPath(), xml, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);

So absolutely nothing special about that at all.

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

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

发布评论

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

评论(1

北方的韩爷 2024-11-21 23:52:44

我假设“data.xml”是您想要导入到存储库的文档视图 XML。
Jackrabbit 没有定义如何从存储库的文档视图导入/导出具有多个值的多值属性。
JCR 2.0规范看来,多值的解释属性取决于实现。
但是 Jackrabbit 确实支持从存储库的系统视图导入多值属性。出口也一样。

您可以使用 Session.exportSystemView() API 下载或导出存储库的系统视图,或者为您的测试存储库手动创建一个系统视图。

这是一个例子。注意 - “汽车:发动机(字符串)多个”。

<car='http://www.modeshape.org/examples/cars/1.0'>
[car:Car] > nt:unstructured, mix:created, mix:referenceable
  - car:maker (string)
  - car:model (string)
  - car:year (string) < '(19|20)[0-9]{2}'  // any 4 digit number starting with '19' or '20'
  - car:msrp (string) < '[$][0-9]{1,3}[,]?[0-9]{3}([.][0-9]{2})?'   // of the form "$X,XXX.ZZ", "$XX,XXX.ZZ" or "$XXX,XXX.ZZ"
                                                       // where '.ZZ' is optional
  - car:userRating (long) < '[1,5]'                        // any value from 1 to 5 (inclusive)
  - car:valueRating (long) < '[1,5]'                       // any value from 1 to 5 (inclusive)
  - car:mpgCity (long) < '(0,]'                            // any value greater than 0
  - car:mpgHighway (long) < '(0,]'                         // any value greater than 0
  - car:lengthInInches (double) < '(0,]'                   // any value greater than 0
  - car:wheelbaseInInches (double) < '(0,]'                // any value greater than 0
  - car:alternateModels (reference)  < 'car:Car'
  - car:engines (string) multiple

以下是包含上述 CND 的存储库的系统视图 XML 示例。
请注意,多值属性“car:engines”表示为列表元素。

<sv:property sv:name="car:engines" sv:type="String" sv:multiple="true">
    <sv:value>1.8L</sv:value>
    <sv:value>1.5L</sv:value>
</sv:property>

可以像您一样使用 Session.importXML() API 将此系统视图 XML 导入到另一个存储库。

<?xml version="1.0" encoding="UTF-8"?>
<sv:node sv:name="Hybrid" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:fn_old="http://www.w3.org/2004/10/xpath-functions"  xmlns:sv="http://www.jcp.org/jcr/sv/1.0" xmlns:car="http://www.modeshape.org/examples/cars/1.0" xmlns:rep="internal" xmlns:nt="http://www.jcp.org/jcr/nt/1.0">
    <sv:property sv:name="jcr:primaryType" sv:type="Name">
        <sv:value>nt:unstructured</sv:value>
    </sv:property>
    <sv:property sv:name="jcr:mixinTypes" sv:type="Name" sv:multiple="true">
        <sv:value>mix:referenceable</sv:value>
    </sv:property>
    <sv:property sv:name="jcr:uuid" sv:type="String">
        <sv:value>7e999653-e558-4131-8889-af1e16872f4d</sv:value>
    </sv:property>
    <sv:node sv:name="Toyota Prius">
        <sv:property sv:name="jcr:primaryType" sv:type="Name">
            <sv:value>car:Car</sv:value>
        </sv:property>
        <sv:property sv:name="jcr:uuid" sv:type="String">
            <sv:value>0111cbd0-8f9b-4588-8ded-d66907174bcd</sv:value>
        </sv:property>
        <sv:property sv:name="car:engines" sv:type="String" sv:multiple="true">
            <sv:value>1.8L</sv:value>
            <sv:value>1.5L</sv:value>
        </sv:property>
        <sv:property sv:name="car:maker" sv:type="String">
            <sv:value>Toyota</sv:value>
        </sv:property>
        <sv:property sv:name="car:model" sv:type="String">
            <sv:value>Prius</sv:value>
        </sv:property>
        <sv:property sv:name="car:mpgCity" sv:type="Long">
            <sv:value>48</sv:value>
        </sv:property>
        <sv:property sv:name="car:mpgHighway" sv:type="Long">
            <sv:value>45</sv:value>
        </sv:property>
        <sv:property sv:name="car:msrp" sv:type="String">
            <sv:value>$21,500</sv:value>
        </sv:property>
        <sv:property sv:name="car:userRating" sv:type="Long">
            <sv:value>4</sv:value>
        </sv:property>
        <sv:property sv:name="car:valueRating" sv:type="Long">
            <sv:value>5</sv:value>
        </sv:property>
        <sv:property sv:name="car:year" sv:type="String">
            <sv:value>2008</sv:value>
        </sv:property>
        <sv:property sv:name="jcr:created" sv:type="Date">
            <sv:value>2011-06-13T23:45:59.175-04:00</sv:value>
        </sv:property>
        <sv:property sv:name="jcr:createdBy" sv:type="String">
            <sv:value>admin</sv:value>
        </sv:property>
        <sv:node sv:name="carImage">
            <sv:property sv:name="jcr:primaryType" sv:type="Name">
                <sv:value>nt:file</sv:value>
            </sv:property>
            <sv:property sv:name="jcr:created" sv:type="Date">
                <sv:value>2011-06-13T23:45:59.181-04:00</sv:value>
            </sv:property>
            <sv:property sv:name="jcr:createdBy" sv:type="String">
                <sv:value>admin</sv:value>
            </sv:property>
            <sv:node sv:name="jcr:content">
                <sv:property sv:name="jcr:primaryType" sv:type="Name">
                    <sv:value>nt:resource</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:uuid" sv:type="String">
                    <sv:value>7de51be4-1466-414b-a3ff-d840095e61bf</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:data" sv:type="Binary">
                    <sv:value/>
                </sv:property>
                <sv:property sv:name="jcr:encoding" sv:type="String">
                    <sv:value>binary</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:lastModified" sv:type="Date">
                    <sv:value>2011-05-18T16:14:30.000-04:00</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:lastModifiedBy" sv:type="String">
                    <sv:value>admin</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:mimeType" sv:type="String">
                    <sv:value>image/jpeg</sv:value>
                </sv:property>
            </sv:node>
        </sv:node>
        <sv:node sv:name="carImage">
            <sv:property sv:name="jcr:primaryType" sv:type="Name">
                <sv:value>nt:file</sv:value>
            </sv:property>
            <sv:property sv:name="jcr:created" sv:type="Date">
                <sv:value>2011-06-13T23:45:59.322-04:00</sv:value>
            </sv:property>
            <sv:property sv:name="jcr:createdBy" sv:type="String">
                <sv:value>admin</sv:value>
            </sv:property>
            <sv:node sv:name="jcr:content">
                <sv:property sv:name="jcr:primaryType" sv:type="Name">
                    <sv:value>nt:resource</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:uuid" sv:type="String">
                    <sv:value>6ad955e2-6e75-4833-904c-6ee3bf7c18b6</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:data" sv:type="Binary">
                    <sv:value/>
                </sv:property>
                <sv:property sv:name="jcr:encoding" sv:type="String">
                    <sv:value>binary</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:lastModified" sv:type="Date">
                    <sv:value>2011-05-18T16:14:30.000-04:00</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:lastModifiedBy" sv:type="String">
                    <sv:value>admin</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:mimeType" sv:type="String">
                    <sv:value>image/jpeg</sv:value>
                </sv:property>
            </sv:node>
        </sv:node>
    </sv:node>
    <sv:node sv:name="Toyota Highlander">
        <sv:property sv:name="jcr:primaryType" sv:type="Name">
            <sv:value>car:Car</sv:value>
        </sv:property>
        <sv:property sv:name="jcr:uuid" sv:type="String">
            <sv:value>99b2afd4-74e8-4a45-a6d2-623bfc66387a</sv:value>
        </sv:property>
        <sv:property sv:name="car:maker" sv:type="String">
            <sv:value>Toyota</sv:value>
        </sv:property>
        <sv:property sv:name="car:model" sv:type="String">
            <sv:value>Highlander</sv:value>
        </sv:property>
        <sv:property sv:name="car:mpgCity" sv:type="Long">
            <sv:value>27</sv:value>
        </sv:property>
        <sv:property sv:name="car:mpgHighway" sv:type="Long">
            <sv:value>25</sv:value>
        </sv:property>
        <sv:property sv:name="car:msrp" sv:type="String">
            <sv:value>$34,200</sv:value>
        </sv:property>
        <sv:property sv:name="car:userRating" sv:type="Long">
            <sv:value>4</sv:value>
        </sv:property>
        <sv:property sv:name="car:valueRating" sv:type="Long">
            <sv:value>5</sv:value>
        </sv:property>
        <sv:property sv:name="car:year" sv:type="String">
            <sv:value>2008</sv:value>
        </sv:property>
        <sv:property sv:name="jcr:created" sv:type="Date">
            <sv:value>2011-06-13T23:45:59.191-04:00</sv:value>
        </sv:property>
        <sv:property sv:name="jcr:createdBy" sv:type="String">
            <sv:value>admin</sv:value>
        </sv:property>
    </sv:node>
</sv:node>

I am assuming the "data.xml" is a document-view XML that you want to import to the repository.
Jackrabbit does not define how to import/export a multi-valued property with multiple values from/to a document view of a repository.
It seems from the JCR 2.0 spec, the interpretation of a multi-valued property is dependent on the implementation.
However Jackrabbit does support the import of multi-valued properties from a system-view of a repository. Same for export.

You can download or export the system-view of the repository using Session.exportSystemView() API or manually create one for your test repository.

Here is an example. Note - "car:engines (string) multiple".

<car='http://www.modeshape.org/examples/cars/1.0'>
[car:Car] > nt:unstructured, mix:created, mix:referenceable
  - car:maker (string)
  - car:model (string)
  - car:year (string) < '(19|20)[0-9]{2}'  // any 4 digit number starting with '19' or '20'
  - car:msrp (string) < '[$][0-9]{1,3}[,]?[0-9]{3}([.][0-9]{2})?'   // of the form "$X,XXX.ZZ", "$XX,XXX.ZZ" or "$XXX,XXX.ZZ"
                                                       // where '.ZZ' is optional
  - car:userRating (long) < '[1,5]'                        // any value from 1 to 5 (inclusive)
  - car:valueRating (long) < '[1,5]'                       // any value from 1 to 5 (inclusive)
  - car:mpgCity (long) < '(0,]'                            // any value greater than 0
  - car:mpgHighway (long) < '(0,]'                         // any value greater than 0
  - car:lengthInInches (double) < '(0,]'                   // any value greater than 0
  - car:wheelbaseInInches (double) < '(0,]'                // any value greater than 0
  - car:alternateModels (reference)  < 'car:Car'
  - car:engines (string) multiple

Following is an example system-view XML of a repository containing the above CND.
Note the multi-valued property "car:engines" is represented as a list element.

<sv:property sv:name="car:engines" sv:type="String" sv:multiple="true">
    <sv:value>1.8L</sv:value>
    <sv:value>1.5L</sv:value>
</sv:property>

This system-view XML can be imported to another repository using Session.importXML() API as you are doing.

<?xml version="1.0" encoding="UTF-8"?>
<sv:node sv:name="Hybrid" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:fn_old="http://www.w3.org/2004/10/xpath-functions"  xmlns:sv="http://www.jcp.org/jcr/sv/1.0" xmlns:car="http://www.modeshape.org/examples/cars/1.0" xmlns:rep="internal" xmlns:nt="http://www.jcp.org/jcr/nt/1.0">
    <sv:property sv:name="jcr:primaryType" sv:type="Name">
        <sv:value>nt:unstructured</sv:value>
    </sv:property>
    <sv:property sv:name="jcr:mixinTypes" sv:type="Name" sv:multiple="true">
        <sv:value>mix:referenceable</sv:value>
    </sv:property>
    <sv:property sv:name="jcr:uuid" sv:type="String">
        <sv:value>7e999653-e558-4131-8889-af1e16872f4d</sv:value>
    </sv:property>
    <sv:node sv:name="Toyota Prius">
        <sv:property sv:name="jcr:primaryType" sv:type="Name">
            <sv:value>car:Car</sv:value>
        </sv:property>
        <sv:property sv:name="jcr:uuid" sv:type="String">
            <sv:value>0111cbd0-8f9b-4588-8ded-d66907174bcd</sv:value>
        </sv:property>
        <sv:property sv:name="car:engines" sv:type="String" sv:multiple="true">
            <sv:value>1.8L</sv:value>
            <sv:value>1.5L</sv:value>
        </sv:property>
        <sv:property sv:name="car:maker" sv:type="String">
            <sv:value>Toyota</sv:value>
        </sv:property>
        <sv:property sv:name="car:model" sv:type="String">
            <sv:value>Prius</sv:value>
        </sv:property>
        <sv:property sv:name="car:mpgCity" sv:type="Long">
            <sv:value>48</sv:value>
        </sv:property>
        <sv:property sv:name="car:mpgHighway" sv:type="Long">
            <sv:value>45</sv:value>
        </sv:property>
        <sv:property sv:name="car:msrp" sv:type="String">
            <sv:value>$21,500</sv:value>
        </sv:property>
        <sv:property sv:name="car:userRating" sv:type="Long">
            <sv:value>4</sv:value>
        </sv:property>
        <sv:property sv:name="car:valueRating" sv:type="Long">
            <sv:value>5</sv:value>
        </sv:property>
        <sv:property sv:name="car:year" sv:type="String">
            <sv:value>2008</sv:value>
        </sv:property>
        <sv:property sv:name="jcr:created" sv:type="Date">
            <sv:value>2011-06-13T23:45:59.175-04:00</sv:value>
        </sv:property>
        <sv:property sv:name="jcr:createdBy" sv:type="String">
            <sv:value>admin</sv:value>
        </sv:property>
        <sv:node sv:name="carImage">
            <sv:property sv:name="jcr:primaryType" sv:type="Name">
                <sv:value>nt:file</sv:value>
            </sv:property>
            <sv:property sv:name="jcr:created" sv:type="Date">
                <sv:value>2011-06-13T23:45:59.181-04:00</sv:value>
            </sv:property>
            <sv:property sv:name="jcr:createdBy" sv:type="String">
                <sv:value>admin</sv:value>
            </sv:property>
            <sv:node sv:name="jcr:content">
                <sv:property sv:name="jcr:primaryType" sv:type="Name">
                    <sv:value>nt:resource</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:uuid" sv:type="String">
                    <sv:value>7de51be4-1466-414b-a3ff-d840095e61bf</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:data" sv:type="Binary">
                    <sv:value/>
                </sv:property>
                <sv:property sv:name="jcr:encoding" sv:type="String">
                    <sv:value>binary</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:lastModified" sv:type="Date">
                    <sv:value>2011-05-18T16:14:30.000-04:00</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:lastModifiedBy" sv:type="String">
                    <sv:value>admin</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:mimeType" sv:type="String">
                    <sv:value>image/jpeg</sv:value>
                </sv:property>
            </sv:node>
        </sv:node>
        <sv:node sv:name="carImage">
            <sv:property sv:name="jcr:primaryType" sv:type="Name">
                <sv:value>nt:file</sv:value>
            </sv:property>
            <sv:property sv:name="jcr:created" sv:type="Date">
                <sv:value>2011-06-13T23:45:59.322-04:00</sv:value>
            </sv:property>
            <sv:property sv:name="jcr:createdBy" sv:type="String">
                <sv:value>admin</sv:value>
            </sv:property>
            <sv:node sv:name="jcr:content">
                <sv:property sv:name="jcr:primaryType" sv:type="Name">
                    <sv:value>nt:resource</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:uuid" sv:type="String">
                    <sv:value>6ad955e2-6e75-4833-904c-6ee3bf7c18b6</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:data" sv:type="Binary">
                    <sv:value/>
                </sv:property>
                <sv:property sv:name="jcr:encoding" sv:type="String">
                    <sv:value>binary</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:lastModified" sv:type="Date">
                    <sv:value>2011-05-18T16:14:30.000-04:00</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:lastModifiedBy" sv:type="String">
                    <sv:value>admin</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:mimeType" sv:type="String">
                    <sv:value>image/jpeg</sv:value>
                </sv:property>
            </sv:node>
        </sv:node>
    </sv:node>
    <sv:node sv:name="Toyota Highlander">
        <sv:property sv:name="jcr:primaryType" sv:type="Name">
            <sv:value>car:Car</sv:value>
        </sv:property>
        <sv:property sv:name="jcr:uuid" sv:type="String">
            <sv:value>99b2afd4-74e8-4a45-a6d2-623bfc66387a</sv:value>
        </sv:property>
        <sv:property sv:name="car:maker" sv:type="String">
            <sv:value>Toyota</sv:value>
        </sv:property>
        <sv:property sv:name="car:model" sv:type="String">
            <sv:value>Highlander</sv:value>
        </sv:property>
        <sv:property sv:name="car:mpgCity" sv:type="Long">
            <sv:value>27</sv:value>
        </sv:property>
        <sv:property sv:name="car:mpgHighway" sv:type="Long">
            <sv:value>25</sv:value>
        </sv:property>
        <sv:property sv:name="car:msrp" sv:type="String">
            <sv:value>$34,200</sv:value>
        </sv:property>
        <sv:property sv:name="car:userRating" sv:type="Long">
            <sv:value>4</sv:value>
        </sv:property>
        <sv:property sv:name="car:valueRating" sv:type="Long">
            <sv:value>5</sv:value>
        </sv:property>
        <sv:property sv:name="car:year" sv:type="String">
            <sv:value>2008</sv:value>
        </sv:property>
        <sv:property sv:name="jcr:created" sv:type="Date">
            <sv:value>2011-06-13T23:45:59.191-04:00</sv:value>
        </sv:property>
        <sv:property sv:name="jcr:createdBy" sv:type="String">
            <sv:value>admin</sv:value>
        </sv:property>
    </sv:node>
</sv:node>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文