【问题标题】:How to get attribute value of node in xml using xpath in javascript如何在javascript中使用xpath获取xml中节点的属性值
【发布时间】:2015-09-02 18:57:11
【问题描述】:

您好,我正在尝试使用 javascript 从客户端的 sharepoint 获取用户配置文件属性。但我没有获得 xml 中节点的值。 如何得到它们。 xml 将如下所示:

如何使用xpath获取xml中节点的属性值

这里我想得到<name>标签<Name>AccountName</Name>Name标签之间的值

想要得到值 = abc xpath 表达式是什么

请帮忙

<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <GetUserProfileByNameResponse xmlns="http://microsoft.com/webservices/SharePointPortalServer/UserProfileService">
      <GetUserProfileByNameResult>
        <Pro pertyData>
          <IsPrivacyChanged>false</IsPrivacyChanged>
          <IsValueChanged>false</IsValueChanged>
          <Name>UserProfile_GUID</N ame>
            <Privacy>NotSet</Privacy>
            <Values>
              <ValueData>
                <Value xmlns:q1="http://microsoft.com/wsdl/types/" xsi:type="q1:guid">8ed84415-7330-4857-a7d2- d797d71c439f

                </Value>
              </ValueData>
            </Values>
            </PropertyData>
            <PropertyData>
              <IsPrivacyChanged>false</IsPrivacyChanged>
              <Is ValueChanged>false</IsValueChanged>
                <Name>AccountName</Name>
                <Privacy>NotSet</Privacy>
                <Values>
                  <ValueData>
                    <Value xsi:type="xsd:string">abc

                    </Value>
                  </ValueData>
                </Values>
            </PropertyData>
      </GetUserProfileByNameResult>
    </GetUserProfileByNameResponse>
    </ soap:Body>
</soap:Envelope>

请帮助我。

【问题讨论】:

  • 发布一些 javascript 显示您到目前为止尝试过的内容..

标签: sharepoint xpath


【解决方案1】:
var propertyData = $(responseXML).find("PropertyData").filter(function(e){
    return $(this).find("Name").text() == "AccountName";
});
var value = propertyData.length > 0 ? propertyData.find('Value').text() : '';

由于您尝试通过 SharePoint Web Services 检索用户配置文件,我建议使用 SPServices library,它隐藏了(几乎)所有在 JavaScript 中使用 SharePoint Web Services 的复杂性。以下示例演示如何使用GetUserProfileByName method 检索用户配置文件并处理结果:

function getUserProfile(accountName,completeFn) {
  var userInfo = {};
  $().SPServices({
    AccountName: accountName,  
    operation: 'GetUserProfileByName',
    completefunc: function (xData, Status) {
      $(xData.responseXML).SPFilterNode("PropertyData").each(function() {
        userInfo[$(this).find("Name").text()] = $(this).find("Value").text();
      }); 
      completeFn(userInfo);
    }
  });
}

var loginName = 'i:0#.f|membership|username@contoso.onmicrosoft.com';
getUserProfile(loginName,function(info){
    console.log(info);    
});

【讨论】:

    【解决方案2】:

    您必须遍历从 SPServices 返回的 xml 节点。我已经编写了一个函数来获取所需的用户配置文件属性。

    function getUPValue(x, p) {  
       var thisValue = $(x).SPFilterNode("PropertyData").filter(function() {  
         return $(this).find("Name").text() == p;  
       }).find("Values").text();  
       return thisValue;  
     }  
    

    进一步查询您只需要调用的用户属性,如下所示,

    getUPValue(xData.responseXML, "WorkEmail");  
    

    这篇文章通过here提供了它的详细概述

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-26
      • 2011-11-20
      • 2023-03-27
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      • 2012-08-05
      • 1970-01-01
      相关资源
      最近更新 更多