Groups | Blog | Home
all groups > dotnet xml > february 2005 >

dotnet xml : Why are my element prefixes disappearing?


Harry Keck
2/11/2005 8:25:02 AM
I am trying to create an xsl stylesheet on the fly as an xml document. I
create elements of type "xsl:value-of" and insert them into my document, but
when I output the xml representation of the document, the "xsl" prefix is not
there, only the "value-of" is present, so my transform does not work. What
do I need to do to get the prefix to become part of the xml output? I have
even tried using the CreateElement version with a separate prefix and local
Harry Keck
2/11/2005 8:53:06 AM
As a follow up, I tried the version of CreateElement that requires a
namespace as a parameter. When I pass a namespace, the element's "xsl"
prefix shows up the way that I want it, but now I have the namepace tag in my
"xsl:value-of" element, which, again, messes up the transform. Is there a
solution that will produce the prefix without the namespace attribute?

[quoted text, click to view]
Harry Keck
2/11/2005 1:43:02 PM
I found that if I put the correct namespace, then the transform works.

xmlValueOfElement = xmlTransformDocument.CreateElement("xsl","value-of",
xmlTransformDocument.DocumentElement.NamespaceURI );

The reason I am not using a writer is because I am starting with a static
document and then adding some extra elements to it, so I need the ability to
dynamically get to elements inside the document while adding to it.

[quoted text, click to view]
Dino Chiesa [Microsoft]
2/11/2005 3:20:13 PM
Does this do what you want?

--begin code--
using System.Xml;

public class XmlWriterTest {
public static void Main() {

System.Console.WriteLine("\n\n");

string filename= "Sample.xsl";
string xsltns= "http://www.w3.org/1999/XSL/Transform";
XmlTextWriter xwriter = new
XmlTextWriter(filename,System.Text.Encoding.UTF8);
xwriter.Formatting = Formatting.Indented;
xwriter.WriteStartDocument(true);

xwriter.WriteStartElement("xsl", "stylesheet",xsltns);
xwriter.WriteAttributeString("version", "1.0");

xwriter.WriteStartElement("output",xsltns);
xwriter.WriteAttributeString("method", "xml");
xwriter.WriteAttributeString("indent", "yes");
xwriter.WriteAttributeString("encoding", "UTF-8");
xwriter.WriteEndElement(); // output

xwriter.WriteStartElement("template",xsltns);
xwriter.WriteAttributeString("match", "text()|@*");
xwriter.WriteEndElement(); // template

xwriter.WriteStartElement("template",xsltns);
xwriter.WriteAttributeString("match", "AdvanceShipmentNotice");
xwriter.WriteStartElement("root","");
xwriter.WriteStartElement("apply-templates",xsltns);
xwriter.WriteEndElement(); // root
xwriter.WriteEndElement(); // xsl-template

xwriter.WriteStartElement("template",xsltns);
xwriter.WriteAttributeString("match", "Rabble");
xwriter.WriteStartElement("value-of",xsltns);
xwriter.WriteAttributeString("select", "Ident");
xwriter.WriteEndElement(); // value-of
xwriter.WriteEndElement(); // template

// and so on with other xsl-template elements ....

xwriter.WriteEndElement(); // stylesheet

xwriter.Flush();
xwriter.Close();

//Read the file back in and parse to ensure well formed XML.
XmlDocument doc = new XmlDocument();
//Preserve white space for readability.
doc.PreserveWhitespace = true;
//Load the file
doc.Load(filename);

//Write the XML content to the console.
doc.Save(System.Console.Out);

System.Console.WriteLine("\n\n");

}
}


--end code--

-Dino


[quoted text, click to view]

AddThis Social Bookmark Button