Yiannis
Yiannis

Reputation: 1

How to select a value and map it to the attributes using xpath with wp all import

I want to import some products from an xml file. I am trying to map some attributes. The problem is I want to have specific attributes names in my site for example "color, size, diameter, led temperature" for one kind of products and some other for example "color, height, material" for an other kind of products

In the xml there are thousands of products and many many categories. There is for example one product that has 20 attributes and another product that has 10 attributes, some of them are common, but not in the same order. Also they have different names than the ones I use in my site

What I want is to take some specific that I need out of the 20 and map them to my attributes that as I said have a different name than in the xml … maybe when I use the name "material" in my site .. in the xml the use another word instead of the word "material"

Also some of the attributes have more than one value separated by comma I need to take these values as separate odjects

Here is the xml


<product>
<id>314</id>
<name>
<![CDATA[
InLight Κρεμαστό φωτιστικό από διάφανο γυαλί και κρύσταλλα 3XE14 D:55cm (5314-3-CLEAR)
]]>
</name>
<image>
<![CDATA[
https://www.moraitis.com/image/2019-2020/5314-3 DIAFANO.jpg
]]>
</image>
<additional_image>
<![CDATA[
https://www.moraitis.com/image/2019-2020/5314-3 GIALI SKITSO.jpg
]]>
</additional_image>
<code>5314-3-CLEAR</code>
<ean>314</ean>
<mpn>314</mpn>
<isbn/>
<rec_fee>0.3</rec_fee>
<price_without_vat>103.95</price_without_vat>
<price_with_vat>128.90</price_with_vat>
<manufacturer>
<![CDATA[ InLight ]]>
</manufacturer>
<url>
<![CDATA[
https://www.moraitis.com/Κρεμαστό φωτιστικό από διάφανο γυαλί και κρύσταλλα 3XE14 D:55cm (5314-3-CLEAR)
]]>
</url>
<url_non_seo>
<![CDATA[ https://www.moraitis.com/&path=209&product_id=314 ]]>
</url_non_seo>
<category>
<![CDATA[ Φωτιστικά ]]>
</category>
<category_id>
<![CDATA[ 209 ]]>
</category_id>
<category_id_path>
<![CDATA[ 209 ]]>
</category_id_path>
<cat_tree>
<cat>
<![CDATA[ Φωτιστικά ]]>
</cat>
<cat>
<![CDATA[ Φωτιστικά Οροφής ]]>
</cat>
<cat>
<![CDATA[ Κρεμαστά Φωτιστικά ]]>
</cat>
</cat_tree>
<weight>3.50</weight>
<weight_if_in_grams>4</weight_if_in_grams>
<weight_if_in_kg>3.50 kg</weight_if_in_kg>
<dimensions>
<length>42.00</length>
<width>36.00</width>
<height>23.00</height>
</dimensions>
<instock>Y</instock>
<availability>Παράδοση σε 4 - 10 ημέρες</availability>
<size/>
<shipping/>
<description>
<![CDATA[
Πρόκειται για ένα διάφανο γυάλινο κρεμαστό φωτιστικό με τρεις λαμπτήρες. Τόσο τα διάφανα κρύσταλλα που κρέμονται όσο και οι χρώμιο μεταλλικές λεπτομέρειες του προσδίδουν μια επιπλέον λάμψη. Η χρώμιο μεταλλική ανάρτηση είναι ρυθμιζόμενη και προσαρμόζεται στον κάθε χώρο.&nbsp;
]]>
</description>
<attributes>
<attr>
<name>
<![CDATA[ Dimmable ]]>
</name>
<text>
<![CDATA[ Ναι ]]>
</text>
</attr>
<attr>
<name>
<![CDATA[ Αριθμός Λαμπτήρων ]]>
</name>
<text>
<![CDATA[ 3 ]]>
</text>
</attr>
<attr>
<name>
Βαθμός Προστασίας
</name>
<text>
IP20
</text>
</attr>
<attr>
<name>
<![CDATA[ Λαμπτήρας ]]>
</name>
<text>
<![CDATA[ Ε14 ]]>
</text>
</attr>
<attr>
<name>
<![CDATA[ Με Λαμπτήρα ]]>
</name>
<text>
<![CDATA[ Όχι ]]>
</text>
</attr>
<attr>
<name>
<![CDATA[ Τοποθέτηση ]]>
</name>
<text>
<![CDATA[ Οροφή ]]>
</text>
</attr>
<attr>
<name>
<![CDATA[ Υλικό Κατασκευής ]]>
</name>
<text>
<![CDATA[ Γυαλί, Κρύσταλλα, Μέταλλο ]]>
</text>
</attr>
<attr>
<name>
<![CDATA[ Χρώμα Ανάρτησης ]]>
</name>
<text>
<![CDATA[ Χρώμιο ]]>
</text>
</attr>
<attr>
<name>
<![CDATA[ Χρώμα Γυαλιού ]]>
</name>
<text>
<![CDATA[ Διάφανο ]]>
</text>
</attr>
<attr>
<name>
<![CDATA[ Χρώμα Κρυστάλλου ]]>
</name>
<text>
<![CDATA[ Διάφανο ]]>
</text>
</attr>
</attributes>
</product>

What I ve tried so far are two things

  1. I used this xpath:
{(attr/text[../name/text()="Υλικό Κατασκευής"]/text())} 

but with no success, it returns empty

  1. then I used this function:
function my_get_attribute_value( $attr, $name, $value ) {
    if ( empty( $name ) || empty( $value ) ) return;
    
    $names = explode( ", ", $name[0] );
    $values = explode( ", ", $value[0] );
    
    foreach ( $names as $key => $v ) {
        if ( $v == $attr && array_key_exists( $key, $values ) ) {
            return $values[ $key ];
        }
    }
}

and call it with this xpath:

[my_get_attribute_value("Χρώμα Ανάρτησης",array({attributes/attr/name}),array({attributes/attr/text}))]

for each attribute I want to map I just change the name (in Greek). The problem with this is that when I have a value with comma separated text like "Γυαλί, Κρύσταλλα, Μέταλλο" it adds this to the array and when I call the attribute it shows only "Γυαλί" and the next attribute instead of showing it's value it shows "Κρύσταλλα"

Upvotes: 0

Views: 648

Answers (1)

Conal Tuohy
Conal Tuohy

Reputation: 3258

Note that the text node identified by the text() step in your XPath expressions starts and ends with a "new line" character. e.g.

<example>
test
</example>

The XPath expression /example/text()='test' will return false.

<example>test</example>

The XPath expression /example/text()='test' will return true.

You may like to use the normalize-space() function to strip the extra white space:

<example>
test
</example>

The XPath expression normalize-space(/example/text())='test' will return true.

Also, when you are comparing an element's content to a string value, you can omit the text() step and use an XPath like /example, which will implicitly convert the element to a string.

Upvotes: 0

Related Questions