tags:

views:

114

answers:

2

I have have two xml docs:

XML1:

<Books>
     <Book id="11">
          .......
          <AuthorName/>
     </Book>
     ......
</Books>

XML2:

<Authors>
     <Author>
          <BookId>11</BookId>
          <AuthorName>Smith</AuthorName>
     </Author>
</Authors>

I'm trying to do the following:

Get the value of XML2/Author/AuthorName where XML1/Book/@id equals XML2/Author/BookId.

XML2/Author/AuthorName[../BookId = XML1/Book/@id]
A: 

The document function is your friend, here is a short tutorial how to combine multiple input files.

EDIT: Of course, that works only if your are using Xpath in an Xslt script.

Doc Brown
Are you aware that the `document()` function is available only in XSLT? The original question is in the area of XPath and XSLT is not expected.Please, correct your answer. As it is, it doesn't provide a solution in the general case.
Dimitre Novatchev
In the end I would like to take the 'authorname' from XML2 and assign the value to 'authorname' in XML1. I'm developing in Livecycle and could use xslt to do the transfor, but they have work flow acitivities were you can assign value using xpath.
Arnej65
@Arnej65: You cannot "assign the value to 'authorname' in XML1" with XSLT. An XSLT transformation cannot change the XML document it is transforming -- it can create just a new XML document as its result.
Dimitre Novatchev
+1  A: 

An XPath 1.0 expression cannot refer to more than one XML document, unless the references to the additional documents have been set up in the context of the XPath engine by the hosting language. For example, if XSLT is the hosting language, then it makes its document() function available to the XPath engine it is hosting.

document($xml2Uri)/Authors/Author[BookId = $mainDoc/Books/Book/@id]

Do note, that even the main XML document needs to be referenced via another <xsl:variable>, named here $mainDoc.

The document() function is available only if Xpath is hosted by XSLT! This is not mentioned in the answer of Doc Brown and is misleading the readers.

An XPath 2.x expression may refer to any additional XML document using the XPath 2.0 doc() function.

for $doc  in /,
    $doc2 in doc(someUri)
  return
    $doc2/Authors/Author[BookId = $doc/Books/Book/@id]
Dimitre Novatchev
I'm using livecycle. Both xml files are in memory and I'm trying to return only one result set of xml. XSLT transforms only work on one file at a time,(from my understanding).
Arnej65
@Arnej65 : An XSLT transformation can work with *many* XML documents -- using the `document()` function, a temporary tree, or documents passed as parameters to the transformation.
Dimitre Novatchev
The xslt process in livecycle can only be applied to one document at a time. I have combined the results, and then filtered the final output with XSLT. Thank you for your solution.
Arnej65