2012-01-26 54 views
0

序列我碰到了,指出錯誤中的String「的XPath失敗,原因是:多個項目的順序是不允許的包含的第一個參數()(‘字符串1’ ,'string2')「當我試圖使用包含在序列中查找一個字符串。試圖找到字符串使用XPath

我的代碼與解決我的其他問題 https://stackoverflow.com/questions/9006479/with-pmd-and-xpath-can-i-determine-if-two-node-sets-have-any-node-in-common

Expression/PrimaryExpression/PrimaryPrefix 
[ 
contains(ancestor::ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/FieldDeclaration/VariableDeclarator/VariableDeclaratorId/@Image,./Name/@Image) 
] 

我就遇到了這個問題,因爲VariableDeclaratorId/@圖片返回當有多個字段來分析兩個String。我還沒有找到解決方案來解決這個問題。我想過使用謂詞,但這是包含謂詞的內部則已,做VariableDeclaratorId/@圖像[./名稱/ @圖片]是行不通的,因爲這種[./Name/@Image]不同於PrimaryPrefix /姓名/ @圖片。

預期輸出:

<Expression BeginColumn="26" BeginLine="6" EndColumn="30" EndLine="6" Image="" Label=""> 
     <PrimaryExpression BeginColumn="26" BeginLine="6" EndColumn="30" EndLine="6" Image="" Label=""> 
      <PrimaryPrefix BeginColumn="26" BeginLine="6" EndColumn="30" EndLine="6" Image="" Label="" SuperModifier="false" ThisModifier="false"> 
       <Name BeginColumn="26" BeginLine="6" EndColumn="30" EndLine="6" Image="m_foo" Label=""/> 
      </PrimaryPrefix> 
     </PrimaryExpression> 
    </Expression> 


    <Expression BeginColumn="26" BeginLine="7" EndColumn="30" EndLine="7" Image="" Label=""> 
     <PrimaryExpression BeginColumn="26" BeginLine="7" EndColumn="30" EndLine="7" Image="" Label=""> 
      <PrimaryPrefix BeginColumn="26" BeginLine="7" EndColumn="30" EndLine="7" Image="" Label="" SuperModifier="false" ThisModifier="false"> 
       <Name BeginColumn="26" BeginLine="7" EndColumn="30" EndLine="7" Image="m_two" Label=""/> 
      </PrimaryPrefix> 
     </PrimaryExpression> 
    </Expression> 

的Jave來源:

public class domainFunction { 
    private int m_foo; // OK 

    public void bar() { 
     calculate (random); 
     System.out.println(m_foo); 
     System.out.println(m_two); 
    } 
    } 

RAW XML:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
    <CompilationUnit BeginColumn="1" BeginLine="1" EndColumn="3" EndLine="13" Image="" Label="" declarationsAreInDefaultPackage="true"> 
     <TypeDeclaration BeginColumn="1" BeginLine="1" EndColumn="3" EndLine="13" Image="" Label=""> 
      <ClassOrInterfaceDeclaration Abstract="false" BeginColumn="8" BeginLine="1" EndColumn="3" EndLine="13" Final="false" Image="domainFunction" Interface="false" Label="" Modifiers="1" Native="false" Nested="false" PackagePrivate="false" Private="false" Protected="false" Public="true" Static="false" Strictfp="false" Synchronized="false" Transient="false" Volatile="false"> 
       <ClassOrInterfaceBody BeginColumn="29" BeginLine="1" EndColumn="3" EndLine="13" Image="" Label=""> 
        <ClassOrInterfaceBodyDeclaration AnonymousInnerClass="false" BeginColumn="5" BeginLine="2" EndColumn="22" EndLine="2" EnumChild="false" Image="" Label=""> 
         <FieldDeclaration Abstract="false" Array="false" ArrayDepth="0" BeginColumn="13" BeginLine="2" EndColumn="22" EndLine="2" Final="false" Image="" InterfaceMember="false" Label="" Modifiers="4" Native="false" PackagePrivate="false" Private="true" Protected="false" Public="false" Static="false" Strictfp="false" Synchronized="false" SyntacticallyFinal="false" SyntacticallyPublic="false" SyntacticallyStatic="false" Transient="false" VariableName="m_foo" Volatile="false"> 
          <Type Array="false" ArrayDepth="0" BeginColumn="13" BeginLine="2" EndColumn="15" EndLine="2" Image="" Label="" TypeImage="int"> 
           <PrimitiveType Array="false" ArrayDepth="0" BeginColumn="13" BeginLine="2" Boolean="false" EndColumn="15" EndLine="2" Image="int" Label=""/> 
          </Type> 
          <VariableDeclarator BeginColumn="17" BeginLine="2" EndColumn="21" EndLine="2" Image="" Label=""> 
           <VariableDeclaratorId Array="false" ArrayDepth="0" BeginColumn="17" BeginLine="2" EndColumn="21" EndLine="2" ExceptionBlockParameter="false" Image="m_foo" Label=""/> 
          </VariableDeclarator> 
         </FieldDeclaration> 
        </ClassOrInterfaceBodyDeclaration> 
        <ClassOrInterfaceBodyDeclaration AnonymousInnerClass="false" BeginColumn="5" BeginLine="3" EndColumn="22" EndLine="3" EnumChild="false" Image="" Label=""> 
         <FieldDeclaration Abstract="false" Array="false" ArrayDepth="0" BeginColumn="13" BeginLine="3" EndColumn="22" EndLine="3" Final="false" Image="" InterfaceMember="false" Label="" Modifiers="4" Native="false" PackagePrivate="false" Private="true" Protected="false" Public="false" Static="false" Strictfp="false" Synchronized="false" SyntacticallyFinal="false" SyntacticallyPublic="false" SyntacticallyStatic="false" Transient="false" VariableName="m_two" Volatile="false"> 
          <Type Array="false" ArrayDepth="0" BeginColumn="13" BeginLine="3" EndColumn="15" EndLine="3" Image="" Label="" TypeImage="int"> 
           <PrimitiveType Array="false" ArrayDepth="0" BeginColumn="13" BeginLine="3" Boolean="false" EndColumn="15" EndLine="3" Image="int" Label=""/> 
          </Type> 
          <VariableDeclarator BeginColumn="17" BeginLine="3" EndColumn="21" EndLine="3" Image="" Label=""> 
           <VariableDeclaratorId Array="false" ArrayDepth="0" BeginColumn="17" BeginLine="3" EndColumn="21" EndLine="3" ExceptionBlockParameter="false" Image="m_two" Label=""/> 
          </VariableDeclarator> 
         </FieldDeclaration> 
        </ClassOrInterfaceBodyDeclaration> 
        <ClassOrInterfaceBodyDeclaration AnonymousInnerClass="false" BeginColumn="5" BeginLine="4" EndColumn="5" EndLine="11" EnumChild="false" Image="" Label=""> 
         <MethodDeclaration Abstract="false" BeginColumn="12" BeginLine="4" EndColumn="5" EndLine="11" Final="false" Image="" InterfaceMember="false" Label="" MethodName="bar" Modifiers="1" Native="false" PackagePrivate="false" Private="false" Protected="false" Public="true" Static="false" Strictfp="false" Synchronized="false" SyntacticallyAbstract="false" SyntacticallyPublic="true" Transient="false" Void="true" Volatile="false"> 
          <ResultType BeginColumn="12" BeginLine="4" EndColumn="15" EndLine="4" Image="" Label="" Void="true" returnsArray="false"/> 
          <MethodDeclarator BeginColumn="17" BeginLine="4" EndColumn="21" EndLine="4" Image="bar" Label="" ParameterCount="0"> 
           <FormalParameters BeginColumn="20" BeginLine="4" EndColumn="21" EndLine="4" Image="" Label="" ParameterCount="0"/> 
          </MethodDeclarator> 
          <Block BeginColumn="23" BeginLine="4" EndColumn="5" EndLine="11" Image="" Label="" containsComment="false"> 
           <BlockStatement Allocation="false" BeginColumn="7" BeginLine="5" EndColumn="22" EndLine="5" Image="" Label=""> 
            <LocalVariableDeclaration Abstract="false" Array="false" ArrayDepth="0" BeginColumn="7" BeginLine="5" EndColumn="21" EndLine="5" Final="false" Image="" Label="" Modifiers="0" Native="false" PackagePrivate="true" Private="false" Protected="false" Public="false" Static="false" Strictfp="false" Synchronized="false" Transient="false" VariableName="random" Volatile="false"> 
             <Type Array="false" ArrayDepth="0" BeginColumn="7" BeginLine="5" EndColumn="9" EndLine="5" Image="" Label="" TypeImage="int"> 
              <PrimitiveType Array="false" ArrayDepth="0" BeginColumn="7" BeginLine="5" Boolean="false" EndColumn="9" EndLine="5" Image="int" Label=""/> 
             </Type> 
             <VariableDeclarator BeginColumn="11" BeginLine="5" EndColumn="21" EndLine="5" Image="" Label=""> 
              <VariableDeclaratorId Array="false" ArrayDepth="0" BeginColumn="11" BeginLine="5" EndColumn="16" EndLine="5" ExceptionBlockParameter="false" Image="random" Label=""/> 
              <VariableInitializer BeginColumn="20" BeginLine="5" EndColumn="21" EndLine="5" Image="" Label=""> 
               <Expression BeginColumn="20" BeginLine="5" EndColumn="21" EndLine="5" Image="" Label=""> 
                <PrimaryExpression BeginColumn="20" BeginLine="5" EndColumn="21" EndLine="5" Image="" Label=""> 
                 <PrimaryPrefix BeginColumn="20" BeginLine="5" EndColumn="21" EndLine="5" Image="" Label="" SuperModifier="false" ThisModifier="false"> 
                  <Literal BeginColumn="20" BeginLine="5" CharLiteral="false" EndColumn="21" EndLine="5" FloatLiteral="false" Image="12" IntLiteral="true" Label="" SingleCharacterStringLiteral="false" StringLiteral="false"/> 
                 </PrimaryPrefix> 
                </PrimaryExpression> 
               </Expression> 
              </VariableInitializer> 
             </VariableDeclarator> 
            </LocalVariableDeclaration> 
           </BlockStatement> 
           <BlockStatement Allocation="false" BeginColumn="7" BeginLine="6" EndColumn="25" EndLine="6" Image="" Label=""> 
            <Statement BeginColumn="7" BeginLine="6" EndColumn="25" EndLine="6" Image="" Label=""> 
             <StatementExpression BeginColumn="7" BeginLine="6" EndColumn="24" EndLine="6" Image="" Label=""> 
              <PrimaryExpression BeginColumn="7" BeginLine="6" EndColumn="24" EndLine="6" Image="" Label=""> 
               <PrimaryPrefix BeginColumn="7" BeginLine="6" EndColumn="15" EndLine="6" Image="" Label="" SuperModifier="false" ThisModifier="false"> 
                <Name BeginColumn="7" BeginLine="6" EndColumn="15" EndLine="6" Image="calculate" Label=""/> 
               </PrimaryPrefix> 
               <PrimarySuffix ArgumentCount="1" Arguments="true" ArrayDereference="false" BeginColumn="17" BeginLine="6" EndColumn="24" EndLine="6" Image="" Label=""> 
                <Arguments ArgumentCount="1" BeginColumn="17" BeginLine="6" EndColumn="24" EndLine="6" Image="" Label=""> 
                 <ArgumentList BeginColumn="18" BeginLine="6" EndColumn="23" EndLine="6" Image="" Label=""> 
                  <Expression BeginColumn="18" BeginLine="6" EndColumn="23" EndLine="6" Image="" Label=""> 
                   <PrimaryExpression BeginColumn="18" BeginLine="6" EndColumn="23" EndLine="6" Image="" Label=""> 
                    <PrimaryPrefix BeginColumn="18" BeginLine="6" EndColumn="23" EndLine="6" Image="" Label="" SuperModifier="false" ThisModifier="false"> 
                     <Name BeginColumn="18" BeginLine="6" EndColumn="23" EndLine="6" Image="random" Label=""/> 
                    </PrimaryPrefix> 
                   </PrimaryExpression> 
                  </Expression> 
                 </ArgumentList> 
                </Arguments> 
               </PrimarySuffix> 
              </PrimaryExpression> 
             </StatementExpression> 
            </Statement> 
           </BlockStatement> 
           <BlockStatement Allocation="false" BeginColumn="7" BeginLine="7" EndColumn="24" EndLine="7" Image="" Label=""> 
            <Statement BeginColumn="7" BeginLine="7" EndColumn="24" EndLine="7" Image="" Label=""> 
             <StatementExpression BeginColumn="7" BeginLine="7" EndColumn="23" EndLine="7" Image="" Label=""> 
              <PrimaryExpression BeginColumn="7" BeginLine="7" EndColumn="23" EndLine="7" Image="" Label=""> 
               <PrimaryPrefix BeginColumn="7" BeginLine="7" EndColumn="15" EndLine="7" Image="" Label="" SuperModifier="false" ThisModifier="false"> 
                <Name BeginColumn="7" BeginLine="7" EndColumn="15" EndLine="7" Image="calculate" Label=""/> 
               </PrimaryPrefix> 
               <PrimarySuffix ArgumentCount="1" Arguments="true" ArrayDereference="false" BeginColumn="17" BeginLine="7" EndColumn="23" EndLine="7" Image="" Label=""> 
                <Arguments ArgumentCount="1" BeginColumn="17" BeginLine="7" EndColumn="23" EndLine="7" Image="" Label=""> 
                 <ArgumentList BeginColumn="18" BeginLine="7" EndColumn="22" EndLine="7" Image="" Label=""> 
                  <Expression BeginColumn="18" BeginLine="7" EndColumn="22" EndLine="7" Image="" Label=""> 
                   <PrimaryExpression BeginColumn="18" BeginLine="7" EndColumn="22" EndLine="7" Image="" Label=""> 
                    <PrimaryPrefix BeginColumn="18" BeginLine="7" EndColumn="22" EndLine="7" Image="" Label="" SuperModifier="false" ThisModifier="false"> 
                     <Name BeginColumn="18" BeginLine="7" EndColumn="22" EndLine="7" Image="m_foo" Label=""/> 
                    </PrimaryPrefix> 
                   </PrimaryExpression> 
                  </Expression> 
                 </ArgumentList> 
                </Arguments> 
               </PrimarySuffix> 
              </PrimaryExpression> 
             </StatementExpression> 
            </Statement> 
           </BlockStatement> 
           <BlockStatement Allocation="false" BeginColumn="7" BeginLine="8" EndColumn="32" EndLine="8" Image="" Label=""> 
            <Statement BeginColumn="7" BeginLine="8" EndColumn="32" EndLine="8" Image="" Label=""> 
             <StatementExpression BeginColumn="7" BeginLine="8" EndColumn="31" EndLine="8" Image="" Label=""> 
              <PrimaryExpression BeginColumn="7" BeginLine="8" EndColumn="31" EndLine="8" Image="" Label=""> 
               <PrimaryPrefix BeginColumn="7" BeginLine="8" EndColumn="24" EndLine="8" Image="" Label="" SuperModifier="false" ThisModifier="false"> 
                <Name BeginColumn="7" BeginLine="8" EndColumn="24" EndLine="8" Image="System.out.println" Label=""/> 
               </PrimaryPrefix> 
               <PrimarySuffix ArgumentCount="1" Arguments="true" ArrayDereference="false" BeginColumn="25" BeginLine="8" EndColumn="31" EndLine="8" Image="" Label=""> 
                <Arguments ArgumentCount="1" BeginColumn="25" BeginLine="8" EndColumn="31" EndLine="8" Image="" Label=""> 
                 <ArgumentList BeginColumn="26" BeginLine="8" EndColumn="30" EndLine="8" Image="" Label=""> 
                  <Expression BeginColumn="26" BeginLine="8" EndColumn="30" EndLine="8" Image="" Label=""> 
                   <PrimaryExpression BeginColumn="26" BeginLine="8" EndColumn="30" EndLine="8" Image="" Label=""> 
                    <PrimaryPrefix BeginColumn="26" BeginLine="8" EndColumn="30" EndLine="8" Image="" Label="" SuperModifier="false" ThisModifier="false"> 
                     <Name BeginColumn="26" BeginLine="8" EndColumn="30" EndLine="8" Image="m_foo" Label=""/> 
                    </PrimaryPrefix> 
                   </PrimaryExpression> 
                  </Expression> 
                 </ArgumentList> 
                </Arguments> 
               </PrimarySuffix> 
              </PrimaryExpression> 
             </StatementExpression> 
            </Statement> 
           </BlockStatement> 
           <BlockStatement Allocation="false" BeginColumn="7" BeginLine="9" EndColumn="32" EndLine="9" Image="" Label=""> 
            <Statement BeginColumn="7" BeginLine="9" EndColumn="32" EndLine="9" Image="" Label=""> 
             <StatementExpression BeginColumn="7" BeginLine="9" EndColumn="31" EndLine="9" Image="" Label=""> 
              <PrimaryExpression BeginColumn="7" BeginLine="9" EndColumn="31" EndLine="9" Image="" Label=""> 
               <PrimaryPrefix BeginColumn="7" BeginLine="9" EndColumn="24" EndLine="9" Image="" Label="" SuperModifier="false" ThisModifier="false"> 
                <Name BeginColumn="7" BeginLine="9" EndColumn="24" EndLine="9" Image="System.out.println" Label=""/> 
               </PrimaryPrefix> 
               <PrimarySuffix ArgumentCount="1" Arguments="true" ArrayDereference="false" BeginColumn="25" BeginLine="9" EndColumn="31" EndLine="9" Image="" Label=""> 
                <Arguments ArgumentCount="1" BeginColumn="25" BeginLine="9" EndColumn="31" EndLine="9" Image="" Label=""> 
                 <ArgumentList BeginColumn="26" BeginLine="9" EndColumn="30" EndLine="9" Image="" Label=""> 
                  <Expression BeginColumn="26" BeginLine="9" EndColumn="30" EndLine="9" Image="" Label=""> 
                   <PrimaryExpression BeginColumn="26" BeginLine="9" EndColumn="30" EndLine="9" Image="" Label=""> 
                    <PrimaryPrefix BeginColumn="26" BeginLine="9" EndColumn="30" EndLine="9" Image="" Label="" SuperModifier="false" ThisModifier="false"> 
                     <Name BeginColumn="26" BeginLine="9" EndColumn="30" EndLine="9" Image="m_two" Label=""/> 
                    </PrimaryPrefix> 
                   </PrimaryExpression> 
                  </Expression> 
                 </ArgumentList> 
                </Arguments> 
               </PrimarySuffix> 
              </PrimaryExpression> 
             </StatementExpression> 
            </Statement> 
           </BlockStatement> 
           <BlockStatement Allocation="false" BeginColumn="7" BeginLine="10" EndColumn="28" EndLine="10" Image="" Label=""> 
            <Statement BeginColumn="7" BeginLine="10" EndColumn="28" EndLine="10" Image="" Label=""> 
             <StatementExpression BeginColumn="7" BeginLine="10" EndColumn="27" EndLine="10" Image="" Label=""> 
              <PrimaryExpression BeginColumn="7" BeginLine="10" EndColumn="27" EndLine="10" Image="" Label=""> 
               <PrimaryPrefix BeginColumn="7" BeginLine="10" EndColumn="10" EndLine="10" Image="" Label="this" SuperModifier="false" ThisModifier="true"/> 
               <PrimarySuffix ArgumentCount="" Arguments="false" ArrayDereference="false" BeginColumn="11" BeginLine="10" EndColumn="20" EndLine="10" Image="calculate" Label=""/> 
               <PrimarySuffix ArgumentCount="1" Arguments="true" ArrayDereference="false" BeginColumn="21" BeginLine="10" EndColumn="27" EndLine="10" Image="" Label=""> 
                <Arguments ArgumentCount="1" BeginColumn="21" BeginLine="10" EndColumn="27" EndLine="10" Image="" Label=""> 
                 <ArgumentList BeginColumn="22" BeginLine="10" EndColumn="26" EndLine="10" Image="" Label=""> 
                  <Expression BeginColumn="22" BeginLine="10" EndColumn="26" EndLine="10" Image="" Label=""> 
                   <PrimaryExpression BeginColumn="22" BeginLine="10" EndColumn="26" EndLine="10" Image="" Label=""> 
                    <PrimaryPrefix BeginColumn="22" BeginLine="10" EndColumn="26" EndLine="10" Image="" Label="" SuperModifier="false" ThisModifier="false"> 
                     <Name BeginColumn="22" BeginLine="10" EndColumn="26" EndLine="10" Image="m_foo" Label=""/> 
                    </PrimaryPrefix> 
                   </PrimaryExpression> 
                  </Expression> 
                 </ArgumentList> 
                </Arguments> 
               </PrimarySuffix> 
              </PrimaryExpression> 
             </StatementExpression> 
            </Statement> 
           </BlockStatement> 
          </Block> 
         </MethodDeclaration> 
        </ClassOrInterfaceBodyDeclaration> 
       </ClassOrInterfaceBody> 
      </ClassOrInterfaceDeclaration> 
     </TypeDeclaration> 
    </CompilationUnit> 
+0

你好,@ttback。你沒有得到答案(對於這個或你之前的問題)的原因是因爲你沒有寫出讓他們自己容易回答的問題。您應該始終提供輸入XML(您已經完成)以及您要根據該輸入查找的*精確*所需輸出。在這裏,您向我們展示了您收到的錯誤以及您嘗試過的一些隨機表達式,但是您從未告訴我們期望的輸出是什麼*並且*我們無法讀取頭腦*。 –

+0

@lwburk:我加入了預期的輸出,如果它可以更清楚。對不起,如果它不夠清楚,當我發佈這個時,我有點慌亂。我腦海裏有很多東西。 – ttback

回答

0

我認爲你正在使用contains()函數是因爲您錯誤地猜測它測試一組字符串是否包含給定的字符串。不要猜測,閱讀spec:那不是contains()所做的;相反,它測試一個字符串是否包含另一個字符串。要測試一組字符串$ S是否包含字符串$ T,請使用($ S = $ T)。

+0

謝謝。得到它解決。直到今晚我才發現這個規範。我在閱讀和猜測PMD的文檔方面浪費了太多時間,而沒有充分考慮XPath。另外,還不清楚哪個XPath規範PMD正在使用一個點。我花了一段時間才把這些基礎知識分類出來。再次感謝。 – ttback