Menu

Excluding certain attributes possible?

2003-06-18
2004-03-03
  • Ritesh Trivedi

    Ritesh Trivedi - 2003-06-18

    Hi,

    I wanted to know if while comparing 2 elements if its possible to exclude 1 or more attributes?

    If yes, can anyone show how?

    Thanks.
    Rit

     
    • Jeff Martin

      Jeff Martin - 2003-06-19

      You could probably do this by passing the test document through an XSL transform which strips of the attributes you don't want to be included in your test.

       
      • Sebastian

        Sebastian - 2004-02-09

        < You could probably do this by passing the test document through an XSL transform which strips of the attributes you don't want to be included in your test.

        Hi Jeff, I am in need to do this also. Wouldn't it be better (looking at your documentation) to use something like IgnoreTextAndAttributeValuesDifferenceListener?

        Now I am not understanding your example. How does this DifferenceListener look like? Where is the information which describes the excluded elements and attributes?

        Please help me with this, thanks in advance, Sebastian

         
    • Ritesh Trivedi

      Ritesh Trivedi - 2003-06-19

      How can I do this simultaneously while the test document is being processed?

      I am new to XMLUnit so please provide little more details than a single statement. The PDF document is kind of cryptic, it takes quiet some time to figure out XMLUnit

       
    • tim bacon

      tim bacon - 2004-02-10

      You could indeed use a Custom DifferenceListener.

      e.g. class Blah implements DifferenceListener {...}

      In differenceFound() the Difference param contains all the information about the nodes which are different:

      if DifferenceConstants.ATTR_VALUE.equals(difference) and difference.getControlNodeDetail().getNode() is the kind of attribute you wish to ignore then
                  return RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR;
              else
                  return RETURN_ACCEPT_DIFFERENCE;

      Hope this helps,

      Tim

       
      • Sebastian

        Sebastian - 2004-02-12

        thanks for your explanation tim,
        now i understand the example.

        > Where is the information which describes the excluded elements and attributes?

        looking for a description for excluding elements and attributes, i think using xpath expressions would be the right way. the underlying implementation could be following your explanation.

        wouldn't it be nicer to enhance the assert methods with these excluding xpath expressions?

         
    • Sebastian

      Sebastian - 2004-02-17

      I am trying this right now.

      I have the diffNode in the DifferenceListener and the NodeList from the xpath.
      How can I check if the diffNode is in this NodeList?
      Please help me with this.
      I can email the classes, I have stripped for this.

      ---------------------------------------------------

      Node diffNode = difference.getControlNodeDetail().getNode();

      SimpleXpathEngine engine = new SimpleXpathEngine();
      NodeList nodes = engine.getMatchingNodes(xpath, expected);
      for (int i = 0; i < nodes.getLength(); i++)
      {
          Node xpathNode = nodes.item(i);
          boolean isEqual = diffNode.equals(xpathNode);    // HOW CAN I CHECK THIS???
          if (isEqual) return true;
      }
      return false;

       
      • Sebastian

        Sebastian - 2004-02-25

        please! help me with this one

         
        • tim bacon

          tim bacon - 2004-02-26

          Rather than use an XPath to identify the attributes to include / exlcude I would suggest that you write an XSL transformation that strips out what you wish to exclude. Then compare the post-XSLT-control-XML to the post-XSLT-test-XML.

          Hope this helps,

          Tim

          PS: XMLUnit uses XSLT to implement XPath traversal under the covers, as XPath is not part of the JAX standard. If you really need XPath functionality I suggest you look at something like Jaxen, or go directly to the underlying XML libraries (e.g. Xalan / Xerces).

           
    • Sebastian

      Sebastian - 2004-02-27

      Hi Tim,
      thank you for your reply.

      Excluding should also be done with elements.

      > I would suggest that you write an XSL transformation that strips out what you wish to exclude. Then compare the post-XSLT-control-XML to the post-XSLT-test-XML.

      well, I am not sure I can handle this.

      > PS: XMLUnit uses XSLT to implement XPath traversal under the covers, as XPath is not part of the JAX standard.

      Why can't I use your xpath result? How can I see if a diff-node is part of an xpath-result? As I see in localName and name there is xpathResult. If there would be the attribute or element name, it could be done.

      As you see in the forums, excluding attributes or elements are often asked for. Could you do this with a minimal effort?

      > If you really need XPath functionality I suggest you look at something like Jaxen, or go directly to the underlying XML libraries (e.g. Xalan / Xerces).

      Yes, I would look here deeper. I think Xalan, could have some functionality. I hope I can match the result with your diff-nodes.

      Anyway, if I am able to come to a solution, I will give it back to you. So you can put in a next release. As I said, there is a common interest for this feature.

      Greetings, Sebastian

       
    • Sebastian

      Sebastian - 2004-03-03

      Dear Tim,

      with Xalan, I was able to retrieve a node from xpath. I used org.apache.xpath.XPathAPI. Now I am testing (with JUnit tests) and enhancing the solution. How can I publish my code so far, that we can discuss it?

      Greetings, Sebastian

       

Log in to post a comment.