[英]How to filter xml records by various values (combination filter) in js/.net
我有一個包含大約 1000 條記錄的 xml 文件,我需要能夠根據名稱、價格、顏色等各種參數過濾記錄,然后生成一個結果集,列出所有符合指定條件的記錄。 我可以使用的技術是 vb.net、javascript 和輸出將把 xml 結果集轉換為 html 以美化一般用戶消費的結果,並提供分頁結果以加快加載速度。
我看過 xpath 但大多數示例都非常有限,只返回特定字段而不是完整記錄,我還看過 json 路徑,因為我可以輕松地在 xml 和 json 之間進行轉換。
Json Path 適用於以下 xml 的 JSON 版本
$..Product[?(@.Gender.male == 'true' && @.Brand.isdesigner == 'true')]
哪個效果很好,但我想知道它們是否是一種更簡單的方法來完成這項工作並保持靈活性
xml 示例
<?xml version="1.0"?>
<catalog>
<Product>
<ID>1000</ID>
<ThumbImg>IMG.jpg</ThumbImg>
<PTID>0</PTID>
<BID>0</BID>
<name>Item name</name>
<type>Type name</type>
<price>0.00</price>
<discountprice>0.00</discountprice>
<Brand>
<Name>Brand name</Name>
<isdesigner>true</isdesigner>
<discount>0</discount>
<discounttype>percentage</discounttype>
</Brand>
<Gender>
<male>false</male>
<female>true</female>
</Gender>
<shape>Item shape</shape>
<style>Item style</style>
<colour>
<black>false</black>
<grey>false</grey>
<white>false</white>
<brown>false</brown>
<red>false</red>
<orange>false</orange>
<yellow>false</yellow>
<green>false</green>
<blue>true</blue>
<purple>false</purple>
<pink>false</pink>
<silver>false</silver>
<gold>false</gold>
<transparent>false</transparent>
</colour>
<material>2</material>
<weight>26</weight>
<sizes>
<size>52</size>
</sizes>
</Product>
</catalog>
例如,我希望能夠過濾部分名稱匹配、價格和品牌,但也可以包括任何屬性的過濾器並返回與過濾器選項匹配的記錄列表。
我正在使用平面文件來減少數據庫上的負載並允許緩存完整的產品列表用戶端以使系統快速響應,而無需服務器群的開銷,數據將在幾個小時后失效然后根據需要從服務器拉取最新版本(如果用戶仍在使用該網站)。
在 VB.Net 中,您可以使用 Linq for Xml,如下例所示。
Dim doc = <catalog>
<Product>
<ID>1000</ID>
<name>Item name</name>
<price>0.00</price>
<discountprice>0.00</discountprice>
<Brand>
<Name>Brand name</Name>
<isdesigner>true</isdesigner>
<discount>0</discount>
<discounttype>percentage</discounttype>
</Brand>
</Product>
</catalog>
Dim query = (From p In doc...<Product>
Where p.<Brand>.<Name>.Value.Contains("name") AndAlso
Convert.ToDecimal(p.<price>.Value) < 2.0
Select p)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.