none
请教 XPath 筛选~~!! RRS feed

  • 问题

  • 最近帮公司做了个Webservice ,,自己调用的时候,,遇到的问题...

    如何通过  Xpath 对下列  xml

    - <Table diffgr:id="Table1" msdata:rowOrder="0">
      <hotel_code>JD00061</hotel_code>
      <hotel_name>深圳威尼斯皇冠假日酒店</hotel_name>
      <roomtype>高级房</roomtype>
      <check_in>2009-05-12</check_in>
      <price>849</price>
      <breakfast>147</breakfast>
      <extra_bed>317</extra_bed>
      <remark />
      </Table>
    - <Table diffgr:id="Table2" msdata:rowOrder="1">
      <hotel_code>JD00061</hotel_code>
      <hotel_name>深圳威尼斯皇冠假日酒店</hotel_name>
      <roomtype>高级房</roomtype>
      <check_in>2009-05-13</check_in>
      <price>849</price>
      <breakfast>147</breakfast>
      <extra_bed>317</extra_bed>
      <remark />
      </Table>
    - <Table diffgr:id="Table3" msdata:rowOrder="2">
      <hotel_code>JD00061</hotel_code>
      <hotel_name>深圳威尼斯皇冠假日酒店</hotel_name>
      <roomtype>豪华房</roomtype>
      <check_in>2009-05-12</check_in>
      <price>1019</price>
      <breakfast>147</breakfast>
      <extra_bed>317</extra_bed>
      <remark />
      </Table>
    - <Table diffgr:id="Table4" msdata:rowOrder="3">
      <hotel_code>JD00061</hotel_code>
      <hotel_name>深圳威尼斯皇冠假日酒店</hotel_name>
      <roomtype>豪华房</roomtype>
      <check_in>2009-05-13</check_in>
      <price>1019</price>
      <breakfast>147</breakfast>
      <extra_bed>317</extra_bed>
      <remark />
      </Table>


    筛选为
    - <Table diffgr:id="Table1" msdata:rowOrder="0">
      <hotel_code>JD00061</hotel_code>
      <hotel_name>深圳威尼斯皇冠假日酒店</hotel_name>
      <roomtype>高级房</roomtype>
      <check_in>2009-05-12</check_in>
      <price>849</price>
      <breakfast>147</breakfast>
      <extra_bed>317</extra_bed>
      <remark />
      </Table>
    - <Table diffgr:id="Table3" msdata:rowOrder="2">
      <hotel_code>JD00061</hotel_code>
      <hotel_name>深圳威尼斯皇冠假日酒店</hotel_name>
      <roomtype>豪华房</roomtype>
      <check_in>2009-05-12</check_in>
      <price>1019</price>
      <breakfast>147</breakfast>
      <extra_bed>317</extra_bed>
      <remark />
      </Table>

    之前试过  distinct-values  根据   //Table/roomtype/text()  判断,,却提示"方法未知"错误~~!!
    其实我想达到的目的是要按 //Table/roomtype 分组显示,, 功能就类似 SQL 语句的  group by roomtype ~~
    请指教

    2009年5月13日 1:54

答案

  •  

    Dim xml = <root> _

     

    <Table id="Table1" rowOrder="0"> _

     

    <hotel_code>JD00061</hotel_code>_

     

    <hotel_name>深圳威尼斯皇冠假日酒店</hotel_name> _

     

    <roomtype>高级房</roomtype> _

     

    <check_in>20090512</check_in> _

     

    <price>849</price> _

     

    <breakfast>147</breakfast> _

     

    <extra_bed>317</extra_bed> _

     

    <remark/> _

     

    </Table> _

     

    <Table id="Table2" rowOrder="1"> _

     

    <hotel_code>JD00061</hotel_code>

     

    <hotel_name>深圳威尼斯皇冠假日酒店</hotel_name> _

     

    <roomtype>高级房</roomtype> _

     

    <check_in>20090513</check_in> _

     

    <price>849</price> _

     

    <breakfast>147</breakfast> _

     

    <extra_bed>317</extra_bed> _

     

    <remark/> _

     

    </Table> _

     

    <Table id="Table3" rowOrder="2"> _

     

    <hotel_code>JD00061</hotel_code> _

     

    <hotel_name>深圳威尼斯皇冠假日酒店</hotel_name> _

     

    <roomtype>豪华房</roomtype> _

     

    <check_in>20090512</check_in> _

     

    <price>1019</price> _

     

    <breakfast>147</breakfast> _

     

    <extra_bed>317</extra_bed> _

     

    <remark/> _

     

    </Table> _

     

    <Table id="Table4" rowOrder="3"> _

     

    <hotel_code>JD00061</hotel_code>

     

    <hotel_name>深圳威尼斯皇冠假日酒店</hotel_name> _

     

    <roomtype>豪华房</roomtype> _

     

    <check_in>20090513</check_in> _

     

    <price>1019</price> _

     

    <breakfast>147</breakfast> _

     

    <extra_bed>317</extra_bed> _

     

    <remark/> _

     

    </Table></root>

     

    Dim l = From e In xml...<Table> Group By e.<roomtype>.Value Into Group, Count()

     

    For Each g In l

     

    Dim tl = g.Group.ToArray

    Console.WriteLine(

    "group count {0} value {1}", g.Count, g.roomtype)

     

    For Each x In tl

    Console.WriteLine(

    "xml {0}", x.ToString)

     

    Next

    Console.WriteLine()

    Console.WriteLine()

    Console.WriteLine()

     

    Next

     

    Console.Read()

     

    修改了下    这次可以看每一组的分组条件


    工作突然有点忙 嘿嘿
    2009年5月13日 3:07

全部回复

  • 试过用    //Table[roomtype='高级房'][1] | //Table[roomtype='豪华房'][1]  这个Xpath 可以实现~~!
    但这个方法,,只能解决一部分而己,,因总共的房型有 几十种,,而且还分了  简体中文,繁体中文,英文  这几个语言显示的~~!!
    我想寻求一种比较科学的方法~~!!
    2009年5月13日 2:34
  • 可否用 linq to xml 呢? 

            Dim xml = <root> _
                        <Table id="Table1" rowOrder="0"> _
                              <hotel_code>JD00061</hotel_code>_
                              <hotel_name>深圳威尼斯皇冠假日酒店</hotel_name> _
                              <roomtype>高级房</roomtype> _
                              <check_in>20090512</check_in> _
                              <price>849</price> _
                              <breakfast>147</breakfast> _
                              <extra_bed>317</extra_bed> _
                              <remark/> _
                        </Table> _
                         <Table id="Table2" rowOrder="1"> _
                          <hotel_code>JD00061</hotel_code>
                              <hotel_name>深圳威尼斯皇冠假日酒店</hotel_name> _
                          <roomtype>高级房</roomtype> _
                          <check_in>20090513</check_in> _
                          <price>849</price> _
                          <breakfast>147</breakfast> _
                          <extra_bed>317</extra_bed> _
                          <remark/> _
                          </Table> _
                         <Table id="Table3" rowOrder="2"> _
                          <hotel_code>JD00061</hotel_code> _
                          <hotel_name>深圳威尼斯皇冠假日酒店</hotel_name> _
                          <roomtype>豪华房</roomtype> _
                          <check_in>20090512</check_in> _
                          <price>1019</price> _
                          <breakfast>147</breakfast> _
                          <extra_bed>317</extra_bed> _
                          <remark/> _
                          </Table> _
                         <Table id="Table4" rowOrder="3"> _
                          <hotel_code>JD00061</hotel_code>
                              <hotel_name>深圳威尼斯皇冠假日酒店</hotel_name> _
                          <roomtype>豪华房</roomtype> _
                          <check_in>20090513</check_in> _
                          <price>1019</price> _
                          <breakfast>147</breakfast> _
                          <extra_bed>317</extra_bed> _
                          <remark/> _
                          </Table></root>
            Dim l = From e In xml...<Table> Group By e.<roomtype>.Value Into Group Select Group
    
            For Each g In l
                Dim tl = g.ToArray
                Console.WriteLine("group count {0}", g.Count)
                For Each x In tl
                    Console.WriteLine("xml {0}", x.ToString)
                Next
                Console.WriteLine()
                Console.WriteLine()
                Console.WriteLine()
            Next
    
    
            Console.Read()
    
    

    工作突然有点忙 嘿嘿
    2009年5月13日 2:57
  • 运行结果

    group count 2
    xml <Table id="Table1" rowOrder="0"> _
                              <hotel_code>JD00061</hotel_code>_
                              <hotel_name>???????????</hotel_name> _
                              <roomtype>???</roomtype> _
                              <check_in>20090512</check_in> _
                              <price>849</price> _
                              <breakfast>147</breakfast> _
                              <extra_bed>317</extra_bed> _
                              <remark /> _
                        </Table>
    xml <Table id="Table2" rowOrder="1"> _
                          <hotel_code>JD00061</hotel_code><hotel_name>???????????</h
    otel_name> _
                          <roomtype>???</roomtype> _
                          <check_in>20090513</check_in> _
                          <price>849</price> _
                          <breakfast>147</breakfast> _
                          <extra_bed>317</extra_bed> _
                          <remark /> _
                          </Table>

     

    group count 2
    xml <Table id="Table3" rowOrder="2"> _
                          <hotel_code>JD00061</hotel_code> _
                          <hotel_name>???????????</hotel_name> _
                          <roomtype>???</roomtype> _
                          <check_in>20090512</check_in> _
                          <price>1019</price> _
                          <breakfast>147</breakfast> _
                          <extra_bed>317</extra_bed> _
                          <remark /> _
                          </Table>
    xml <Table id="Table4" rowOrder="3"> _
                          <hotel_code>JD00061</hotel_code><hotel_name>???????????</h
    otel_name> _
                          <roomtype>???</roomtype> _
                          <check_in>20090513</check_in> _
                          <price>1019</price> _
                          <breakfast>147</breakfast> _
                          <extra_bed>317</extra_bed> _
                          <remark /> _
                          </Table>


    我的console 是英文的  不好意思


    工作突然有点忙 嘿嘿
    2009年5月13日 2:58
  •  

    Dim xml = <root> _

     

    <Table id="Table1" rowOrder="0"> _

     

    <hotel_code>JD00061</hotel_code>_

     

    <hotel_name>深圳威尼斯皇冠假日酒店</hotel_name> _

     

    <roomtype>高级房</roomtype> _

     

    <check_in>20090512</check_in> _

     

    <price>849</price> _

     

    <breakfast>147</breakfast> _

     

    <extra_bed>317</extra_bed> _

     

    <remark/> _

     

    </Table> _

     

    <Table id="Table2" rowOrder="1"> _

     

    <hotel_code>JD00061</hotel_code>

     

    <hotel_name>深圳威尼斯皇冠假日酒店</hotel_name> _

     

    <roomtype>高级房</roomtype> _

     

    <check_in>20090513</check_in> _

     

    <price>849</price> _

     

    <breakfast>147</breakfast> _

     

    <extra_bed>317</extra_bed> _

     

    <remark/> _

     

    </Table> _

     

    <Table id="Table3" rowOrder="2"> _

     

    <hotel_code>JD00061</hotel_code> _

     

    <hotel_name>深圳威尼斯皇冠假日酒店</hotel_name> _

     

    <roomtype>豪华房</roomtype> _

     

    <check_in>20090512</check_in> _

     

    <price>1019</price> _

     

    <breakfast>147</breakfast> _

     

    <extra_bed>317</extra_bed> _

     

    <remark/> _

     

    </Table> _

     

    <Table id="Table4" rowOrder="3"> _

     

    <hotel_code>JD00061</hotel_code>

     

    <hotel_name>深圳威尼斯皇冠假日酒店</hotel_name> _

     

    <roomtype>豪华房</roomtype> _

     

    <check_in>20090513</check_in> _

     

    <price>1019</price> _

     

    <breakfast>147</breakfast> _

     

    <extra_bed>317</extra_bed> _

     

    <remark/> _

     

    </Table></root>

     

    Dim l = From e In xml...<Table> Group By e.<roomtype>.Value Into Group, Count()

     

    For Each g In l

     

    Dim tl = g.Group.ToArray

    Console.WriteLine(

    "group count {0} value {1}", g.Count, g.roomtype)

     

    For Each x In tl

    Console.WriteLine(

    "xml {0}", x.ToString)

     

    Next

    Console.WriteLine()

    Console.WriteLine()

    Console.WriteLine()

     

    Next

     

    Console.Read()

     

    修改了下    这次可以看每一组的分组条件


    工作突然有点忙 嘿嘿
    2009年5月13日 3:07