困惑什么应该是我的Xpath表达式

本文关键字:我的 Xpath 表达式 什么 | 更新日期: 2023-09-27 18:22:33

下面给出了我的categories.xml文件

<categories>
  <root name="Cars -Vehicles" id="CV"></root>
  <root name="Personals" id="PER"></root>
  <root name="Real Estate" id="RE"></root>
  <root name="Property For Rent" id="PFR"></root>
  <root name="Community" id="COM"></root>
</categories>

我的attibutes.xml文件在下面给出

<attributes>
  <attribute value="ethnicity=ethnicity_african_american" name ="American Indian" categoryid="PER"/>
  <attribute value="ethnicity=ethnicity_asian_pacific_islander" name ="Asian - Pacific Islander" categoryid="PER"/>
  <attribute value="ethnicity=ethnicity_caucasian" name ="Caucasian" categoryid="PER"/>

  <attribute value="amenities=amenities_ac" name ="AC" categoryid="RE, PFR"/>
  <attribute value="amenities=amenities_alarm" name ="Alarm" categoryid="RE, PFR"/>
</attributes>

我在选择categoriesDropDown时绑定了我的attributeDropdown。下方给出了代码

 protected void ddCategories_SelectedIndexChanged(object sender, EventArgs e)
 {
            XmlDataSource xd = new XmlDataSource();
            xd.DataFile = Server.MapPath("Xmls") + "''attributes.xml";
            xd.XPath = "/attributes/attribute[@categoryid='" + 
                        categoriesDropDown.SelectedValue.ToString() + "']";
            attributeDropdown.DataSource = xd;
            attributeDropdown.DataTextField = "name";
            attributeDropdown.DataValueField = "value";
            attributeDropdown.DataBind();
         // categoriesDropDown.DataTextField = "name";
         // categoriesDropDown.DataValueField = "id";
 }

现在的问题是,名为ACAlarm的两个属性属于Real EstateProperty For Rent两类。如何在选择这些类别时绑定这些属性?

如果属性在多个类别中,那么我的Xpath表达式应该是什么

困惑什么应该是我的Xpath表达式

也许这不是一个好的解决方案,但您可以使用contains函数,例如:

/attributes/attribute[contains(@categoryid, 'category')]

根据您的代码:

xd.XPath = string.Format("/attributes/attribute[contains(@categoryid, '{0}')]",
    categoriesDropDown.SelectedValue);