Dom4j 规则与所有预期节点不匹配

发布于 2024-07-17 22:30:18 字数 1462 浏览 2 评论 0原文

如果预定义模式 //authorize 与以下 xml 片段中的元素匹配,我将使用 dom4j 的规则 api 来触发操作。

        <authorize role_required="admin">
        <node title="node which is visible only to admin" path="" >
            <authorize role_required="admin">
                <node title="node which is visible only to admin" path=""/>
            </authorize>
            <authorize role_required="admin">
                <node title="node which is visible only to admin" path="">
                    <authorize role_required="admin">
                    </authorize>
                        <node title="node which is visible only to admin" path=""/>
                </node>
            </authorize>
        </node>
    </authorize>
    <authorize role_deny="admin">
        <node title="Node which is not visible to admin" path=""/>
    </authorize>

不幸的是,它似乎不适用于嵌套元素,只能找到第一级的授权元素。 该操作仅触发两次,但有 5 个授权元素。 有人知道如何解决这个问题吗? 提前致谢。

我尝试将授权标签与以下规则进行匹配:

 Rule authorizationRule = new Rule();
 authorizationRule.setPattern( DocumentHelper.createPattern( "//authorize" ) );
 authorizationRule.setAction( new AuthorizationRule() );

this.stylesheet = new Stylesheet();

this.stylesheet.addRule(authorizationRule);
this.stylesheet.run(document);

该规则在第一层的元素上匹配两次。 我使用 document.selectNodes 方法交叉检查了 XPath 模式并获取了所有五个元素。

I'm using dom4j's rules api to fire an action if the predefined pattern //authorize matches an element in the following xml snippet.

        <authorize role_required="admin">
        <node title="node which is visible only to admin" path="" >
            <authorize role_required="admin">
                <node title="node which is visible only to admin" path=""/>
            </authorize>
            <authorize role_required="admin">
                <node title="node which is visible only to admin" path="">
                    <authorize role_required="admin">
                    </authorize>
                        <node title="node which is visible only to admin" path=""/>
                </node>
            </authorize>
        </node>
    </authorize>
    <authorize role_deny="admin">
        <node title="Node which is not visible to admin" path=""/>
    </authorize>

Unfortunately it seems that it doesn't work with nested elements, only the authorize elements on the first level are found. The action is triggered only two times, but there are 5 authorize elements.
Does anybody have an idea how to solve this problem?
Thanks in advance.

I've tried to match the authorize tag with the following rule:

 Rule authorizationRule = new Rule();
 authorizationRule.setPattern( DocumentHelper.createPattern( "//authorize" ) );
 authorizationRule.setAction( new AuthorizationRule() );

this.stylesheet = new Stylesheet();

this.stylesheet.addRule(authorizationRule);
this.stylesheet.run(document);

The rule matches two times on the elements on the fist level.
I cross checked the XPath pattern with the document.selectNodes method and got all five elements.

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

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

发布评论

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

评论(2

我恋#小黄人 2024-07-24 22:30:18

你的规则有这条线吗?

stylesheet.applyTemplates(node);

请记住,您的规则控制着更深层次元素的下降。

模式似乎不是用于选择元素,而是用于在遍历树时检查元素是否匹配。 如果元素与模式匹配,则会调用您的操作,但您有责任在子元素中继续。 如果不这样做,子元素将被跳过。

(免责声明:我的理解可能是错误的,我不使用dom4j,只是查看了

Do your rules have this line?

stylesheet.applyTemplates(node);

Remember that your rules control descent into deeper elements.

It seems that patterns are not used for selecting elements, but for checking that element matches when going through the tree. If element matches the pattern, your action is called, but it's your responsibility to continue in child elements. If you don't, child elements are skipped.

(Disclaimer: My understanding may be wrong, I don't use dom4j, and just looked at cookbook).

花桑 2024-07-24 22:30:18

我认为 Peter 解决了这个问题; 我只是在他的回答的基础上进行构建。

Thomas 的代码示例中的这一行有点令人困惑:

 authorizationRule.setAction( new AuthorizationRule() );

...除非 AuthorizationRule 是 Action,因为这就是 setAction 所采取的操作。

无论如何,使用以下代码,确实会为每五个“授权”元素调用操作的 run 方法:(

Rule authorizationRule = new Rule();
authorizationRule.setPattern(DocumentHelper.createPattern("//authorize"));

final Stylesheet stylesheet = new Stylesheet();                                 
authorizationRule.setAction(new Action(){
    public void run(Node node) throws Exception {
       stylesheet.applyTemplates(node);
    }
 });

stylesheet.addRule(authorizationRule);
stylesheet.run(document);

仅在将 XML 片段更改为格式良好的文档后才有效。)

您需要在操作中使用样式表的方式似乎有点尴尬,而且我不确定这种事情在 dom4j 中应该如何完成。 遗憾的是相关类如 Stylesheet 和 规则 似乎记录相当不充分。 (考虑例如方法 run(Node node, String mode) 其模式参数似乎完全缺乏解释。)

I think Peter nailed the issue; I'm just building on top of his answer.

This line in the code example by Thomas was a little confusing:

 authorizationRule.setAction( new AuthorizationRule() );

... unless AuthorizationRule is a custom implementation of Action, because that's what setAction takes.

Anyway, with the following code the action's run method indeed gets called for each five "authorize" elements:

Rule authorizationRule = new Rule();
authorizationRule.setPattern(DocumentHelper.createPattern("//authorize"));

final Stylesheet stylesheet = new Stylesheet();                                 
authorizationRule.setAction(new Action(){
    public void run(Node node) throws Exception {
       stylesheet.applyTemplates(node);
    }
 });

stylesheet.addRule(authorizationRule);
stylesheet.run(document);

(Only works after changing the XML snippet to well-formed document.)

The way you need to use the stylesheet inside the action seems a little awkward, and I'm not sure how this kind of thing is supposed to be done in dom4j. It's a shame that relevant classes like Stylesheet and Rule seem to be rather inadequately documented. (Consider e.g. the method run(Node node, String mode) whose mode parameter seems to lack explanation entirely.)

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