当属性已声明为父属性的属性时,使用 XPath 匹配名称采用 ParentElement.Property 格式的元素



我有一个XML文件,如下所示:

<Main>
    <Element1 Property1="Hello" Property3="world">
        <Element1.Property2>again</Element1.Property2>
        <Element1.Property3>Dave</Element1.Property3>
    </Element1>
    <Element2 Property1="Hello" Property3="world">
        <Element2.Property2>again</Element2.Property2>
        <Element2.Property1>
            <SpecialElementForSayingHi/>
        </Element2.Property1>
    </Element2>
</Main>

我需要使用 XPath 匹配以下元素 - 除非有办法禁止它们使用架构存在,但我认为没有:

<Element1.Property3>Dave</Element1.Property3>
...
<Element2.Property1>
    <SpecialElementForSayingHi/>
</Element2.Property1>

具体来说,我需要匹配元素名称格式为以下格式的所有元素:

ParentElementName.NameOfAttributeThatExistsOnTheParentElement

我正在使用 .Net,宁愿不使用外部库,所以如果可以使用 XPath 1.0 实现这一点,那将是理想的。如果它的效率要高得多,我愿意使用与重复属性而不是元素匹配的系统。

编辑:实际上没有问题。我该怎么做?

我尝试用XPAth 1.0做这件事没有成功,也许可以用XPath 2或XQuery做。在 .NET 中,最好使用 LINQ:

var xml = @"
    <Main>
         <Element1 Property1=""Hello"" Property3=""world"">
             <Element1.Property2>again</Element1.Property2>
             <Element1.Property3>Dave</Element1.Property3>
         </Element1>
         <Element2 Property1=""Hello"" Property3=""world"">
             <Element2.Property2>again</Element2.Property2>
             <Element2.Property1>
                 <SpecialElementForSayingHi/>
             </Element2.Property1>
         </Element2>
   </Main>";
   var doc = XDocument.Load(new StringReader(xml));
   var result = doc.Root.Descendants().
            Where(x => x.Parent.Attributes().
                                Any(y => x.Name == x.Parent.Name + "." + y.Name)
            );

如果你想得到字符串作为结果,你可以这样做

   var result = doc.Root.Descendants().
            Where(x => x.Parent.Attributes().
                                Any(y => x.Name == x.Parent.Name + "." + y.Name)
            ).Select(e => e.ToString()).Aggregate((current, next) => current + next);
您可以使用

LINQ2XML

XElement doc=XElement.Load("yourXML.xml");
foreach(XElement elm in doc.Elements())
{
    var attr=elm.Attributes();
     foreach(var elm1 in elm.Elements())
     {
          if(attr.Any(x=>elm1.Name.ToString().EndsWith(x.Name.ToString())) 
              elm1.ToString();//this contains your required XML
     }
}

这在 XPath 1.0 中是不可能的,因为它缺少范围变量或将函数指定为位置步骤的功能。

下面是一个 XPath 2.0 解决方案

/*/*/*
    [substring-before(name(), '.') eq name(..)
   and
     substring-after(name(), '.') = ../@*/name()
    ]

基于 XSLT 2.0 的验证:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:template match="/">
  <xsl:sequence select=
  "/*/*/*
    [substring-before(name(), '.') eq name(..)
   and
     substring-after(name(), '.') = ../@*/name()
    ]"/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时:

<Main>
    <Element1 Property1="Hello" Property3="world">
        <Element1.Property2>again</Element1.Property2>
        <Element1.Property3>Dave</Element1.Property3>
    </Element1>
    <Element2 Property1="Hello" Property3="world">
        <Element2.Property2>again</Element2.Property2>
        <Element2.Property1>
            <SpecialElementForSayingHi/>
        </Element2.Property1>
    </Element2>
</Main>
计算

XPath 表达式,并将此计算的结果复制到输出中:

<Element1.Property3>Dave</Element1.Property3>
<Element2.Property1>
            <SpecialElementForSayingHi/>
        </Element2.Property1>

最新更新