2015-05-12 28 views
1

我有以下XML结构:MATLAB - XML查找同级元素

<LMS> 
<StressGradientCorrection> 
     <Gradients> 
      <Gradient> 
       <Curve> 
        <Point> 
         <NormalizedGradient Value="0.01" /> 
         <ReductionFactor Value="1" /> 
        </Point> 
        <Point> 
         <NormalizedGradient Value="0.1" /> 
         <ReductionFactor Value="1" /> 
        </Point> 
        <Point> 
         <NormalizedGradient Value="1" /> 
         <ReductionFactor Value="1" /> 
        </Point> 
        <Point> 
         <NormalizedGradient Value="10" /> 
         <ReductionFactor Value="1" /> 
        </Point> 
       </Curve> 
      </Gradient> 
     </Gradients> 
    </StressGradientCorrection> 
</LMS> 

我需要改变每个<ReductionFactor>的价值的基础上,<NormalizedGadient>在同一<Point>值。到目前为止,我只能根据它的属性区分元素,如名称或值,但在这里我不能。如何根据兄弟元素的值区分元素?

这里是代码的开始部分:

clear all 
close all 
clc 

% Import the XPath classes 
import javax.xml.xpath.* 

% Construct the DOM. 
doc = xmlread('SGC_EXAMPLE.xml'); 

% Creating an xPath 
factory = javax.xml.xpath.XPathFactory.newInstance(); 
xpath = factory.newXPath(); 

Path = xpath.compile('/LMS/StressGradientCorrection/Gradients/Gradient/Curve/Point/NormalizedGradient[@Value="0.01"]'); 
List = Path.evaluate(doc, XPathConstants.NODESET); 
Sibling = List.item(0); 

% YOUR PROPOSAL HERE 

MyNewlyFoundVariable.setAttribute('Value','5') 

xmlwrite('Final.xml',doc); 

到目前为止,我只知道如何找到同级基于它的价值。

回答

2

您可以找到下面的兄弟这样的:

/LMS/StressGradientCorrection/Gradients/Gradient/Curve/Point/NormalizedGradient[@Value="0.01"]/following-sibling::ReductionFactor/@Value 
+0

谢谢!奇迹般有效。你只是在我的初始文章中发现了其中一个错误:“=”而不是“==” – Trenera

+0

是的,已更正:) – flafoux