【问题标题】:SSIS Xpath query expression errorSSIS Xpath 查询表达式错误
【发布时间】:2014-12-17 15:59:10
【问题描述】:

我正在尝试通过 SSIS 调用 SalesForce Web 服务,并且正在尝试检索 sessionID 节点的值。

这是 XML:

 <?xml version="1.0" encoding="utf-16"?>
<LoginResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <metadataServerUrl xmlns="urn:partner.soap.sforce.com">https://xxxxxx/services/Soap/m/31.0/xxxxxx</metadataServerUrl>
  <passwordExpired xmlns="urn:partner.soap.sforce.com">false</passwordExpired>
  <sandbox xmlns="urn:partner.soap.sforce.com">true</sandbox>
  <serverUrl xmlns="urn:partner.soap.sforce.com">https://xxx/services/Soap/u/31.0/xxx</serverUrl>
  <sessionId xmlns="urn:partner.soap.sforce.com">xxxxxxxxxx</sessionId>
  <userId xmlns="urn:partner.soap.sforce.com">xxx</userId>
  <userInfo xmlns="urn:partner.soap.sforce.com">
    <accessibilityMode>false</accessibilityMode>
    <currencySymbol>$</currencySymbol>
    <orgAttachmentFileSizeLimit>5242880</orgAttachmentFileSizeLimit>
    <orgDefaultCurrencyIsoCode>USD</orgDefaultCurrencyIsoCode>
    <orgDisallowHtmlAttachments>false</orgDisallowHtmlAttachments>
    <orgHasPersonAccounts>false</orgHasPersonAccounts>
    <organizationId>xxxxxxxx</organizationId>
    <organizationMultiCurrency>false</organizationMultiCurrency>
    <organizationName>xxxxx</organizationName>
    <profileId>xxxxx</profileId>
    <roleId xsi:nil="true" />
    <sessionSecondsValid>7200</sessionSecondsValid>
    <userDefaultCurrencyIsoCode xsi:nil="true" />
    <userEmail>xxxxxxx</userEmail>
    <userFullName>xxxxx</userFullName>
    <userId>xxxxxxx</userId>
    <userLanguage>en_US</userLanguage>
    <userLocale>en_US</userLocale>
    <userName>xxxxxxx</userName>
    <userTimeZone>America/New_York</userTimeZone>
    <userType>Standard</userType>
    <userUiSkin>Theme3</userUiSkin>
  </userInfo>
</LoginResult>

我通过http://www.xpathtester.com/xpath 成功测试了这个表达式。

编辑: 现在尝试执行脚本任务,但我仍然没有找到正确的组合来选择此信息。 XML 中有多个命名空间,下面的代码返回 0 个节点。很郁闷!

public void Main()
    {
        string loginResult;
        string sessionID;

        loginResult = Dts.Variables["User::loginResult"].Value.ToString();

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(loginResult);

        var xmlnsManager = new System.Xml.XmlNamespaceManager(doc.NameTable);
        //xmlnsManager.AddNamespace("t1", "http://www.w3.org/2001/XMLSchema-instance");

        xmlnsManager.AddNamespace("ns", "urn:partner.soap.sforce.com");

        XmlNodeList list = doc.SelectNodes("/LoginResult/ns:sessionID", xmlnsManager);

        for (int i = 0; i < list.Count; i++)
        {
            sessionID = list[i].Value;
        }

        Dts.TaskResult = (int)ScriptResults.Success;
    }
}

【问题讨论】:

    标签: sql-server xml xpath ssis


    【解决方案1】:

    找到sessionId 元素的正确表达式是:

    //*[local-name() = 'sessionId']/text()
    

    在您的输入 XML 中,您要查找的元素:

    <sessionId xmlns="urn:partner.soap.sforce.com">xxxxxxxxxx</sessionId>
    

    没有有前缀吗?它位于默认命名空间中,不需要元素作为前缀。 因此,一个可能的解释是这样的表达式

    //*:sessionId
    

    仅查找输入 XML 中带有前缀的元素。这应该不是问题,但据我所知,SSIS 以命名空间 XML 的问题而闻名(参见例如 herehere)。

    就 XPath 规范而言,像 //*:root 这样的表达式应该能够找到像这样的元素

    <root xmlns="www.example.com"/>
    

    编辑:显然,您已经完全改变了问题 - 现在还有另一个问题:最外层元素 LoginResult 根本不在 no 命名空间中,而不是在xsi:命名空间:

    <LoginResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
    

    架构实例命名空间恰好在此元素上声明,但并未在此处使用。因此,将您的代码更改为:

    var xmlnsManager = new System.Xml.XmlNamespaceManager(doc.NameTable);
    
    xmlnsManager.AddNamespace("ns", "urn:partner.soap.sforce.com");
    
    XmlNodeList list = doc.SelectNodes("/LoginResult/ns:sessionId", xmlnsManager);
    

    【讨论】:

    • 看到有关 XML 错误/黑客的文章让我觉得我会使用另一种方法来做我想做的事。谢谢。
    • 我更新了原始帖子中的代码以显示我尝试过的内容。它仍然选择 0 个节点。
    • @Jeremy 另外,将sessionId 的最后一个字符小写。 XPath 区分大小写。
    • 啊。键盘和椅子之间存在问题。非常感谢!
    猜你喜欢
    • 2010-11-18
    • 2023-02-17
    • 2020-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    相关资源
    最近更新 更多