【问题标题】:Filter xml using xpath and linq (price with decimal separator and thousands separator spanish) VB. NET使用 xpath 和 linq 过滤 xml(带有小数分隔符和千位分隔符的西班牙文价格)VB。网
【发布时间】:2012-06-07 21:42:39
【问题描述】:

我正在尝试过滤 xml 文件的结果。我被要求过滤的字段之一是每个节点的属性价格。问题是 xml 来自西班牙来源,价格是小数点分隔符,而不是 .而千位分隔符是 , 而不是 .

我正在尝试过滤一个 xml 文件,其中仅显示价格低于提供的价格的节点。

xml结构为:

<RoomStays>
    <RoomStay InfoSource="" Price="201,60" Discount="0" MealPlan="Sólo alojamiento" MealPlanId="1" Provider="Marsol" ProviderId="3" HotelName="HOTEL LA ESTACION" Rating="" Latitud="" Longitud=""> 
    <RoomStay InfoSource="" Price="1.234,75" Discount="0" MealPlan="Sólo alojamiento" MealPlanId="1" Provider="Marsol" ProviderId="3" HotelName="HOTEL LA ESTACION" Rating="" Latitud="" Longitud=""> 
    ....More nodes
</RoomStays>

我尝试使用 XPath 和 Linq2Xml 进行过滤。我已经得出结论,我必须将 , 替换为 。和 。 ,但我得到 0 个结果。

如果只有 1000 以下的值,我可以使用:

doc.XPathSelectElements(./RoomStays/RoomStay[translate(@Price,',','.')<=256.78])

但是当价格超过 1000 并带有千位分隔符时。它不起作用,所以我尝试了:

doc.XPathSelectElements(./RoomStays/RoomStay[translate(translate(@Price,'.',''),',','.')<=256.78])

但它返回 0 个结果(我尝试使用 replace 函数而不是 translate 来删除 . 字符,但出现异常)。

所以我被困住了,如果有人可以帮助我,即使有另一种方法(不一定是 XPath)。

提前致谢。

【问题讨论】:

    标签: vb.net linq xpath


    【解决方案1】:

    使用

    translate(@Price, ',.', '.')
    

    这将删除任何'.' 并将任何',' 替换为'.'

    基于 XSLT 的验证

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="/">
         <xsl:copy-of select=
          "/RoomStays/RoomStay[not(translate(@Price, ',.', '.') > 256.78)]"/>
     </xsl:template>
    </xsl:stylesheet>
    

    当此转换应用于提供的 XML 文档时

    <RoomStays>
        <RoomStay InfoSource="" Price="201,60" Discount="0" MealPlan="Sólo alojamiento" MealPlanId="1" Provider="Marsol" ProviderId="3" HotelName="HOTEL LA ESTACION" Rating="" Latitud="" Longitud=""/>
        <RoomStay InfoSource="" Price="1.234,75" Discount="0" MealPlan="Sólo alojamiento" MealPlanId="1" Provider="Marsol" ProviderId="3" HotelName="HOTEL LA ESTACION" Rating="" Latitud="" Longitud=""/>
        ....More nodes
    </RoomStays>
    

    计算 XPath 表达式并将选定节点(本例中只有一个)复制到输出

    <RoomStay InfoSource="" Price="201,60" Discount="0" MealPlan="Sólo alojamiento" MealPlanId="1" Provider="Marsol" ProviderId="3" HotelName="HOTEL LA ESTACION" Rating="" Latitud="" Longitud="" />
    

    【讨论】:

      猜你喜欢
      • 2020-08-25
      • 2013-11-07
      • 1970-01-01
      • 2014-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-11
      • 2021-04-19
      相关资源
      最近更新 更多