最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

vba - XML Namespace Prefixes inconsistencies - Stack Overflow

programmeradmin5浏览0评论

I have the following code which is supposed to insert content controls for category and title of the document.

Sub insert_Header()

' Declare the CCs that we use later on
Dim titleCC As contentControl
Dim idCC As contentControl
Dim rng As range

' Set range to the header of the first section
Set rng = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).range

' Insert the Category content control
Set idCC = ActiveDocument.ContentControls.Add(wdContentControlText, rng)
With idCC
    .Title = "Category"
    .xmlMapping.SetMapping "/ns1:coreProperties[1]/ns1:category[1]" ' Correct mapping for Category
End With

' Collapse range to the end
rng.Collapse Direction:=wdCollapseEnd
rng.SetRange Start:=rng.End, End:=rng.End + 1

' Insert a _
rng.Text = "_"

' Collapse range to the end
rng.Collapse Direction:=wdCollapseEnd
rng.SetRange Start:=rng.End, End:=rng.End + 1


' Insert Title CC
Set titleCC = ActiveDocument.ContentControls.Add(wdContentControlRichText, rng)
With titleCC
    .Title = "doc. Name"
    .xmlMapping.SetMapping "/ns1:coreProperties[1]/ns0:title[1]"
End With
End Sub

Now the issue is that while the category is inserted just fine and linked to the built in doc props, the title just throws a run time error '6324': Reference to undeclared namespace prefix: 'ns1'.

I got these XPaths by inserting content controls into the document via the quick parts menu and converting it to a zip file. I then went into the XML files of the zip to copy paste the XPaths. Is there a different way of finding out XPaths?

I have the following code which is supposed to insert content controls for category and title of the document.

Sub insert_Header()

' Declare the CCs that we use later on
Dim titleCC As contentControl
Dim idCC As contentControl
Dim rng As range

' Set range to the header of the first section
Set rng = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).range

' Insert the Category content control
Set idCC = ActiveDocument.ContentControls.Add(wdContentControlText, rng)
With idCC
    .Title = "Category"
    .xmlMapping.SetMapping "/ns1:coreProperties[1]/ns1:category[1]" ' Correct mapping for Category
End With

' Collapse range to the end
rng.Collapse Direction:=wdCollapseEnd
rng.SetRange Start:=rng.End, End:=rng.End + 1

' Insert a _
rng.Text = "_"

' Collapse range to the end
rng.Collapse Direction:=wdCollapseEnd
rng.SetRange Start:=rng.End, End:=rng.End + 1


' Insert Title CC
Set titleCC = ActiveDocument.ContentControls.Add(wdContentControlRichText, rng)
With titleCC
    .Title = "doc. Name"
    .xmlMapping.SetMapping "/ns1:coreProperties[1]/ns0:title[1]"
End With
End Sub

Now the issue is that while the category is inserted just fine and linked to the built in doc props, the title just throws a run time error '6324': Reference to undeclared namespace prefix: 'ns1'.

I got these XPaths by inserting content controls into the document via the quick parts menu and converting it to a zip file. I then went into the XML files of the zip to copy paste the XPaths. Is there a different way of finding out XPaths?

Share Improve this question asked Mar 21 at 13:51 MDKMDK 274 bronze badges 3
  • In order to evaluate an XPath that uses the namespace-prefix ns1, you have to configure what namespace that prefix is for. Think of namespace-prefixes like a variable (it can be named anything) and the actual namespace that it's referencing is the value (needs to be set) otherwise is "null". – Mads Hansen Commented Mar 21 at 13:57
  • To find all xpaths from an xml you can use pyxml2xpath python package/command or xml2xpath Bash utility. Both use libxml2 in the background. – LMC Commented Mar 21 at 13:58
  • 2 Similar Q here: stackoverflow/questions/36145050/… The accepted solution uses SetMappingByNode and not SetMapping – Tim Williams Commented Mar 21 at 15:45
Add a comment  | 

1 Answer 1

Reset to default 0

If you work with xml including namespaces you need prefix and uri to register the right namespace to use it in xpath. The ns0 and ns1 are only placeholders for missing registration in your code.

/ns1:coreProperties[1]/ns1:category[1] should be
/cp:coreProperties/cp:category
/ns1:coreProperties[1]/ns0:title[1] should be
/cp:coreProperties/dc:title

===== EXAMPE =====

Dim customXML As CustomXMLPart
Dim xmlData As String

' Loop through all Custom XML Parts in the Word document
For Each customXML In ActiveDocument.CustomXMLParts
    ' Print XML content to Immediate Window (Ctrl + G)
    xmlData = customXML.XML
    Debug.Print xmlData
Next customXML
发布评论

评论列表(0)

  1. 暂无评论