• Resolved taro0904

    (@taro0904)


    How can I output the nested field <auszeichnung> in the middle of the text…? :

    My xml file looks like this:
    <inhalt>text bla bla bla bla bla <auszeichnung format=”fett”>Langboot</auszeichnung>. more bla bla bla </inhalt>

    [FOREACH({verlauf[1]/reisetag})]
    {inhalt[1] }
    [ENDFOREACH]

    Thank you in advance

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author WP All Import

    (@wpallimport)

    Hi @taro0904

    The only way this would be possible is by using our API to create your own XPath element. Here’s an example snippet for this:

    add_filter( 'wpallimport_xml_row', 'wpai_wpallimport_xml_row', 10, 1 );
    
    function wpai_wpallimport_xml_row( $node ){
        $elements = $node->xpath( '//reisetag[1]' ); 
        if ( !empty( $elements[ 0 ] ) ) $node->addChild( 'alltext', $elements[ 0 ]->asXML() );
        return $node;
    }

    Once you’ve added that, you can then use the following element to return ‘text bla bla bla bla bla Langboot. more bla bla bla’:

    {alltext[1]}

    Thread Starter taro0904

    (@taro0904)

    Thank you for your answer, but therre are some mor questions…

    Ive copied the snipped in my functions.php. Do I have to add or change anything else in the snipped ? The root is much longer than in my example..
    {data[1]/journeys[1]/journey[1]/description[1]/reise[1]/verlauf[1]/reisetag[1]/inhalt[1]}

    An how should I create the output ?
    Ive tried several variations like this, but nothing is working…

    
    [FOREACH({data[1]/journeys[1]/journey[1]/description[1]/reise[1]/verlauf[1]/reisetag})]
    [wpai_wpallimport_xml_row({inhalt[1]},{inhalt[1]/auszeichnung},{alltext[1]})]
    [ENDFOREACH]

    Best regards
    taro

    Plugin Author WP All Import

    (@wpallimport)

    Hi @taro0904

    You would need to adjust our example snippet to run the correct XPath query for this. The $node variable in our code is a SimpleXMLElement resource, you can read more about that here: https://php.net/manual/en/class.simplexmlelement.php

    Thread Starter taro0904

    (@taro0904)

    Sorry, but I still have problems..

    Because its an attribute which has to be added as a child, maybe there exists a function to add attributes in a foreach loop?? Would it be different with atributes?

    ...
    <verlauf>
    <reisetag>
    <inhalt>
    
        The sun is 
    
      <auszeichnung format=”bold”>  very hot   </auszeichnung>
        
       in the summer
    
    </inhalt>
    </reisetag>
    </verlauf>
    ...

    Output should be: “The sun is very hot in the summer”

    The whole xpath…
    {stml_data[1]/srml_journeys[1]/srml_journey[1]/srml_description[1]/reise[1]/verlauf[1]/reisetag[5]/inhalt[1]/auszeichnung[1]}

    And…Because I have some more field in my FOREACH loop how do I have to write the template?

    [FOREACH({data[1]/journeys[1]/journey[1]/description[1]/reise[1]/verlauf[1]/reisetag })] 
    {@order}. Day:{titel[1]}
    [wpai_wpallimport_xml_row({alltext[1]})]
    {meals[1]}
    [ENDFOREACH]
    Plugin Author WP All Import

    (@wpallimport)

    Hi @taro0904

    Very sorry for the delay on this.

    The code we provided before would need to be adjusted to achieve the results in your explanation. Accessing individual elements and attributes can all be done with SimpleXML: https://php.net/manual/en/class.simplexmlelement.php. Unfortunately, we don’t have any example snippets for this particular use case.

    Keep in mind also that our code creates a single element that you can use in the import template: {alltext}. The FOREACH loop could be used on this element to get all of the results.

    So, for example, if you had a file in this format – https://paste.ee/p/J30O2 – and you were importing the “node” element, you could re-write the function like so:

    add_filter( 'wpallimport_xml_row', 'wpai_wpallimport_xml_row', 10, 1 );
    
    function wpai_wpallimport_xml_row( $node ){
        $elements = $node->xpath( '//data/journeys/journey/description/reise/verlauf/reisetag' ); 
    	if ( !empty( $elements ) ) {
    		foreach ( $elements as $element ) {
    			$node->addChild('alltext', $element->asXML() );	
    		}
    	}
        return $node;
    }

    And, you could out put both “The sun is very hot in the summer” and “The snow is very cold in the winter” with:

    [FOREACH({alltext})]
    {.}
    [ENDFOREACH]

    Or, you could access each one individually with {alltext[1]} and {alltext[2]}.

    • This reply was modified 7 years, 10 months ago by WP All Import.
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘nested xml fields’ is closed to new replies.