MS word XML映射,重复段控制 [英] MS word XML mapping, repeating section control

查看:254
本文介绍了MS word XML映射,重复段控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我有一个MS Word(2016)temaplate并将XML文件映射到表格中的内容控件,同时使用重复控件。这工作正常,但现在我想在重复控件中过滤映射的XML数据。



说我有这个XML文件



 <?  xml     version   =  1.0   编码  =  utf-8  >  
< 图书 >
< book >
< title > Word Programmierung < / title >
< < span class =code-leadattribute> authors >
< 作者 > Cindy Meister < < span class =code-leadattribute> / author >
< 作者 > ChristianFreßdorf< / author >
< / authors >
< publisher < span class =code-keyword>> Microsoft Press < / publisher < span class =code-keyword>>
< isbn > 000 < / isbn < span class =code-keyword>>
< / book >
< book >
< title > XML Pocket Consultant < / title >
< 作者 >
< ; 作者 > William Stanek < / author >
< / authors >
< 发布商 > Microsoft Press < / publisher >
< isbn > 111 < / isbn >
< / book < span class =code-keyword>>
< book >
< title > Office的.NET开发< / title >
< 作者 >
< 作者 > Andrew Whitechapel < / author >
< / authors >
< publisher > Microsoft Press < / publisher >
< isbn > 111 < / isbn >
< / book >
< book >
< title > Office 2007的Visual Studio工具< / title >
< authors >
< author > Eric Carter < / author >
< 作者 > Eric Lippert < / author >
< / authors >
< 发布商 > Addison Wesley < span class =code-keyword>< / publisher >
< isbn > 000 < span class =code-keyword>< / isbn >
< / book >
< / books >





我使用此代码进行映射



设置 rng = doc.Tables( 1 )。行( 2 )。范围
doc.SelectContentControlsByTitle( Title).Item( 1 )。XMLMapping.SetMapping _
// title
doc.SelectContentControlsByTitle( Publisher )。项目( 1 )。XMLMapping.SetMapping _
// publisher
doc.SelectContentControlsByTitle( ISBN )。Item( 1 )。XMLMapping.SetMapping _
< span class =code-string> // isbn
doc.SelectContentControlsByTitle( 作者)。Item( 1 ) .XMLMapping.SetMapping _
// author
doc.SelectContentControlsByTitle( 作者)。项目( 1 ).XMLMapping.SetMapping _
//作者/作者
< span class =code-keyword>设置 ccRepSec = rng.ContentControls.Add(Word.WdContentControlType.wdContentControlRepeatingSection,rng)
ccRepSec.XMLMapping.SetMapping // books / book [./ isbn ='000']
MapContentControls = True





Xpath字符串//books/book[./isbn ='000']应该只给2书籍,但返回4本书在文件中。



我尝试过:



我测试过我的XML文件上的Xpath是正确的,所以认为这不是在MS Word中过滤重复控件上的映射的方法

解决方案

这有点晚了帮助原始海报,但万一其他人发现这篇文章,这里是对问题的解释。



虽然其他内容控件支持标准(MSXML3)XPath语法,重复节内容控件似乎只支持非常简单的XPath语法,只允许/(根选择器),元素名称和索引谓词[n],并且最终元素名称不能有索引。甚至不支持索引谓词[position()= n]的长形式。



如果使用任何无效的XPath,Word会忽略无效部分或根据你设置的方式返回一个错误,所以最终将//books/book[./isbn ='000']作为/ books [1] / book来处理。



我怀疑复杂的XPath不允许的原因是因为实现了重复节内容控件的方式:删除最后一个元素(例如通过将谓词设置为false)也删除模板

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 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

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

解决方案

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.


这篇关于MS word XML映射,重复段控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆