R
Rainer
Hello everybody,
I need to format a long XML file with Word 2007. I try to do this with
the new "custom XML" features in conjunction with content controls.
However, I have a problem using XPath expressions programmatically.
Consider the following XML file called "foo.xml":
<foo title="an attempt">
<ul>
<li>This works fine</li>
<li>Before the inline element <em>Inside the inline element</em> after
the inline element</li>
<li>This also works fine</li>
</ul>
</foo>
I load this file into Word 2007 using a short VBA sub:
Sub load_custom_xml_part()
ActiveDocument.CustomXMLParts.Add.Load ("c:\foo.xml")
End Sub
The content control for the mapping was also created
programmatically:
Sub create_content_control()
Dim strTitle As String
strTitle = "My_arbitrary_title"
Dim oCC2 As Word.ContentControl
Set oCC2 =
Application.Selection.ContentControls.Add(wdContentControlText)
oCC2.Title = strTitle
End Sub
In order to map the content of the first list element into the content
control, I use the XPath expression "/foo/ul/li[1]", which works fine:
Sub bind_nodes()
Dim strXPath As String
strXPath = "/foo/ul/li[1]"
ActiveDocument.ContentControls(1).XMLMapping.SetMapping strXPath
End Sub
However, I need an XPath expression that returns the second list
element in three "portions" as they have to be formatted with separate
content controls:
- "Before the inline element "
- "Inside the inline element"
- " after the inline element"
The XPath expression "/foo/ul/li[2]/text()[1]" worked perfectly fine
when I tested it with this XPath Demo website:
http://www.futurelab.ch/xmlkurs/xpath.en.html
However, it doesn't seem to work with VBA. The content control doesn't
change its text when I use
strXPath = "/foo/ul/li[2]/text()[1]"
inside of bind_nodes().
Any ideas?
Kind regards,
Rainer
I need to format a long XML file with Word 2007. I try to do this with
the new "custom XML" features in conjunction with content controls.
However, I have a problem using XPath expressions programmatically.
Consider the following XML file called "foo.xml":
<foo title="an attempt">
<ul>
<li>This works fine</li>
<li>Before the inline element <em>Inside the inline element</em> after
the inline element</li>
<li>This also works fine</li>
</ul>
</foo>
I load this file into Word 2007 using a short VBA sub:
Sub load_custom_xml_part()
ActiveDocument.CustomXMLParts.Add.Load ("c:\foo.xml")
End Sub
The content control for the mapping was also created
programmatically:
Sub create_content_control()
Dim strTitle As String
strTitle = "My_arbitrary_title"
Dim oCC2 As Word.ContentControl
Set oCC2 =
Application.Selection.ContentControls.Add(wdContentControlText)
oCC2.Title = strTitle
End Sub
In order to map the content of the first list element into the content
control, I use the XPath expression "/foo/ul/li[1]", which works fine:
Sub bind_nodes()
Dim strXPath As String
strXPath = "/foo/ul/li[1]"
ActiveDocument.ContentControls(1).XMLMapping.SetMapping strXPath
End Sub
However, I need an XPath expression that returns the second list
element in three "portions" as they have to be formatted with separate
content controls:
- "Before the inline element "
- "Inside the inline element"
- " after the inline element"
The XPath expression "/foo/ul/li[2]/text()[1]" worked perfectly fine
when I tested it with this XPath Demo website:
http://www.futurelab.ch/xmlkurs/xpath.en.html
However, it doesn't seem to work with VBA. The content control doesn't
change its text when I use
strXPath = "/foo/ul/li[2]/text()[1]"
inside of bind_nodes().
Any ideas?
Kind regards,
Rainer