473,385 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Why are my element prefixes disappearing?

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
name parameters, but it did not change anything. Thanks.
Nov 12 '05 #1
3 1437
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?

"Harry Keck" wrote:
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
name parameters, but it did not change anything. Thanks.

Nov 12 '05 #2
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
"Harry Keck" <Ha*******@discussions.microsoft.com> wrote in message
news:11**********************************@microsof t.com...
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?

"Harry Keck" wrote:
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
name parameters, but it did not change anything. Thanks.

Nov 12 '05 #3
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.

"Dino Chiesa [Microsoft]" wrote:
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
"Harry Keck" <Ha*******@discussions.microsoft.com> wrote in message
news:11**********************************@microsof t.com...
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?

"Harry Keck" wrote:
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
name parameters, but it did not change anything. Thanks.


Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Chris Spencer | last post by:
Does anyone know how to make ElementTree preserve namespace prefixes in parsed xml files? The default behavior is to strip a document of all prefixes and then replace them autogenerated prefixes...
5
by: Adam Barr | last post by:
I have a tag foo that I want to copy unchanged when it is a subtag of bar, so I have a template (x is the namespace for the document): <xsl:template match="x:bar/x:foo"> <xsl:copy>...
4
by: Hollywood | last post by:
I'm using XML serialization to produce the following XML document: <TestDoc xmlns:srd="some-url"> <Additional> <Security> <srd:Login>login_id</srd:Login> <srd:Password>password</srd:Password>...
2
by: Wayne Wengert | last post by:
I am exporting an XML file based on a dataset using VB.NET. This works fine but the resulting xml file does not include namespace prefixes, which are required by another tool I am trying to use...
4
by: FabrizioSW | last post by:
Hi all i've to create a xml doc like this <?xml version="1.0" encoding="UTF-8"?> <Main xmlns:x="http://www.w3.org/1999/XML/xinclude"> <x:include href="one.xml"/> <x:include href="two.xml"/>...
7
by: shaun roe | last post by:
I should like to use xslt to produce a document like the following: <crate xmlns:xi="http://www.w3.org/2001/XInclude"> <rod id="0"> <slot>1</slot> <xi:include...
9
by: Mark Olbert | last post by:
I'm trying to serialize (using XmlSerializer.Serialize) a class that I generated from an XSD schema using XSD.EXE /c. The problem I'm running into is that the root element needs to be unqualified,...
10
by: Simon Brooke | last post by:
I'm struggling to understand what 'exclude-result-prefixes' does and is for; the language of the standard http://www.zvon.org/xxl/XSLTreference/W3C/xslt.html#literal-result-element is not...
0
by: deeptimore | last post by:
I am having problem in adding prefix to nodes in xml document. I want it like this: <xsl:value-of select="NewDataSet/SyouhinTable/ItemTran03 /> but it is writing: <xsl:value-of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.