MXXML2.IVBSAXLexicalHandler_startCDATA 未检测到词法 <![CDATA[]]>

Lexical <![CDATA[]]> is not getting detected by MXXML2.IVBSAXLexicalHandler_startCDATA

我正在尝试通过 VBA

中的 SAXON 库处理大型 XML 文件

来源 XML 有以下标签

<Id><![CDATA[IPMS_TEST_DS_2 & 13]]></Id>

Destination XML while while writing into a destination file if am getting following xml tag

<Id>IPMS_TEST_DS_2 &amp; 13</Id> <!--- This should be as same as parent XML -->

通过将 SAXXMLReader60 实现为 reader & MXXMLWriter60 作为编写器 (来源:FlyLib.com, docs.microsoft.com

Implements IVBSAXContentHandler
Implements SAXXMLReader60
Implements IVBSAXLexicalHandler
Implements IVBSAXDTDHandler

Private rdr As New SAXXMLReader60
Private wrt As New MXXMLWriter60

Private ch As IVBSAXContentHandler
Private dtdh As IVBSAXDTDHandler
Private lexh As IVBSAXLexicalHandler

Private Sub Class_Initialize()

    Set ch = wrt 
    Set dtdh = wrt

    Call rdr.putProperty("http://xml.org/sax/properties/lexical-handler", wrt)
    Set lexh = wrt

End Sub

'....Following Event handlers are not getting triggered...

Private Sub IVBSAXLexicalHandler_comment(strChars As String)
End Sub

Private Sub IVBSAXLexicalHandler_endCDATA()
    lexh.endCDATA
End Sub

Private Sub IVBSAXLexicalHandler_endDTD()
End Sub

Private Sub IVBSAXLexicalHandler_endEntity(strName As String)
End Sub

Private Sub IVBSAXLexicalHandler_startCDATA()
    lexh.startCDATA
End Sub

请建议在阅读时检测 < ![CDATA[]]> 的方法

CDATA 不是 XSLT 使用的 XDM 数据模型的一部分:<![CDATA[IPMS_TEST_DS_2 & 13]]>IPMS_TEST_DS_2 &amp; 13 不同,但完全同义,表示具有字符串值 IPMS_TEST_DS_2 & 13 的文本节点。 XSLT 样式表无法区分它们。

A​​ndrew Welch 有一个名为 LEXEV 的处理器,它允许在 XDM 数据模型中表示 CDATA。我忘记了细节,但它使用了类似处理指令的东西 <?CDATA IPMS_TEST_DS_2 & 13?>。您可以在 SAX 过滤器中使用类似的技术。当然,这与名为 "CDATA".

的实际处理指令没有区别

经过各种选项的多次迭代,我知道了调用 SAXReader.putProperty

的正确方法

它捕获 CDATA 以写入目标 XML

主代码块

Sub Main()
     Dim saxRdr As New SAXXMLReader60
     Dim evntHndlr As New SAXEventHandlerClass

     Set saxRdr.contentHandler = evntHndlr
     Call saxRdr.putProperty("http://xml.org/sax/properties/lexical-handler", evntHndlr.wrt)
     '......above line makes event IVBSAXContentHandler_characters(strChars As String) to catch CDATA tag as it is......

     saxRdr.parseURL "test.xml"

     Set saxRdr = Nothing
End sub

SAXEventHandlerClass: characters 事件将 CDATA Lexical 自动捕获到 strChars

Private Sub IVBSAXContentHandler_characters(strChars As String)
     'content writer logic goes here
End Sub