Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a MS Word (2016) temaplate and mapped a XML file to the content controls in a table, also using a repeating control. This Works fine but now I would like to filter the mapped XML data in the repeating control.

Say I have this XML File

XML
<?xml version="1.0" encoding="utf-8"?>
<books>
  <book>
    <title>Word Programmierung</title>
    <authors>
      <author>Cindy Meister</author>
      <author>Christian Freßdorf</author>
    </authors>
    <publisher>Microsoft Press</publisher>
    <isbn>000</isbn>
  </book>
  <book>
    <title>XML Pocket Consultant</title>
    <authors>
      <author>William Stanek</author>
    </authors>
    <publisher>Microsoft Press</publisher>
    <isbn>111</isbn>
  </book>
   <book>
    <title>.NET Development for Office</title>
    <authors>
      <author>Andrew Whitechapel</author>
    </authors>
    <publisher>Microsoft Press</publisher>
    <isbn>111</isbn>
  </book>
    <book>
    <title>Visual Studio Tools for Office 2007</title>
    <authors>
      <author>Eric Carter</author>
      <author>Eric Lippert</author>
    </authors>
    <publisher>Addison Wesley</publisher>
    <isbn>000</isbn>
  </book>
</books>


and I use this code for the mapping

VB
Set rng = doc.Tables(1).Rows(2).Range
    doc.SelectContentControlsByTitle("Title").Item(1).XMLMapping.SetMapping _
      "//title"
    doc.SelectContentControlsByTitle("Publisher").Item(1).XMLMapping.SetMapping _
      "//publisher"
    doc.SelectContentControlsByTitle("ISBN").Item(1).XMLMapping.SetMapping _
      "//isbn"
    doc.SelectContentControlsByTitle("Author").Item(1).XMLMapping.SetMapping _
      "//author"
    doc.SelectContentControlsByTitle("Authors").Item(1).XMLMapping.SetMapping _
      "//authors/author"
    Set ccRepSec = rng.ContentControls.Add(Word.WdContentControlType.wdContentControlRepeatingSection, rng)
    ccRepSec.XMLMapping.SetMapping "//books/book[./isbn = '000']"
    MapContentControls = True


The Xpath string "//books/book[./isbn = '000']" should only give 2 books but instead al 4 books are returned in the document.

What I have tried:

I tested the Xpath on my XML file and that is correct, so think this is not the way to filter the mapping on a repeating control in MS Word
Posted
Updated 15-Apr-19 15:33pm
v2

1 solution

It's a bit late to help the original poster, but in case anyone else finds this article, here's an explanation of the problem.

Although other content controls support standard (MSXML3) XPath syntax, the repeating section content control only appears to support very simple XPath syntax, allowing only / (root selector), element name, and index predicate [n], and the final element name can't have an index. Even the long form of the index predicate [position() = n] isn't supported.

If you use any invalid XPath, Word either ignores the invalid part or returns an error, depending how you set it, so ends up treating //books/book[./isbn = '000'] as /books[1]/book.

I suspect the reason complex XPaths aren't allowed is because of the way the repeating section content control is implemented: deleting the last element (for example by setting the predicate to false) deletes the template as well.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900