MSXML - 命名空间信息不持久

发布于 2024-09-26 12:03:41 字数 1864 浏览 0 评论 0原文

我正在使用 MSXML 6.0 将我自己的 XML 转换为另一种 XML 格式。我不确定我是否只是不完全理解 MSXML 的工作原理,但我相信我已经注意到它的一些奇怪的行为....

我使用 setProperty 方法,例如

XmlDocument.setProperty('SelectionNamespaces',
' xmlns:ms=''http://mydomain.com/2010/MySchema''');

然后我使用自己的自定义序列化程序构建 XML在内存中(不保存到磁盘)。序列化后,我将加载 XSLT 文件并使用 transformNodeToObject 例如,

AppXmlDoc.transformNodeToObject(XslXmlDoc, AStreamForTransformedXml);

问题是转换正在工作,但没有一个与我其中的 XPath 匹配的特定模板。我通过 Visual Studio 使用测试数据运行 XSLT 文件本身,消除了该文件本身的任何问题,并且它按预期工作。然后我认为这一定是一个编码问题,所以我确保所有涉及的文档都以 UTF-8 格式读/写......仍然没有运气。

以下是转换的示例:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ms="http://mydomain.com/2010/MySchema" exclude-result-prefixes="ms">
<xsl:template match="/">
<ARoot>      
  <head>
    <xsl:apply-templates select="ms:Element/ms:SubElement" />
  </head>
  <body>
    <xsl:apply-templates select="ms:Element/ms:DifferentSubElement" />
  </body>
</ARoot>

因此,通过 MSXML 运行时的转换结果会带来基本结构,但不包含任何模板数据。经过一些测试,我发现让它工作的唯一方法是按照以下步骤进行:

  1. 创建一个新的 XML 文档
  2. 使用 setProperty 设置命名空间信息
  3. 序列化 XML 并保存到磁盘。
  4. 关闭文档 - 额外步骤
  5. 创建新的 Xml 文档 - 额外步骤
  6. 重新加载文档 - 额外步骤
  7. 重新设置命名空间信息 - 额外步骤
  8. 执行转换。

因此,MSXML 似乎在某个时刻失去了对命名空间信息的跟踪。更奇怪的是,即使您重置名称空间信息(序列化后)并尝试转换,它仍然不起作用!它似乎只有在我保存文档、关闭它并重新创建一个新的 XML 文档并将其重新加载时才起作用(因此我需要重置命名空间)。

有人对此有什么想法/想法吗?

I am using MSXML 6.0 to perform a transform of my own XML into another XML format. I am not sure if I maybe just don't understand exactly how MSXML works, but I believe I have noticed some strange behaviour with it....

I am adding in the namespaces to my XML doc using the setProperty method e.g.

XmlDocument.setProperty('SelectionNamespaces',
' xmlns:ms=''http://mydomain.com/2010/MySchema''');

Then I am building up the XML using my own custom serializer in memory (not saving to disk). Once serialized I then load in the XSLT file and perform the transformation using transformNodeToObject e.g.

AppXmlDoc.transformNodeToObject(XslXmlDoc, AStreamForTransformedXml);

The problem is the transform is working but none of the specific template matching XPath I have in it is. I eliminated any problems with the XSLT file itself by running it with test data through Visual Studio and it worked as expected. I then assumed it must have been an encoding issue so I made sure that all the documents involved were being read/written out as UTF-8....still no luck.

Here is an example of what the transform looks like:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ms="http://mydomain.com/2010/MySchema" exclude-result-prefixes="ms">
<xsl:template match="/">
<ARoot>      
  <head>
    <xsl:apply-templates select="ms:Element/ms:SubElement" />
  </head>
  <body>
    <xsl:apply-templates select="ms:Element/ms:DifferentSubElement" />
  </body>
</ARoot>

So the result of the transform when run through MSXML brings over the basic structure but does not include any of the template data. After some testing I discovered the only way to get it to work is to do it in the following steps:

  1. Create a new XML doc
  2. Set the namespace info using setProperty
  3. Serialize the XML and save to disk.
  4. Close the document - extra step
  5. Create a new Xml doc - extra step
  6. Reload the document - extra step
  7. Re-set the namespace info - extra step
  8. Perform the transform.

So it appears that MSXML loses track of the Namespace information at somepoint. What makes it even more weird is even if you reset the namespace info (after serializing) and try the transform it still doesn't work! It will only seem to work if I save the document, close it and recreate a new XML document and load it back in (which as a consequence I need to reset the namespaces).

Anyone have any thoughts/ideas on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原来分手还会想你 2024-10-03 12:03:41

SelectionNamespace 属性 用于 XPath使用 selectNodes 和 selectSingleNode 方法,不用于 XSLT 或其他目的。所以我不确定你为什么设置它以及你期望它有什么帮助,因为你似乎没有使用 selectNodes 或 selectSingleNode。
您希望通过 MSXML 6 实现什么目标?如果要创建一些在命名空间中包含元素和/或属性的 MSXML DOM 文档,请确保使用 createNode,因为这是 MSXML API 中唯一的命名空间感知方法。

The SelectionNamespace property is used for XPath with the selectNodes and selectSingleNode methods, not for XSLT or other purposes. So I am not sure why you set it and what you expect it to help with as you do not seem to use selectNodes or selectSingleNode.
What is it that you want to achieve with MSXML 6? If you want to create some MSXML DOM documents with elements and/or attributes in namespaces then make sure you use createNode as that is the only namespace aware method in MSXML's API.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文