php - How do I get all of the non excluded items in this list using an XPATH?

18

I have the following xml file:

<items>
    <item>
        <name>Suede Shoe</name>
        <category>Footwear</category>
        <price>£90.00</price>
        <tags>
            <footwear />
            <shoes />
        </tags>
    </item>
    <item>
        <name>Leather Shoe</name>
        <category>footwear</category>
        <price>£90.00</price>
        <tags>
            <footwear />
        </tags>
    </item>
    <item>
        <name>Canvas Shoe</name>
        <category>Footwear</category>
        <price>£90.00</price>
        <tags>
            <footwear exclude="true"/>
            <shoes/>
        </tags>
    </item>
    <item>
        <name>Toecap Shoe</name>
        <category>Footwear</category>
        <price>£90.00</price>
        <tags>
            <footwear exclude="true"/>
        </tags>
    </item>
</items>

I would like to select all of the items that contain one of my selected tags, but not bring back a result if the tag is excluded.

examples:

tags: footwear
returns: 1, 2

tags: shoes
returns 1, 3

tags: footwear|shoes
returns 1,2,3

Any help please?

Before including the 'exclude' requirement this xpath was working:

//tags[implode('|', $tags)]/parent::item

the tag "footwear" that is not excluded.

431

Answer

Solution:

for the footwear|shoes the XPath may look like

//tags[footwear[not( @exclude='true' )]|shoes[not( @exclude='true' )]]/parent::item

This will show 1,2,3

324

Answer

Solution:

This XPath 1.0 expression:

/items/item[
   tags/*[
      not(@exclude='true')
   ][
      contains(
         concat('|',$tags,'|'),
         concat('|',name(),'|')
      )
   ]
]

With $tags variable set as'footwear' string selects:

<item>
    <name>Suede Shoe</name>
    <category>Footwear</category>
    <price>£90.00</price>
    <tags>
        <footwear />
        <shoes />
    </tags>
</item>
<item>
    <name>Leather Shoe</name>
    <category>footwear</category>
    <price>£90.00</price>
    <tags>
        <footwear />
    </tags>
</item>

With $tags variable set as'shoes' string selects:

<item>
    <name>Suede Shoe</name>
    <category>Footwear</category>
    <price>£90.00</price>
    <tags>
        <footwear />
        <shoes />
    </tags>
</item>
<item>
    <name>Canvas Shoe</name>
    <category>Footwear</category>
    <price>£90.00</price>
    <tags>
        <footwear exclude="true" />
        <shoes />
    </tags>
</item>

With $tags variable set as'footwear|shoes' string selects:

<item>
    <name>Suede Shoe</name>
    <category>Footwear</category>
    <price>£90.00</price>
    <tags>
        <footwear />
        <shoes />
    </tags>
</item>
<item>
    <name>Leather Shoe</name>
    <category>footwear</category>
    <price>£90.00</price>
    <tags>
        <footwear />
    </tags>
</item>
<item>
    <name>Canvas Shoe</name>
    <category>Footwear</category>
    <price>£90.00</price>
    <tags>
        <footwear exclude="true" />
        <shoes />
    </tags>
</item>

People are also looking for solutions to the problem: PHP mod rewrite .htaccess file godaddy.com question

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.