4

I have searched here and I'm not able to find how to filter an xml based on their attribute. I have this xml:





     <?xml version="1.0" encoding="utf-8"?>
        <document>
            <document_head>
                <title>This is the title</title>
                <version>This is the title</version>
            </document_head>
            <document_body>
                <paragraph id="AXD">
                    <text>
                        This is a text that should be in the result
                    </text>
                    <properties>
                        <size>13px</size>
                        <color>#000000</color>
                    </properties>
                    <author>Current user</author>
                </paragraph>
                <paragraph id="SFI">
                    <properties>
                        <text>
                            This is some other text that should not be in there
                        </text>
                    </properties>
                </paragraph>
                <paragraph id="SFA">
                    <author>Some random guy</author>
                </paragraph>      
                <paragraph id="ARG">
                    This doesn't mean anything.
                </paragraph>
                <paragraph id="RRR">
                    This does, hence should be in there.
                </paragraph>
            </document_body>
        </document>


I expect this result:



    <?xml version="1.0" encoding="UTF-8"?>
    <document>
        <document_head>
            <title>This is the title</title>
            <version>This is the title</version>
        </document_head>
        <document_body>
            <paragraph id="AXD">
                <text>
                    This is a text that should be in the result
                </text>
                <properties>
                    <size>13px</size>
                    <color>#000000</color>
                </properties>
                <author>Current user</author>
            </paragraph>
            <paragraph id="RRR">
                This does, hence should be in there.
            </paragraph>      
        </document_body>
    </document>


Currently, I have this XSLT:



    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" indent="yes"/>  
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>

      <xsl:template match="document_body/paragraph[not(@id='AXD')][not(@id='RRR')]" />
    </xsl:stylesheet>


Which produces this XML:





     <?xml version="1.0" encoding="UTF-8"?>
        <document>
            <document_head>
                <title>This is the title</title>
                <version>This is the title</version>
            </document_head>
            <document_body>
                <paragraph id="AXD">
                    <text>
                        This is a text that should be in the result
                    </text>
                    <properties>
                        <size>13px</size>
                        <color>#000000</color>
                    </properties>
                    <author>Current user</author>
                </paragraph>      
            </document_body>
        </document>


Do you know what I am missing?

Thanks.

Update: It seems that the code works for another XSLT processor, but it doesn't for Java Transformer.

2
  • Are you sure it is not working? I just tried, and it did produce your expected output!
    – Tim C
    Jul 18, 2012 at 16:23
  • @tim-c Hi Tim. Yes, I tried it several times using Java, and nothing. It gives me the result that I posted. It seems that it is not checking for the second condition :(
    – Ondino
    Jul 18, 2012 at 16:34

1 Answer 1

7

I am sure your condition should work! However, here is couple of alternative ways to check, so give this a go instead to see if that makes a difference.

<xsl:template match="document_body/paragraph[not(@id='AXD' or @id='RRR')]"/>

<xsl:template match="document_body/paragraph[@id != 'AXD' and @id != 'RRR']"/>
3
  • It works! Thanks a lot. Side question: Is it possible to change that OR by AND for comparisons? I know it is kind of unrelated, but it saves me another question here.
    – Ondino
    Jul 18, 2012 at 16:46
  • I am not sure I fully understand, but I have edited by answer just in case. Perhaps another question would be best, there is no harm in that!
    – Tim C
    Jul 18, 2012 at 16:51
  • Sorry about that. It was about the operator itself. And yes, both of the answers work. Thanks a lot.
    – Ondino
    Jul 18, 2012 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.