XMLUNIT:NestedText的自定义元素选择器byNameAndText

时间:2018-09-17 16:10:10

标签: java xmlunit-2

我正在比较2种XML。而且,即使 book:name 文本值被 bookorg:entity 包围,我也想忽略这种差异。我使用了ElementSelectors.byNameAndText。但是我想我必须使用ElementSelectors.InnerNameAndText之类的东西来定制这个ElementSelector。现在 byNameAndText 使用此方法 getMergedNestedText 。但这默认情况下不起作用。

public static String getMergedNestedText(Node n)
    /*     */ {
        /*  59 */ StringBuilder sb = new StringBuilder();
        /*  60 */ for (Node child : new IterableNodeList(n.getChildNodes())) {
            /*  61 */ if ((child instanceof Text) || (child instanceof CDATASection)) {
                /*  62 */ String s = child.getNodeValue();
                /*  63 */ if (s != null) {
                    /*  64 */ sb.append(s);
                    /*     */ }
                /*     */ }
            /*     */ }
        /*  68 */ return sb.toString();
        /*     */ }

控制

            <book:book typeValue="cr">
                <book:name>
                    <bookorg:entity role="urn:contentType:organization">
                        <bookorg:content>Harry Potter</bookorg:content>
                        <bookorg:entityReference role="urn:contentType:organization" guid:guid="urn:entityauthority:o">
                        </bookorg:entityReference>
                    </bookorg:entity>
                </book:name>
                <book:typeCode>C</book:typeCode>
                <book:typeDesc>cr</book:typeDesc>
                <book:supplement>
                    <book:cleanName>Harry Potter</book:cleanName>
                    <book:cleanTimestamp day="17" month="09" year="2012">2012-09-17T06:49:00</book:cleanTimestamp>
                </book:supplement>
            </book:book>

测试

<book:book typeValue="cr">
                    <book:name>Harry Potter</book:name>
                    <book:typeCode>C</book:typeCode>
                    <book:typeDesc>cr</book:typeDesc>
                    <book:supplement>
                        <book:cleanName>Harry Potter</book:cleanName>
                        <book:cleanTimestamp day="17" month="09" year="2012">2012-09-17T06:49:00</book:cleanTimestamp>
                    </book:supplement>
                </book:book>    

代码

    QName bookQName = new QName("http://xyz", "book", "book");
    Map<String, String> mapBookQName = new HashMap<String, String>();
    mapBookQName.put("book", "http://xyz");


    ElementSelector bookNameDescSelector = ElementSelectors.byXPath("./book:name", mapBookQName, ElementSelectors.byNameAndText);   

    ElementSelector esNS = ElementSelectors.conditionalBuilder()
            .whenElementIsNamed(bookQName)
            .thenUse(bookNameDescSelector)
            .elseUse(ElementSelectors.byName)
            .build();   

请提出任何建议

0 个答案:

没有答案