如何在 ISXmlLocator 表的 XPath 表达式中使用命名空间?

发布于 2024-10-02 15:51:27 字数 2292 浏览 3 评论 0原文

我在 InstallShield 项目中使用 XML 系统搜索和 XML 文件更改。在以前的安装中,用户为服务器选择了主机名和端口。当用户再次安装时,如果显示以前的设置将是理想的。这个想法是使用 XML 系统搜索功能从 XML 文件中读取值(如果存在)。

鉴于 XML 不包含任何命名空间信息,我能够实现此功能。下面是一个没有命名空间的 XML 示例:

<?xml version="1.0" encoding="UTF-8"?>
<ApplicationSettings ProductVersion="2.4.0.0001" Version="1">
    <Source Mechanism="Server">
        <Server Host="127.0.0.1" Port="11111"></Server>
    </Source>    
</ApplicationSettings>

我用来获取 Server 元素的 XPath 查询是:

/ApplicationSettings/Source/Server

如果我添加一些命名空间信息,那么 XML 系统搜索将不起作用。

<?xml version="1.0" encoding="UTF-8"?>
<ApplicationSettings ProductVersion="2.4.0.0001" Version="1" xmlns="http://127.0.0.1/schema/ApplicationSetting.xsd">
    <Source Mechanism="Server">
        <Server Host="127.0.0.1" Port="11111"></Server>
    </Source>    
</ApplicationSettings>

我还尝试了以下 XPath 表达式:

/*[local-name() = 'ApplicationSettings' and *[local-name() = 'Source' and *[local-name() = 'Server']]]

这不起作用,并且在日志中似乎确实找到了元素,但没有找到属性:

MSI (c) (84:C8) [10:47:17:836]: Invoking remote custom action. DLL: C:\Users\CZIETS~1\AppData\Local\Temp\MSIFF9E.tmp, Entrypoint: ISXmlAppSearch
InstallShield 10:47:17: Searching for an XML Attribute value using the Element '/*[local-name() = 'ApplicationSettings' and *[local-name() = 'Source' and *[local-name() = 'Server']]]' and the Attribute 'Host'.
InstallShield 10:47:17: Attribute 'Host' not found using the following Element: '/*[local-name() = 'ApplicationSettings' and *[local-name() = 'Source' and *[local-name() = 'Server']]]'. Check for the existence of the Attribute.
InstallShield 10:47:17: Searching for an XML Attribute value using the Element '/*[local-name() = 'ApplicationSettings' and *[local-name() = 'Source' and *[local-name() = 'Server']]]' and the Attribute 'Port'.
InstallShield 10:47:17: Attribute 'Port' not found using the following Element: '/*[local-name() = 'ApplicationSettings' and *[local-name() = 'Source' and *[local-name() = 'Server']]]'. Check for the existence of the Attribute.
Action ended 10:47:17: ISXmlAppSearch. Return value 1.

有什么想法吗?

I'm using XML System Search and XML File Changes in an InstallShield project. In a previous installation a user selected a hostname and port for a server. When the user installs again it would be ideal if the previous settings are shown. The idea is to read the values from the XML file if it exists using the XML System Search feature.

I'm able to get this working given that the XML does not contain any namespace information. Here is an example of the XML without a namespace:

<?xml version="1.0" encoding="UTF-8"?>
<ApplicationSettings ProductVersion="2.4.0.0001" Version="1">
    <Source Mechanism="Server">
        <Server Host="127.0.0.1" Port="11111"></Server>
    </Source>    
</ApplicationSettings>

The XPath query I'm using to get to the Server element is:

/ApplicationSettings/Source/Server

If I add some namespace information then the XML System Search does not work.

<?xml version="1.0" encoding="UTF-8"?>
<ApplicationSettings ProductVersion="2.4.0.0001" Version="1" xmlns="http://127.0.0.1/schema/ApplicationSetting.xsd">
    <Source Mechanism="Server">
        <Server Host="127.0.0.1" Port="11111"></Server>
    </Source>    
</ApplicationSettings>

I've also tried the following XPath expression:

/*[local-name() = 'ApplicationSettings' and *[local-name() = 'Source' and *[local-name() = 'Server']]]

This does not work and in the log it seems it does find the element, but not the attribute:

MSI (c) (84:C8) [10:47:17:836]: Invoking remote custom action. DLL: C:\Users\CZIETS~1\AppData\Local\Temp\MSIFF9E.tmp, Entrypoint: ISXmlAppSearch
InstallShield 10:47:17: Searching for an XML Attribute value using the Element '/*[local-name() = 'ApplicationSettings' and *[local-name() = 'Source' and *[local-name() = 'Server']]]' and the Attribute 'Host'.
InstallShield 10:47:17: Attribute 'Host' not found using the following Element: '/*[local-name() = 'ApplicationSettings' and *[local-name() = 'Source' and *[local-name() = 'Server']]]'. Check for the existence of the Attribute.
InstallShield 10:47:17: Searching for an XML Attribute value using the Element '/*[local-name() = 'ApplicationSettings' and *[local-name() = 'Source' and *[local-name() = 'Server']]]' and the Attribute 'Port'.
InstallShield 10:47:17: Attribute 'Port' not found using the following Element: '/*[local-name() = 'ApplicationSettings' and *[local-name() = 'Source' and *[local-name() = 'Server']]]'. Check for the existence of the Attribute.
Action ended 10:47:17: ISXmlAppSearch. Return value 1.

Any ideas?

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

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

发布评论

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

评论(1

不甘平庸 2024-10-09 15:51:27

不幸的是,您是对的,内置 XML 系统搜索不支持名称空间。不过我对你的 XPath 感到困惑。与命名空间无关的搜索不应该仍然反映元素层次结构吗?我认为您最多可能会找到一个具有子源和子服务器的ApplicationsSettings 元素,但要引用ApplicationSettings 元素而不是Server 元素。如果它有效的话。

我建议将:

/ApplicationSettings/Source/Server

改为(未经测试):

/*[local-name() = 'ApplicationSettings']/*[local-name() = 'Source']/*[local-name() = 'Server']

Unfortunately you're right that the built-in XML System Search doesn't support namespaces. However I'm confused about your XPath. Shouldn't the namespace-agnostic search still reflect the element hierarchy? I think at best the one you had might find an ApplicationsSettings element that has a child Source with a child Server, but refer to the ApplicationSettings element instead of the Server element. If it works at all.

I'd suggest changing:

/ApplicationSettings/Source/Server

into this instead (untested):

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