Hi.
I've an XML file to import on my drupal site, and I'm using feeds, with xpath parser.
The structure of my xml is like this:

<mysite>
  <song>
    <title>Song title</title>
    <terms>
       <term id="6">First term</term>
       <term id="8">Second term</term>
    </terms>
  </song>
</mysite>

The context of xpath is //song, and "title" is imported without problems.
In my site there is a "terms" taxonomy, and on my content type I've added a term reference field (multiple select).to this taxonomy.
The xpath query for terms is "terms/term"
But it doesn't work, terms are always empty.

Why?

Thanks,
Sergej

Comments

jeroen87’s picture

subscribe

aubjr_drupal’s picture

subscribe

aubjr_drupal’s picture

This is assuming that you want to 1) assign multiple taxonomy terms to the newly-created nodes upon the pull, and 2) add terms to the vocabulary if they aren't already into the vocabulary, right?

The problem may be that the import is trying to put both terms into one tax term, which leaves you with a blank entry. I was trying to pull in 5 nodes from http://example-source.com/feed_1 with 8-10 terms each assigned to each node (including dupes), but I was getting a "created 5 terms" debug message (one for each node) and 5 new terms with blank names in the destination site (DS) vocabulary.

To get around this, I 1) set up a separate XML feed in the source site's view (as a separate feed "page" in the Views view) as http://example-source.com/feed_2 that listed only the taxonomy terms for all the nodes I wanted to pull in from feed_1. Then I 2) set up a 2nd feed importer on the destination site and pulled in all the terms from feed_2 into the DS vocabulary, with the XPath root set as /xml/taxonomy/terms/term. (In your case, the XPath root would be /mysite/song/terms, with the XPath query being just being 'term' - a child element, not 'terms/term' - a grandchild element).

Then, with all the terms already in place in the destination site's vocabulary, I went back after the original XML feed content (feed_1) to generate the nodes. Since I had the original feed ALSO putting all the terms for each node into /xml/node/all_terms (see Sample 2 below), I 3) pulled in the nodes using Feeds and Feeds XPath Parser, and all the accompanying terms Feeds Tamper module. I had Feeds Tamper parse <all_terms> by setting a non-comma delimiter (|| in my case) to explode the terms apart in order for Feeds to assign them as individual terms for each node. This properly added all the terms to the newly generated node on the destination server.

Why did this work? I'm not sure, but it seems like where you set the context level in your root makes a difference (<all_terms> is at that level). In other words, if the taxonomy term tags are not at the same level as the root (i.e. //song for you), then it seems to want to stuff all the terms into one term.

There had to have been a simpler way to do this (i.e. multiple term assignments to a single node from the XML) during the node import to Skip #1 and the Feeds Tamper parsing, but I didn't see it. If someone else has the solution, please let me know. :)

All XML was generated and parsed by Views, Bonus:Views Export, Feeds, Feeds XPath Parser, and Feeds Tamper modules, and some custom PHP (Views template files - .tpl) on the source server.

Sample 1 = http://example-source.com/feed_1... Sample 2 = http://example-source.com/feed_2.

Sample of XML for #1 (most terms omitted)

<xml>
<taxonomy_terms>
	<term>
		<term_id>353</term_id>
		<term_vocab_id>26</term_vocab_id>
		<term_text>Term 1 here</term_text>
		<term_description></term_description>
	</term>
	<term>
		<term_id>65</term_id>
		<term_vocab_id>5</term_vocab_id>
		<term_text>Term 2 here</term_text>
		<term_description></term_description>
	</term>
	<term>
		<term_id>380</term_id>
		<term_vocab_id>5</term_vocab_id>
		<term_text>Term3</term_text>
		<term_description></term_description>
	</term>
	<term>
		<term_id>25</term_id>
		<term_vocab_id>3</term_vocab_id>
		<term_text>Term 4, here</term_text>
		<term_description></term_description>
	</term>
</taxonomy_terms>
</xml>

Sample XML of node pull from #2 (this is only one node - there were dozens in my live case)

<xml>
  <node>
    <nid>20483</nid>

    <path>http://example.com/node/20483</path>

    <title>Article Title</title>

    <all_terms>Term 1 here||Term 2 here||Term3||Term 4, here</all_terms>

    <body>Body Test blahd blah dlb ahdlb ahd b. Body Test blahd blah dlb ahdlb ahd b. Body Test blahd blah dlb ahdlb ahd b. Body Test blahd blah dlb ahdlb ahd b. Body Test blahd blah dlb ahdlb ahd b. Body Test blahd blah dlb ahdlb ahd b. Body Test blahd blah dlb ahdlb ahd b. Body Test blahd blah dlb ahdlb ahd b. </body>

<taxonomy_terms>
	<term>
		<term_id>353</term_id>
		<term_vocab_id>26</term_vocab_id>
		<term_text>Term 1 here</term_text>
		<term_description></term_description>
	</term>
	<term>
		<term_id>65</term_id>
		<term_vocab_id>5</term_vocab_id>
		<term_text>Term 2 here</term_text>
		<term_description></term_description>
	</term>
	<term>
		<term_id>380</term_id>
		<term_vocab_id>5</term_vocab_id>
		<term_text>Term3</term_text>
		<term_description></term_description>
	</term>
	<term>
		<term_id>25</term_id>
		<term_vocab_id>3</term_vocab_id>
		<term_text>Term 4, here</term_text>
		<term_description></term_description>
	</term>
</taxonomy_terms>

  </node>
</xml>
summit’s picture

Hi,

I have a xml feed what I can't manipulate myself, so I am interested in the solution of the following:
There had to have been a simpler way to do this (i.e. multiple term assignments to a single node from the XML) during the node import to Skip #1 and the Feeds Tamper parsing, but I didn't see it. If someone else has the solution, please let me know.
Thanks a lot in advance,

Greetings, Martijn

arkjoseph’s picture

I don't believe the proposed solution of using different importers for each nested element is ideal and in fact, quite a lot of over head in xml creation.

Still researching this...