JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java & XML、Web Service  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 請教 dom4j 的 xpath 問題
ShunYi





發文: 45
積分: 0
於 2004-09-09 11:05 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
在執行這個程式的時候如果 sample.xml 的 <html> 有設 xmlns="http://www.w3.org/1999/xhtml" 這個 attribute 的話,
就沒辦法用 xpath 找出 <submit>,請問是為什麼呢?

TestDom4j.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import org.dom4j.*;
import org.dom4j.io.*;
import junit.framework.*;
 
public class TestDom4j extends TestCase {
 
  public static void main(String[] args) {
    junit.textui.TestRunner.run(TestDom4j.class);
  }
 
  public void testXPath() {
    try {
      SAXReader reader = new SAXReader();
      Document doc = reader.read(getClass().getResource("sample.xml"));
 
      Node node = doc.selectSingleNode("//submit"); //這行會出錯
      assertNotNull("node is null.", node);
 
      Element test = (Element)node;
      test.addElement("test").addAttribute("name", "test");
 
      String xml = doc.asXML();
      System.out.println(xml);
    }
    catch (DocumentException ex) {
      System.err.println(ex);
    }
  }
}

sample.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:xforms="http://www.w3.org/2002/01/xforms"
      xmlns:xlink="http://www.w3.org/1999/xlink"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      xmlns:my="http://commerce.example.com/payment"
      xml:lang="en">
  <head>
    <title>XForms in XHTML</title>
 
    <xforms:model>
      <xforms:instance>
        <payment as="credit" xmlns="http://commerce.example.com/payment">
          <cc/>
          <exp/>
        </payment>
      </xforms:instance>
      <xforms:schema xlink:href="payschema.xsd"/>
      <xforms:submitInfo action="http://example.com/submit" method="post" id="s00"/>
      <xforms:bind ref="my:payment/my:cc"
                   relevant="../my:payment/@as = 'credit'"
                   required="true" type="my:cc"/>
      <xforms:bind ref="my:payment/my:exp"
                   relevant="../my:payment/@as = 'credit'"
                   required="true" type="xsd:gYearMonth"/>
    </xforms:model>
  </head>
  <body>
    <group>
      <selectOne ref="@as">
        <caption>Select Payment Method</caption>
        <choices>
          <item>
            <caption>Cash</caption>
            <value>cash</value>
          </item>
          <item>
            <caption>Credit</caption>
            <value>credit</value>
          </item>
        </choices>
      </selectOne>
 
      <input ref="my:cc">
        <caption>Credit Card Number</caption>
      </input>
 
      <input ref="my:exp">
        <caption>Expiration Date</caption>
      </input>
 
      <submit submitInfo="s00">
        <caption>Submit Form</caption>
      </submit>
    </group>
  </body>
</html>


reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
5740 請教 dom4j 的 xpath 問題 ShunYi 2628 2004-09-09 11:05
3357 Re:請教 dom4j 的 xpath 問題 linexpmail 187 2004-09-09 13:10
3496 Re:請教 dom4j 的 xpath 問題 ShunYi 289 2004-09-12 14:53
» JWorld@TW »  Java & XML、Web Service

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8