首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >元素的最近祖先的xs:documentation节点

元素的最近祖先的xs:documentation节点
EN

Stack Overflow用户
提问于 2011-02-01 00:18:30
回答 2查看 418关注 0票数 3

我有一个wsdl文档,其摘录如下所示...

代码语言:javascript
复制
<xs:complexType name="CustomerNameType">
  <xs:annotation>
    <xs:documentation>Structure for customer name</xs:documentation>
  </xs:annotation>
  <xs:sequence>
    <xs:element name="FullName" minOccurs="0">
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:maxLength value="60"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
    <xs:element name="Forenames" minOccurs="0">
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:maxLength value="60"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
  </xs:sequence>
</xs:complexType>

我知道xs: element /@name,我想获得最近的xs:documentation元素。

使用上面的示例,我知道xs:element/@name = "FullName",并且我希望从最近的xs:documentation节点获得文本"Structure for customer name“!

我已经尝试更改了我在stackoverflow (和其他站点)上找到的几个示例,但它们都不起作用。典型:0)。

干杯。

谢谢你们的回答..。希望这能帮上忙。

代码语言:javascript
复制
public static string DecryptStupidCapsError(string sOriginalErrorMessage)
{
    string sProblem = sOriginalErrorMessage.Substring(sOriginalErrorMessage.IndexOf("---> System.Xml.Schema.XmlSchemaException: ") + "---> System.Xml.Schema.XmlSchemaException: ".Length);
    sProblem = sProblem.Substring(0, sProblem.IndexOf("An error occurred"));

    string sElementName = sProblem.Substring(sProblem.IndexOf(":") + 1);
    sElementName = sElementName.Substring(sElementName.IndexOf(":") + 1);
    sElementName = sElementName.Substring(0, sElementName.IndexOf("'"));

    XmlDocument xd = new XmlDocument();
    xd.LoadXml(Properties.Resources.ServiceRequest_Service_74b1);

    XmlNamespaceManager xnsm = new XmlNamespaceManager(xd.NameTable);
    XPathDocument x = new XPathDocument(new StringReader(Properties.Resources.ServiceRequest_Service_74b1));
    XPathNavigator foo = x.CreateNavigator();
    foo.MoveToFollowing(XPathNodeType.Element);
    IDictionary<string, string> whatever = foo.GetNamespacesInScope(XmlNamespaceScope.All);

    foreach (KeyValuePair<string, string> xns in whatever)
    {
        xnsm.AddNamespace(xns.Key, xns.Value);
    }

    XmlNodeList xnl = xd.SelectNodes("//xs:element[@name='" + sElementName + "']", xnsm);

    StringBuilder sb = new StringBuilder();

    sb.AppendLine("CAPS has reported a (cryptic) error whilst validating the data you entered.");
    sb.AppendLine();
    sb.AppendLine("The following summary should enable you to determine what has caused the '" + sElementName + "' data to be invalid.");
    sb.AppendLine("----------");

    string sLast = string.Empty;
    foreach (XmlElement xe in xnl)
    {
        StringBuilder sbLast = new StringBuilder();
        XmlElement xeDocumentation = (XmlElement)xe.OwnerDocument.SelectSingleNode("(//xs:element[@name='" + sElementName + "']/ancestor-or-self::*/xs:annotation/xs:documentation)[last()]", xnsm);
        if (xeDocumentation.InnerText == sLast) continue;

        sbLast.AppendLine(sElementName + " AKA " + xeDocumentation.InnerText + ": ");
        sbLast.AppendLine("has the following validation rules:");
        XDocument xdoc = XDocument.Parse(xe.OuterXml);
        sbLast.AppendLine(xdoc.ToString());
        sbLast.AppendLine("----------");

        sb.AppendLine(sbLast.ToString());
        sLast = xeDocumentation.InnerText;
    }


    return sb.ToString();
}

基本上,wsdl = and (),而Properties.Resources.ServiceRequest_Service_74b1是验证数据所依据的sOriginalErrorMessage。此函数(缺少正则表达式!)为用户提供更好的线索,告诉他们是什么导致了验证失败,而不是旧的XmlSchemaException。

再次感谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-02-01 01:15:49

下面的XPATH将返回指定元素的文档和所有父元素的文档。我认为Alejandro的答案可能会返回兄弟的文档,而您可能对不同的模式不太感兴趣。

代码语言:javascript
复制
(//xs:element[@name='orderRequest']
     /ancestor-or-self::*
         /xs:annotation
             /xs:documentation)[last()]
票数 2
EN

Stack Overflow用户

发布于 2011-02-01 00:29:56

@name等于"FullName"xs:element元素没有xs:documentation 祖先元素,而是先于元素。

因此,您可以使用:

代码语言:javascript
复制
//xs:element[@name='FullName']/preceding::xs:documentation[1]

注意::我使用了起始//操作符,因为我不知道您想要深入到模式的多深层次。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4853204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档