Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

modify xml using xslt...

Status
Not open for further replies.

AT76

Technical User
Apr 14, 2005
460
0
0
US
Hi,

I have a DropDownList which source is an XML Doc. However, the items displayed by the DropDownList are not sorted. My Goal is to be able to sort the items in the DropDownList.

I understand that I can achieve the sorting issue via an XSL doc. Is this correct? If so, my attempts have not been successful so far.

Here's what I got:

Code:
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (selection.SelectedValue == "")
        {
            AlertMsg("Please select a role.");
            goto Exit;
        }
        this.selection.BackColor = Color.White;
        this.lblPath.Visible = true;
        this.selection2.Visible = true;
        this.selection2.Items.Clear();
        this.Button2.Visible = true;
        xDoc.Load(xmlPath);
        [COLOR=green]//Trying to Transform XML file to sort [/color]
        [b][COLOR=red]XslCompiledTransform xslDoc = new XslCompiledTransform();
        xslDoc.Load(Server.MapPath("sortAudience.xsl"));
        XmlTextWriter writer = new XmlTextWriter(Response.Output);
        writer.WriteStartDocument(); 
        xslDoc.Transform(xDoc, null, writer);[/color][/b]

        audienceList = xDoc.SelectNodes("/dataroot/Partner_x0020_Readiness_x0020_List[Audience='" + selection.SelectedValue + "']/Path_x0020_Type");

        foreach (XmlNode n in audienceList)
        {
            item = new ListItem(n.InnerText, n.InnerText);
            this.selection2.Items.Add(item);
        }
        this.selection2.Items.Insert(0, "");
        Exit: ;
    }

A sample of my XML file is:

Code:
<?xml version="1.0" encoding="UTF-8"?>
[b][COLOR=red]<dataroot[/color] [/b]xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema-instance"[/URL] xsi:noNamespaceSchemaLocation="Partner%20Readiness%20List.xsd" generated="2007-05-17T21:52:53">
	[b][COLOR=red]<Partner_x0020_Readiness_x0020_List>[/color][/b]
		<ID>66</ID>
		<Title>5178: Implementing Conferencing Solutions Using Microsoft Office Communications Server 2007 and Live Meeting</Title>
		<Product>
			<Value>LCS 2005/OCS 2007</Value>
		</Product>
		<Form_x0020_Factor>
			<Value>Instructor Led Training</Value>
		</Form_x0020_Factor>
		<Content_x0020_Level>300</Content_x0020_Level>
		<Availability>2007-10-03T00:00:00</Availability>
		<Source>MS Learning</Source>
		<Path_x0020_Type>
			<Value>UC Specializations</Value>
		</Path_x0020_Type>
		<Partner_x0020_Type>All</Partner_x0020_Type>
		<Core>Yes</Core>
		<Exam_x0020_Mapping>70-638</Exam_x0020_Mapping>
		[b][COLOR=red]<Audience>[/color][/b]Implementer</Audience>
		<Audience_x0020_Level>Experienced</Audience_x0020_Level>
	</Partner_x0020_Readiness_x0020_List>

and here is the xsl I wrote:

Code:
<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
    xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]

<xsl:template match="/">
  <html>
      <dataroot>
        <xsl:apply-templates/>
      </dataroot>
  </html>
</xsl:template>
  <xsl:template match="Partner_x0020_Readiness_List">
    <Partner_x0020_Readiness_List>
      [b][COLOR=red]<xsl:for-each select="dataroot/Partner_x0020_Readiness_List">
        <xsl:sort select="Audience"/>[/color][/b]
      </xsl:for-each>
    </Partner_x0020_Readiness_List>
  </xsl:template>  
</xsl:stylesheet>

Thank you for any help provided!!!
 
It seems to me the error lies in the xslt.

The result from the above gives the following:

<html><dataroot>665178: Implementing Conferencing Solutions Using Microsoft Office Communications Server 2007 and Live MeetingLCS 2005/OCS 2007Instructor Led Training3002007-10-03T00:00:00MS LearningUC SpecializationsAllYes70-638Implementer ... </dataroot></html>

This is what I got after I tried an example from the web:

Code:
private string GetTransformedXML(XmlDocument doc, XslCompiledTransform xslt)
    { 
        XPathNavigator xpathNav = doc.CreateNavigator();     
        BufferedStream myStream = new BufferedStream(new MemoryStream()); 
        xslt.Transform(xpathNav, null,myStream);     
        myStream.Seek(0, SeekOrigin.Begin); 

        StreamReader mySReader = new StreamReader(myStream); 
        String output = mySReader.ReadToEnd(); 

        return output;                   
    }

When I call the above function:

Code:
string xmlResults = GetTransformedXML(xDoc, xslDoc);

The result is what I mentioned in the beginning. What I need to do is basically keep the same structure from the original XML file but I need to sort on the Audience Element.

Thank you for any suggestions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top