GetHtmlElementAttributeResult GetHtmlElementAttribute(IHTMLElement& htmlElement,
  const ATL::CComBSTR& attributeName)
{
  GetHtmlElementAttributeResult retValue;
  ATL::CComVariant vAttr;
  ATL::CComPtr<IHTMLElement4> htmlElement4;
  if (FAILED(htmlElement.QueryInterface(&htmlElement4)) || !htmlElement4)
  {
    return retValue;
  }
  ATL::CComPtr<IHTMLDOMAttribute> attributeNode;
  if (FAILED(htmlElement4->getAttributeNode(attributeName, &attributeNode)) || !attributeNode)
  {
    return retValue;
  }
  // we set that attribute found but it's not necessary that we can retrieve its value
  retValue.isAttributeFound = true;
  if (FAILED(attributeNode->get_nodeValue(&vAttr)))
  {
    return retValue;
  }
  if (vAttr.vt == VT_BSTR && vAttr.bstrVal)
  {
    retValue.attributeValue = vAttr.bstrVal;
  }
  else if (vAttr.vt == VT_I4)
  {
    retValue.attributeValue = std::to_wstring(vAttr.iVal);
  }
  return retValue;
}
/**
 * Parses XML PERCo response from 'staff' request.
 *
 * Parameters:
 * >pResponse
 * XML response.
 *
 * Returns:
 * Identifiers map.
 */
perco_exchange::ids_t perco_exchange::parseStaffResponse(_In_ IXMLDOMDocument2* pResponse)
{
    ids_t ids;
    ATL::CComVariant value(TEXT("XPath"));
    if (SUCCEEDED(pResponse->setProperty(TEXT("SelectionLanguage"), value)))
    {
        ATL::CComPtr<IXMLDOMElement> documentRequest;
        if (SUCCEEDED(pResponse->get_documentElement(&documentRequest)))
        {
            ATL::CComPtr<IXMLDOMNodeList> staffNodes;
            if (SUCCEEDED(documentRequest->selectNodes(TEXT("staff/staffnode"), &staffNodes)))
            {
                ATL::CComPtr<IXMLDOMNode> staffNode;
                ATL::CComPtr<IXMLDOMNode> identifierNode;
                ATL::CComPtr<IXMLDOMNamedNodeMap> attributeMap;
                while (SUCCEEDED(staffNodes->nextNode(&staffNode)) && staffNode)
                {
                    if (SUCCEEDED(staffNode->selectSingleNode(
                        TEXT("identifiers/identifier/@identifier"), &identifierNode)) &&
                        identifierNode &&
                        SUCCEEDED(identifierNode->get_nodeValue(&value)) &&
                        (VT_BSTR == value.vt))
                    {
                        attributeMap.Release();
                        if (SUCCEEDED(staffNode->get_attributes(&attributeMap)))
                        {
                            ids_t::key_type szKey(value.bstrVal, SysStringLen(value.bstrVal));
                            ids_t::mapped_type data(std::make_unique<data_t>());
                            staffNode.Release();
                            if (SUCCEEDED(attributeMap->getNamedItem(TEXT("last_name"), &staffNode)) &&
                                SUCCEEDED(staffNode->get_nodeValue(&value)) &&
                                (VT_BSTR == value.vt))
                            {
                                data->szLastName =
                                    std::make_unique<STLADD string_type>(value.bstrVal, SysStringLen(value.bstrVal));
                            }
                            staffNode.Release();
                            if (SUCCEEDED(attributeMap->getNamedItem(TEXT("first_name"), &staffNode)) &&
                                SUCCEEDED(staffNode->get_nodeValue(&value)) &&
                                (VT_BSTR == value.vt))
                            {
                                data->szFirstName =
                                    std::make_unique<STLADD string_type>(value.bstrVal, SysStringLen(value.bstrVal));
                            }
                            staffNode.Release();
                            if (SUCCEEDED(attributeMap->getNamedItem(TEXT("middle_name"), &staffNode)) &&
                                SUCCEEDED(staffNode->get_nodeValue(&value)) &&
                                (VT_BSTR == value.vt))
                            {
                                data->szMiddleName =
                                    std::make_unique<STLADD string_type>(value.bstrVal, SysStringLen(value.bstrVal));
                            }
                            staffNode.Release();
                            if (SUCCEEDED(attributeMap->getNamedItem(TEXT("id_internal"), &staffNode)) &&
                                SUCCEEDED(staffNode->get_nodeValue(&value)) &&
                                (VT_BSTR == value.vt))
                            {
                                data->szPERCoId =
                                    std::make_unique<STLADD string_type>(value.bstrVal, SysStringLen(value.bstrVal));
                            }
                            staffNode.Release();
                            if (SUCCEEDED(attributeMap->getNamedItem(TEXT("tabel_id"), &staffNode)) &&
                                SUCCEEDED(staffNode->get_nodeValue(&value)) &&
                                (VT_BSTR == value.vt))
                            {
                                const STLADD regex_type regex(TEXT("[[:blank:]]*([[:graph:]]+)[[:blank:]]*"));
                                STLADD cmatch_results match;
                                if (std::regex_search(
                                    static_cast<const wchar_t*> (value.bstrVal),
                                    static_cast<const wchar_t*> (value.bstrVal + SysStringLen(value.bstrVal)),
                                    match,
                                    regex))
                                {
                                    data->szClockNumber =
                                        std::make_unique<STLADD string_type>(match[1].first, match[1].second);
                                }
                                else
                                {
                                    data->szClockNumber = std::make_unique<STLADD string_type>(0, TEXT('\x00'));
                                }
                            }
                            ids.insert(std::make_pair(szKey, std::move(data)));
                        }
                        identifierNode.Release();
                    }
                    staffNode.Release();
                }
            }
        }
    }
    return ids;
}