2012-05-17 168 views
1

XML節點我想根據屬性值

<!-- url path="/jsp/Admin_BetaSignup.jsp" roles="ZohoCampaignAdmin" authentication="optional" description="Page used to add the Beta users"> 
     <param name="zuid" xss="throwerror" max-len="300"/> 
</url --> 

我要選擇通過XPath的這個節點選擇一個註釋節點。我在java中使用下面的代碼。

Document document = DocumentBuilderFactory.newInstance() 
    .newDocumentBuilder() 
    .parse("/home/local/ZOHOCORP/bharathi-1397/build/AdventNet/Sas/webapps/zcadmin/WEB-INF/security.xml"); 
XPath xpath = XPathFactory.newInstance().newXPath(); 
System.out.println(
    xpath.evaluate("//comment()[@path='/jsp/Admin_BetaSignup.jsp']", 
    document,XPathConstants.NODE) 
); 

輸出:null。

爲什麼?

+0

我不認爲意見可以有屬性... – beerbajay

回答

1

使用

//comment()[contains(., 'path="/jsp/Admin_BetaSignup.jsp"')] 

XSLT - 基於驗證

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

<xsl:template match="/"> 
    <xsl:copy-of select= 
    "//comment() 
     [contains(., 'path=&quot;/jsp/Admin_BetaSignup.jsp&quot;')] 
    "/> 
</xsl:template> 
</xsl:stylesheet> 

當這種轉變是在下面的XML文檔應用:

<!-- url path="/jsp/Admin_BetaSignup.jsp" roles="ZohoCampaignAdmin" authentication="optional" description="Page used to add the Beta users">   <param name="zuid" xss="throwerror" max-len="300"/> </url --> 
<t> 
<!-- Another comment --> 
</t> 

通緝註釋節點被選中並複製到輸出:

<!-- url path="/jsp/Admin_BetaSignup.jsp" roles="ZohoCampaignAdmin" authentication="optional" description="Page used to add the Beta users">   <param name="zuid" xss="throwerror" max-len="300"/> </url --> 
3

註釋不是元素節點,它不包含屬性。所以你必須得到所有評論節點,然後解析它們。