2013-01-23 47 views
0

我想写一个检查实体是否支持“创建”操作的xpath表达式。 这是我正在使用的代码:xpath表达式来选择具有特定字符串作为文本的父节点的子节点

在这种情况下,字符串行的实体名称不带下划线,如'TciProfile'。

while ((line = br.readLine()) != null) { 
     System.out.println("entity name: "+line); 
     XPathFactory factory = XPathFactory.newInstance(); 
     XPath xpath1 = factory.newXPath(); 

     XPathExpression expr = xpath1.compile("//Entity[matches(Name,'" +line+ "') and .//Operation/Name='create']"); 

    Object result = expr.evaluate(doc, XPathConstants.NODESET); 
     NodeList res = (NodeList) result; 
     System.out.println("NodeList size: " + res.getLength()); 

其中在这种情况下的变量行将是TciProfile

但是,当评估时,此表达式重新生成null值。

有人能告诉我我在做什么错吗?

<Entity> 
<Name>TCI_Profile|TciProfile</Name> 
<AttributeList> 
    <Attribute> 
     <Name>Tci_Profile_Id|tciProfileId</Name> 
     <Type>string|string</Type> 
     <Range>0-23</Range> 
     <Default>null</Default> 
    </Attribute> 
    <Attribute> 
     <Name>Description|description</Name> 
     <Type>string|string</Type> 
     <Range>0-199</Range> 
     <Default>null</Default> 
    </Attribute> 
</AttributeList> 
<KeyAttributeNameList> 
    <Name>Tci_Profile_Id|tciProfileId</Name> 
</KeyAttributeNameList> 
<OperationList> 
    <Operation> 
     <Name>show|retrieve</Name> 
     <RequestAttributeList> 
      <RequestAttribute> 
       <Name>Tci_Profile_Id|tciProfileId</Name> 
       <Flag>required</Flag> 
      </RequestAttribute> 
     </RequestAttributeList> 
     <ResponseAttributeNameList> 
      <Name>Tci_Profile_Id|tciProfileId</Name> 
      <Name>Description|description</Name> 
     </ResponseAttributeNameList> 
    </Operation> 
    <Operation> 
     <Name>create</Name> 
     <RequestAttributeList> 
      <RequestAttribute> 
       <Name>Tci_Profile_Id|tciProfileId</Name> 
       <Flag>required</Flag> 
      </RequestAttribute> 
      <RequestAttribute> 
       <Name>Description|description</Name> 
       <Flag>required</Flag> 
      </RequestAttribute> 
     </RequestAttributeList> 
     <ResponseAttributeNameList> 
     </ResponseAttributeNameList> 
    </Operation> 
    <Operation> 
     <Name>find|getNextItems</Name> 
     <RequestAttributeList> 
      <RequestAttribute> 
       <Name>Tci_Profile_Id|tciProfileId</Name> 
       <Flag>optional/searchable</Flag> 
      </RequestAttribute> 
     </RequestAttributeList> 
     <ResponseAttributeNameList> 
      <Name>Tci_Profile_Id|tciProfileId</Name> 
     </ResponseAttributeNameList> 
    </Operation> 
    <Operation> 
     <Name>delete</Name> 
     <RequestAttributeList> 
      <RequestAttribute> 
       <Name>Tci_Profile_Id|tciProfileId</Name> 
       <Flag>required</Flag> 
      </RequestAttribute> 
     </RequestAttributeList> 
     <ResponseAttributeNameList> 
     </ResponseAttributeNameList> 
    </Operation> 
    <Operation> 
     <Name>put</Name> 
     <RequestAttributeList> 
      <RequestAttribute> 
       <Name>Tci_Profile_Id|tciProfileId</Name> 
       <Flag>required</Flag> 
      </RequestAttribute> 
      <RequestAttribute> 
       <Name>Description|description</Name> 
       <Flag>required</Flag> 
      </RequestAttribute> 
     </RequestAttributeList> 
     <ResponseAttributeNameList> 
     </ResponseAttributeNameList> 
    </Operation> 
    <Operation> 
     <Name>update</Name> 
     <RequestAttributeList> 
      <RequestAttribute> 
       <Name>Tci_Profile_Id|tciProfileId</Name> 
       <Flag>required</Flag> 
      </RequestAttribute> 
      <RequestAttribute> 
       <Name>Description|description</Name> 
       <Flag>required</Flag> 
      </RequestAttribute> 
     </RequestAttributeList> 
     <ResponseAttributeNameList> 
     </ResponseAttributeNameList> 
    </Operation> 
</OperationList> 

+0

这不开始由于缺少引号而编译......请发布_real_代码。 –

回答

0

我想你想要的是

//Entity[matches(Name,'TciProfile') and .//Operation/Name='create'] 

在氧气/ XML测试,这个返回满足两个条件实体节点(S)

+0

当我使用一个变量而不是字符串时,它返回null。 //实体[匹配(名称,'“+行+”')和.//操作/名称='创建'] 其中行是TciProfile。 – jumov

+0

在String中创建XPath表达式并在使用它创建XPathExpression对象之前将其打印出来。 –

+0

:在帖子中添加了代码。 – jumov

相关问题