locked
XML / XPATH / DropDown control RRS feed

  • Question

  • User266526312 posted

    Hello, I'm having a little trouble with a drop down control that I'm trying to populate with XML data.

    I have the following XML file (this is a partial excerpt for testing)

    <?xml version='1.0' encoding='UTF-8'?>
    <ArrayOfMSAG xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <MSAG>
        <StatusCode>3</StatusCode>
        <StatusDesc_E>Active</StatusDesc_E>
        <StatusDesc_F>Actif</StatusDesc_F>
        <PlaceCode>521</PlaceCode>
        <PlaceName_E>Kedgwick-Ouest</PlaceName_E>
        <PlaceName_F>Kedgwick-Ouest</PlaceName_F>
        <StreetName>GAUTHIER</StreetName>
        <StreetTypeCode>23</StreetTypeCode>
        <StreetTypeDesc>Chemin</StreetTypeDesc>
        <StreetTypeDesc_Language>Francais</StreetTypeDesc_Language>
        <StreetDirCode>0</StreetDirCode>
        <StreetDirDesc_E />
        <StreetDirDesc_F />
        <StreetClassCode>2</StreetClassCode>
        <StreetClassDesc_E>Private</StreetClassDesc_E>
        <StreetClassDesc_F>Prive</StreetClassDesc_F>
        <Functional_Road_Class>7</Functional_Road_Class>
        <Segment_Low_Left_CivicNum>1</Segment_Low_Left_CivicNum>
        <Segment_High_Left_CivicNum>27</Segment_High_Left_CivicNum>
        <Segment_Low_Right_CivicNum>2</Segment_Low_Right_CivicNum>
        <Segment_High_Right_CivicNum>28</Segment_High_Right_CivicNum>
        <PoliceCode />
        <PoliceDesc>St Quentin Rcmp</PoliceDesc>
        <FireDepCode />
        <FireDepDesc>Kedgwick Fir</FireDepDesc>
        <AmbulanceCode />
        <AmbulanceDesc>Kedgwick Amb</AmbulanceDesc>
        <PoisonControlCode />
        <PoisonControlDesc>Poison Control</PoisonControlDesc>
        <PSAPCode />
        <PSAPDesc>Edmn Psap</PSAPDesc>
        <Element_Revision_Date>20160115</Element_Revision_Date>
      </MSAG>
      <MSAG>
        <StatusCode>3</StatusCode>
        <StatusDesc_E>Active</StatusDesc_E>
        <StatusDesc_F>Actif</StatusDesc_F>
        <PlaceCode>309</PlaceCode>
        <PlaceName_E>DSL de Saint-Quentin</PlaceName_E>
        <PlaceName_F>DSL de Saint-Quentin</PlaceName_F>
        <StreetName>JONATHAN</StreetName>
        <StreetTypeCode>110</StreetTypeCode>
        <StreetTypeDesc>Rue</StreetTypeDesc>
        <StreetTypeDesc_Language>Francais</StreetTypeDesc_Language>
        <StreetDirCode>0</StreetDirCode>
        <StreetDirDesc_E />
        <StreetDirDesc_F />
        <StreetClassCode>1</StreetClassCode>
        <StreetClassDesc_E>Public</StreetClassDesc_E>
        <StreetClassDesc_F>Publique</StreetClassDesc_F>
        <Functional_Road_Class>5</Functional_Road_Class>
        <Segment_Low_Left_CivicNum>1</Segment_Low_Left_CivicNum>
        <Segment_High_Left_CivicNum>59</Segment_High_Left_CivicNum>
        <Segment_Low_Right_CivicNum>2</Segment_Low_Right_CivicNum>
        <Segment_High_Right_CivicNum>60</Segment_High_Right_CivicNum>
        <PoliceCode />
        <PoliceDesc>St Quentin Rcmp</PoliceDesc>
        <FireDepCode />
        <FireDepDesc>St Quentin Fir</FireDepDesc>
        <AmbulanceCode />
        <AmbulanceDesc>St Quentin Amb</AmbulanceDesc>
        <PoisonControlCode />
        <PoisonControlDesc>Poison Control</PoisonControlDesc>
        <PSAPCode />
        <PSAPDesc>Edmn Psap</PSAPDesc>
        <Element_Revision_Date>20140416</Element_Revision_Date>
      </MSAG>
      <MSAG>
        <StatusCode>3</StatusCode>
        <StatusDesc_E>Active</StatusDesc_E>
        <StatusDesc_F>Actif</StatusDesc_F>
        <PlaceCode>1248</PlaceCode>
        <PlaceName_E>Wyers Brook</PlaceName_E>
        <PlaceName_F>Wyers Brook</PlaceName_F>
        <StreetName>WYERS BROOK</StreetName>
        <StreetTypeCode>103</StreetTypeCode>
        <StreetTypeDesc>Road</StreetTypeDesc>
        <StreetTypeDesc_Language>English</StreetTypeDesc_Language>
        <StreetDirCode>0</StreetDirCode>
        <StreetDirDesc_E />
        <StreetDirDesc_F />
        <StreetClassCode>1</StreetClassCode>
        <StreetClassDesc_E>Public</StreetClassDesc_E>
        <StreetClassDesc_F>Publique</StreetClassDesc_F>
        <Functional_Road_Class>422</Functional_Road_Class>
        <Segment_Low_Left_CivicNum>111</Segment_Low_Left_CivicNum>
        <Segment_High_Left_CivicNum>569</Segment_High_Left_CivicNum>
        <Segment_Low_Right_CivicNum>112</Segment_Low_Right_CivicNum>
        <Segment_High_Right_CivicNum>570</Segment_High_Right_CivicNum>
        <PoliceCode />
        <PoliceDesc>Campbellton Cou</PoliceDesc>
        <FireDepCode />
        <FireDepDesc>Robinsonvill Fir</FireDepDesc>
        <AmbulanceCode />
        <AmbulanceDesc>Campbellton Amb</AmbulanceDesc>
        <PoisonControlCode />
        <PoisonControlDesc>Poison Control</PoisonControlDesc>
        <PSAPCode />
        <PSAPDesc>Btst Psap</PSAPDesc>
        <Element_Revision_Date>20160113</Element_Revision_Date>
      </MSAG>
      <MSAG>
        <StatusCode>11</StatusCode>
        <StatusDesc_E>Reserved</StatusDesc_E>
        <StatusDesc_F>Réservé</StatusDesc_F>
        <PlaceCode>1345</PlaceCode>
        <PlaceName_E>Shediac River</PlaceName_E>
        <PlaceName_F>Shediac River</PlaceName_F>
        <StreetName>River</StreetName>
        <StreetTypeCode>58</StreetTypeCode>
        <StreetTypeDesc>Lane</StreetTypeDesc>
        <StreetTypeDesc_Language>English</StreetTypeDesc_Language>
        <STREET_TYPE_DESC_F>Allée</STREET_TYPE_DESC_F>
        <StreetClassCode>2</StreetClassCode>
        <StreetClassDesc_E>Private</StreetClassDesc_E>
        <StreetClassDesc_F>Privée</StreetClassDesc_F>
        <Segment_Low_Left_CivicNum>0</Segment_Low_Left_CivicNum>
        <Segment_High_Left_CivicNum>0</Segment_High_Left_CivicNum>
        <Segment_Low_Right_CivicNum>0</Segment_Low_Right_CivicNum>
        <Segment_High_Right_CivicNum>0</Segment_High_Right_CivicNum>
        <PoliceCode>0</PoliceCode>
        <PoliceDesc />
        <FireDepCode>0</FireDepCode>
        <FireDepDesc />
        <AmbulanceCode>0</AmbulanceCode>
        <AmbulanceDesc />
        <PoisonControlCode>0</PoisonControlCode>
        <PoisonControlDesc />
        <PSAPCode>0</PSAPCode>
        <PSAPDesc />
        <GSACode>0</GSACode>
        <ExternalSegment_Id>0</ExternalSegment_Id>
        <SegmentEffectiveDate />
        <GroupId />
        <FirstResponder>Unknown</FirstResponder>
      </MSAG>
      <MSAG>
        <StatusCode>11</StatusCode>
        <StatusDesc_E>Reserved</StatusDesc_E>
        <StatusDesc_F>Réservé</StatusDesc_F>
        <PlaceCode>1348</PlaceCode>
        <PlaceName_E>Canaan Forks</PlaceName_E>
        <PlaceName_F>Canaan Forks</PlaceName_F>
        <StreetName>Cleveland</StreetName>
        <StreetTypeCode>58</StreetTypeCode>
        <StreetTypeDesc>Lane</StreetTypeDesc>
        <StreetTypeDesc_Language>English</StreetTypeDesc_Language>
        <STREET_TYPE_DESC_F>Allée</STREET_TYPE_DESC_F>
        <StreetClassCode>2</StreetClassCode>
        <StreetClassDesc_E>Private</StreetClassDesc_E>
        <StreetClassDesc_F>Privée</StreetClassDesc_F>
        <Segment_Low_Left_CivicNum>0</Segment_Low_Left_CivicNum>
        <Segment_High_Left_CivicNum>0</Segment_High_Left_CivicNum>
        <Segment_Low_Right_CivicNum>0</Segment_Low_Right_CivicNum>
        <Segment_High_Right_CivicNum>0</Segment_High_Right_CivicNum>
        <PoliceCode>0</PoliceCode>
        <PoliceDesc />
        <FireDepCode>0</FireDepCode>
        <FireDepDesc />
        <AmbulanceCode>0</AmbulanceCode>
        <AmbulanceDesc />
        <PoisonControlCode>0</PoisonControlCode>
        <PoisonControlDesc />
        <PSAPCode>0</PSAPCode>
        <PSAPDesc />
        <GSACode>0</GSACode>
        <ExternalSegment_Id>0</ExternalSegment_Id>
        <SegmentEffectiveDate />
        <GroupId />
        <FirstResponder>Unknown</FirstResponder>
      </MSAG>
    </ArrayOfMSAG>

    This is my xslt file (designed to transform elements to attributes, so I can reference them in the dropdown form control):

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output indent="yes"/>
      <xsl:strip-space elements="*"/>
    
      <!--Identity transform-->
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
    
      <!--Match elements that contain children elements that don't contain
        children elements themselves.-->
      <xsl:template match="*[*[not(*)]]">
        <xsl:copy>
          <xsl:apply-templates select="@*"/>
          <xsl:apply-templates select="*[not(*)]" mode="attr"/>
          <xsl:apply-templates select="*[*]"/>
        </xsl:copy>
      </xsl:template>
    
      <!--Moded template that turns an element into an attribute.-->
      <xsl:template match="*" mode="attr">
        <xsl:attribute name="{name()}">
          <xsl:value-of select="string()"/>
        </xsl:attribute>
      </xsl:template>
    </xsl:stylesheet>
    

    and this is the part of the aspx file that implements the dropdown and is failing currently:

        <tr>
            <td valign="top">
                <font face="Arial" size="2" color="#005349"><strong>Community / Localit&eacute; : </strong></font>
            </td>
            <td valign="top">
    		<asp:xmldatasource runat="server" DataFile="~/New/MSAG.xml" XPath="//*[name()='ArrayOfMSAG']/MSAG[not(@PlaceName_E=preceding-sibling::MSAG/@PlaceName_E)]/@PlaceName_E" ID="Xmldatasource2" TransformFile="~/New/MSAG.xslt"></asp:xmldatasource>
                    <asp:DropDownList ID="CommunityName" runat="server" DataSourceID="Xmldatasource2" AppendDataBoundItems="True" DataTextField="PlaceName_E" DataValueField="PlaceName_E">
                        <asp:ListItem Value="" Selected="True"></asp:ListItem>
                    </asp:DropDownList>
                              
            </td>
        </tr>
    PREVIOUS INCORRECT CODE BLOCK REMOVED

    Obviously that's not the whole aspx file, just the problem part.  

    The XPATH is selecting distinct items out of the XML file - i.e. I only want each possible Place Name listed once.

    So I'm getting a drop down with only the pre-selected empty item in the list.  None of the other items are showing up.

    I have tested my XML, XPATH and XSLT using a sandbox I found online here: http://www.flynn1179.net/xml/  They all appear to be providing the expected result.

    My first thought was that I'm missing something in the code behind page that would perform the transformation, but none of the examples I've looked at had anything there.  If I am missing something, please let me know what that might be.

    Thanks, 

    Colin

    Friday, October 6, 2017 2:48 PM

All replies

  • User2103319870 posted

    XML which is generated using your XSLT will be like below

    <?xml version="1.0" encoding="utf-8"?>
    <ArrayOfMSAG xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <MSAG StatusCode="3" StatusDesc_E="Active" StatusDesc_F="Actif" PlaceCode="521" PlaceName_E="Kedgwick-Ouest" PlaceName_F="Kedgwick-Ouest" StreetName="GAUTHIER" StreetTypeCode="23" StreetTypeDesc="Chemin" StreetTypeDesc_Language="Francais" StreetDirCode="0" StreetDirDesc_E="" StreetDirDesc_F="" StreetClassCode="2" StreetClassDesc_E="Private" StreetClassDesc_F="Prive" Functional_Road_Class="7" Segment_Low_Left_CivicNum="1" Segment_High_Left_CivicNum="27" Segment_Low_Right_CivicNum="2" Segment_High_Right_CivicNum="28" PoliceCode="" PoliceDesc="St Quentin Rcmp" FireDepCode="" FireDepDesc="Kedgwick Fir" AmbulanceCode="" AmbulanceDesc="Kedgwick Amb" PoisonControlCode="" PoisonControlDesc="Poison Control" PSAPCode="" PSAPDesc="Edmn Psap" Element_Revision_Date="20160115" />
      <!--Other items removed for brevity -->
     </ArrayOfMSAG>

    Hence you need to make the below changes inorder to get the dropdownlist populated

    XMLDataSource

    Assign the XSLT to datasource using Transform file property and also change the XPath as per the xml generated at runtime

      <asp:xmldatasource runat="server" DataFile="XMLFile1.xml" XPath="ArrayOfMSAG/MSAG" ID="StreetTypes" TransformFile="XSLTFile1.xslt" ></asp:xmldatasource>

    Change the path for Datafile and TransformFile as per your design

    Dropdownlist

    You dont have a property namely value in xml, use the actual property name which you want in dropdownlist

        <asp:DropDownList ID="StreetType" runat="server" DataSourceID="StreetTypes" DataTextField="PlaceName_F" DataValueField="PlaceName_F" AppendDataBoundItems="True">
                    <asp:ListItem Value="" Selected="True"></asp:ListItem>
                </asp:DropDownList>

    Friday, October 6, 2017 3:25 PM
  • User266526312 posted

    Sorry, I'm apparently having a brain melt down today - I put the wrong piece of code into my original post - The portion of the ASPX code which is failing has been edited above.

    I appreciate your attempt to help, but I'll get better results if my original post has the right problem shown.

    Friday, October 6, 2017 4:12 PM
  • User2103319870 posted

    Have you considered changing the XPath in your dropdownlist like I provided above, if not please provide that. It will populate the dropdownlist with placenames with out any duplicates

      <asp:xmldatasource runat="server" DataFile="XMLFile1.xml" XPath="ArrayOfMSAG/MSAG" ID="StreetTypes" TransformFile="XSLTFile1.xslt" ></asp:xmldatasource>
    
             <asp:DropDownList ID="StreetType" runat="server" DataSourceID="StreetTypes" DataTextField="PlaceName_E" DataValueField="PlaceName_E" AppendDataBoundItems="True">
                    <asp:ListItem Value="" Selected="True"></asp:ListItem>
                </asp:DropDownList>

    Friday, October 6, 2017 4:36 PM
  • User266526312 posted

    I tried that, now I am getting:

    Server Error in '/TEST' Application.
    
    DataBinding: 'System.Web.UI.WebControls.XmlDataSourceNodeDescriptor' does not contain a property with the name 'PlaceName_E'.
    
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.Web.HttpException: DataBinding: 'System.Web.UI.WebControls.XmlDataSourceNodeDescriptor' does not contain a property with the name 'PlaceName_E'.
    
    Source Error: 
    
    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
    
    Stack Trace: 
    
    
    [HttpException (0x80004005): DataBinding: 'System.Web.UI.WebControls.XmlDataSourceNodeDescriptor' does not contain a property with the name 'PlaceName_E'.]
       System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName) +8752021
       System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName, String format) +8
       System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource) +332
       System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +107
       System.Web.UI.WebControls.ListControl.PerformSelect() +34
       System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
       System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
       System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgs e) +22
       System.Web.UI.WebControls.ListControl.OnPreRender(EventArgs e) +18
       System.Web.UI.Control.PreRenderRecursiveInternal() +80
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842

    It's like it isn't applying the xslt file at all.  

    If I change the XPATH to "ArrayOfMSAG/MSAG/@PlaceName_E"  I don't get an error message, but I get a blank list again.

    Friday, October 6, 2017 5:58 PM
  • User-1838255255 posted

    Hi chlang,

    Try modifying your XPath to ArrayOfMSAG/MSAG/PlaceName_E.

    Best Regards,

    Eric Du

    Tuesday, October 10, 2017 9:43 AM
  • User266526312 posted

    tried that, didn't work.  Still a blank dropdown

    Tuesday, October 10, 2017 5:53 PM
  • User-1838255255 posted

    Hi chlang,

    Please remove XPath, and use below code:

    <asp:XmlDataSource runat="server" DataFile="~/New/MSAG.xml"
          TransformFile="~/New/MSAG.xslt"
          ID="Xmldatasource2">
    </asp:XmlDataSource>
    <asp:DropDownList ID="CommunityName" runat="server" DataSourceID="Xmldatasource2"
          AppendDataBoundItems="True" DataTextField="PlaceName_E" DataValueField="PlaceName_E">
          <asp:ListItem Value="" Selected="True"></asp:ListItem>
    </asp:DropDownList>

    Result:

    And here is the result of the above code:

    Best Regards,

    Eric Du

    Wednesday, October 18, 2017 9:20 AM