467,145 Members | 1,004 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,145 developers. It's quick & easy.

XslTransform is not matching node when source XML contains an xmlns attribute

I am trying to transform XML to XML using an XSLT in C#, but the root node
of my XML is not being matched by the XSLT if it has an xmlns attribute. Am
I handling my namespaces incorrectly?

My C# code is,

// Create an XSLT transform object
XslTransform xslTransform = new XslTransform();

// Load the stylesheet
xslTransform.Load(xsltFilename);

// Create a new XPathDocument and load the XML data to be transformed.
// (string xmlString contains the XML below)
XPathDocument xPathDocument = new XPathDocument(xmlString);

// Create a MemoryStream to receive the data
MemoryStream transformedXmlStream = new MemoryStream();

// Transform the file
xslTransform.Transform(xPathDocument, null, transformedXmlStream, null);

// Convert output MemoryStream to a string
ASCIIEncoding ascii = new ASCIIEncoding();
string outputXmlString = ascii.GetString(transformedXmlStream.GetBuffer());
my XML string is,

<?xml version="1.0" encoding="utf-8" ?>
<CalculatorParameters xmlns="http://www.kettley.com/CalculatorParameters">
<ChildNode>TextValue</ChildNode>
</CalculatorParameters>
my XSD is,

<?xml version="1.0"?>
<xs:schema id="NewDataSet"
targetNamespace="http://www.kettley.com/CalculatorParameters"
xmlns:mstns="http://tempuri.org/XMLFile1.xsd"
xmlns="http://tempuri.org/XMLFile1.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
attributeFormDefault="qualified"
elementFormDefault="qualified">
<xs:element name="CalculatorParameters">
<xs:complexType>
<xs:sequence>
<xs:element name="ChildNode" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="NewDataSet" msdata:IsDataSet="true"
msdata:EnforceConstraints="False">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element ref="CalculatorParameters" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
and my XSLT is,

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" omit-xml-declaration="no"
indent="yes"
encoding="UTF-8"/>

<xsl:template match = "/">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="CalculatorParameters">
<AnyResult />
</xsl:template>

</xsl:stylesheet>
My problem is that the root CalculatorParameters node is only matched if I
remove its
xmlns="http://www.kettley.com/CalculatorParameters"attribute from it.

A second glitch is that, the contents of the output string have what look
like junk characters, i.e.,

"o;?<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<AnyResult />"with a whole
bunch of '\0' characters appended to the end. Where are the leading "o;?"
and trailing '\0' characters coming from?

This is really my first time working with XML much or in C# so I am a bit
wobbly.
Jan 13 '06 #1
  • viewed: 3229
Share:
4 Replies
I made progress. I figured out what was causing my transform to return
nothing, but I don't understand why it caused a problem. The problem was in
my XSLT. Can someone explain default namespaces in XSLTs?

I had the XSLT as,
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" omit-xml-declaration="no"
indent="yes"
encoding="UTF-8"/>

<xsl:template match = "/">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="CalculatorParameters">
<AnyResult />
</xsl:template>

</xsl:stylesheet>when I change it to (add a namespace for my source XML),

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:k="http://www.kettley.com/CalculatorParameters"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" version="1.0" omit-xml-declaration="no"
indent="yes" encoding="UTF-8"/>

<!--xsl:template match = "/">
<xsl:apply-templates/>
</xsl:template -->

<xsl:template match="k:CalculatorParameters">
<AnyResult />
</xsl:template>
</xsl:stylesheet>I get the output that I expect. I am still getting the
leading "o;?" and a bunch of trailing '\0' characters, which I don't
understand. It must have something to do with the XSLT parser needing to
distinguish between the tags to process and the XML tags to be interpreted
as literal output from the transform.If anyone could explain this, I would
appreciate it.Thanks.
"David S. Alexander" <da*************@kettley.com> wrote in message
news:eC**************@TK2MSFTNGP11.phx.gbl...
I am trying to transform XML to XML using an XSLT in C#, but the root node
of my XML is not being matched by the XSLT if it has an xmlns attribute.
Am I handling my namespaces incorrectly?

My C# code is,

// Create an XSLT transform object
XslTransform xslTransform = new XslTransform();

// Load the stylesheet
xslTransform.Load(xsltFilename);

// Create a new XPathDocument and load the XML data to be transformed.
// (string xmlString contains the XML below)
XPathDocument xPathDocument = new XPathDocument(xmlString);

// Create a MemoryStream to receive the data
MemoryStream transformedXmlStream = new MemoryStream();

// Transform the file
xslTransform.Transform(xPathDocument, null, transformedXmlStream, null);

// Convert output MemoryStream to a string
ASCIIEncoding ascii = new ASCIIEncoding();
string outputXmlString =
ascii.GetString(transformedXmlStream.GetBuffer());
my XML string is,

<?xml version="1.0" encoding="utf-8" ?>
<CalculatorParameters xmlns="http://www.kettley.com/CalculatorParameters">
<ChildNode>TextValue</ChildNode>
</CalculatorParameters>
my XSD is,

<?xml version="1.0"?>
<xs:schema id="NewDataSet"
targetNamespace="http://www.kettley.com/CalculatorParameters"
xmlns:mstns="http://tempuri.org/XMLFile1.xsd"
xmlns="http://tempuri.org/XMLFile1.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
attributeFormDefault="qualified"
elementFormDefault="qualified">
<xs:element name="CalculatorParameters">
<xs:complexType>
<xs:sequence>
<xs:element name="ChildNode" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="NewDataSet" msdata:IsDataSet="true"
msdata:EnforceConstraints="False">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element ref="CalculatorParameters" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
and my XSLT is,

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" omit-xml-declaration="no"
indent="yes"
encoding="UTF-8"/>

<xsl:template match = "/">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="CalculatorParameters">
<AnyResult />
</xsl:template>

</xsl:stylesheet>
My problem is that the root CalculatorParameters node is only matched if I
remove its
xmlns="http://www.kettley.com/CalculatorParameters"attribute from it.

A second glitch is that, the contents of the output string have what look
like junk characters, i.e.,

"o;?<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<AnyResult />"with a
whole bunch of '\0' characters appended to the end. Where are the leading
"o;?" and trailing '\0' characters coming from?

This is really my first time working with XML much or in C# so I am a bit
wobbly.

Jan 14 '06 #2


David S. Alexander wrote:
I made progress. I figured out what was causing my transform to return
nothing, but I don't understand why it caused a problem. The problem was in
my XSLT. Can someone explain default namespaces in XSLTs?
default namespaces is nothing defined for XSLT itself, it is something
defined for XML.
Of course you have use default namespaces in XSLT as XSLT is XML so you
could e.g. do
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml"
version="1.0">

<xsl:template match="/">
<html xml:lang="en">
and so on to ensure that your literal result elements (like that html
element) are in the namespace with namespace URI
http://www.w3.org/1999/xhtml.
The XPath 1.0 language that is used in XSLT 1.0 to XPath expressions and
for match patterns does not have any concept of a default namespace
which means to match an element in a particular namespace you need to
write an XPath expression with a qualified name consisting of a prefix
and a local name e.g.
pf:element-name
where the prefix pf must be bound the the namespace URI of the elements
you want to select.
See
<http://www.faqts.com/knowledge_base/view.phtml/aid/34022/fid/616>
for more explanation.

I am still getting the
leading "o;?" and a bunch of trailing '\0' characters, which I don't
understand.


MemoryStream transformedXmlStream = new MemoryStream();

// Transform the file
xslTransform.Transform(xPathDocument, null, transformedXmlStream, null);

// Convert output MemoryStream to a string
ASCIIEncoding ascii = new ASCIIEncoding();
string outputXmlString =
ascii.GetString(transformedXmlStream.GetBuffer() );


If you try to decode a byte stream like a memory stream to a string then
of course it matters that you choose the proper encoding for decoding,
otherwise the decoding will produce characters that were not in the byte
stream. The output stream was probably UTF-16 or UTF-8 encoded and not
ASCII encoded.

There is no need however to use a memory stream and later convert to a
string yourself if all you want is a string, you can directly transform
to a StringWriter
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemIOStringWriterClassTopic.asp>
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXslXslTransformClassTransformTopic14 .asp>
e.g. pseudo code
StringWriter stringResult = new StringWriter();
xslTransform.Transform(xPathDocument, null, stringResult, null);
string result = stringResult.ToString();
stringResult.Close()

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jan 14 '06 #3
Martin,

Thanks a lot for taking the time to help me out. I appreciate it. Now I can
hopefully get my proof of concept proven, conceptually, anyway.

Regards,

David S. Alexander

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...


David S. Alexander wrote:
I made progress. I figured out what was causing my transform to return
nothing, but I don't understand why it caused a problem. The problem was
in my XSLT. Can someone explain default namespaces in XSLTs?


default namespaces is nothing defined for XSLT itself, it is something
defined for XML.
Of course you have use default namespaces in XSLT as XSLT is XML so you
could e.g. do
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml"
version="1.0">

<xsl:template match="/">
<html xml:lang="en">
and so on to ensure that your literal result elements (like that html
element) are in the namespace with namespace URI
http://www.w3.org/1999/xhtml.
The XPath 1.0 language that is used in XSLT 1.0 to XPath expressions and
for match patterns does not have any concept of a default namespace which
means to match an element in a particular namespace you need to write an
XPath expression with a qualified name consisting of a prefix and a local
name e.g.
pf:element-name
where the prefix pf must be bound the the namespace URI of the elements
you want to select.
See
<http://www.faqts.com/knowledge_base/view.phtml/aid/34022/fid/616>
for more explanation.

I am still getting the
leading "o;?" and a bunch of trailing '\0' characters, which I don't
understand.


MemoryStream transformedXmlStream = new MemoryStream();

// Transform the file
xslTransform.Transform(xPathDocument, null, transformedXmlStream, null);

// Convert output MemoryStream to a string
ASCIIEncoding ascii = new ASCIIEncoding();
string outputXmlString =
ascii.GetString(transformedXmlStream.GetBuffer( ));


If you try to decode a byte stream like a memory stream to a string then
of course it matters that you choose the proper encoding for decoding,
otherwise the decoding will produce characters that were not in the byte
stream. The output stream was probably UTF-16 or UTF-8 encoded and not
ASCII encoded.

There is no need however to use a memory stream and later convert to a
string yourself if all you want is a string, you can directly transform to
a StringWriter
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemIOStringWriterClassTopic.asp>
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXslXslTransformClassTransformTopic14 .asp>
e.g. pseudo code
StringWriter stringResult = new StringWriter();
xslTransform.Transform(xPathDocument, null, stringResult, null);
string result = stringResult.ToString();
stringResult.Close()

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Jan 16 '06 #4
Martin,

I took your suggestions. By using the UTF8Encoding object, the "o;?" leading
characters went away and using StringWriter got rid of all the trailing '\0'
characters.

Thanks again.

David

"David S. Alexander" <da*************@kettley.com> wrote in message
news:ON**************@TK2MSFTNGP15.phx.gbl...
Martin,

Thanks a lot for taking the time to help me out. I appreciate it. Now I
can hopefully get my proof of concept proven, conceptually, anyway.

Regards,

David S. Alexander

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...


David S. Alexander wrote:
I made progress. I figured out what was causing my transform to return
nothing, but I don't understand why it caused a problem. The problem was
in my XSLT. Can someone explain default namespaces in XSLTs?


default namespaces is nothing defined for XSLT itself, it is something
defined for XML.
Of course you have use default namespaces in XSLT as XSLT is XML so you
could e.g. do
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml"
version="1.0">

<xsl:template match="/">
<html xml:lang="en">
and so on to ensure that your literal result elements (like that html
element) are in the namespace with namespace URI
http://www.w3.org/1999/xhtml.
The XPath 1.0 language that is used in XSLT 1.0 to XPath expressions and
for match patterns does not have any concept of a default namespace which
means to match an element in a particular namespace you need to write an
XPath expression with a qualified name consisting of a prefix and a local
name e.g.
pf:element-name
where the prefix pf must be bound the the namespace URI of the elements
you want to select.
See
<http://www.faqts.com/knowledge_base/view.phtml/aid/34022/fid/616>
for more explanation.

> I am still getting the
leading "o;?" and a bunch of trailing '\0' characters, which I don't
understand.


MemoryStream transformedXmlStream = new MemoryStream();

// Transform the file
xslTransform.Transform(xPathDocument, null, transformedXmlStream, null);

// Convert output MemoryStream to a string
ASCIIEncoding ascii = new ASCIIEncoding();
string outputXmlString =
ascii.GetString(transformedXmlStream.GetBuffer ());


If you try to decode a byte stream like a memory stream to a string then
of course it matters that you choose the proper encoding for decoding,
otherwise the decoding will produce characters that were not in the byte
stream. The output stream was probably UTF-16 or UTF-8 encoded and not
ASCII encoded.

There is no need however to use a memory stream and later convert to a
string yourself if all you want is a string, you can directly transform
to a StringWriter
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemIOStringWriterClassTopic.asp>
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXslXslTransformClassTransformTopic14 .asp>
e.g. pseudo code
StringWriter stringResult = new StringWriter();
xslTransform.Transform(xPathDocument, null, stringResult, null);
string result = stringResult.ToString();
stringResult.Close()

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


Jan 16 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Rolf Kemper | last post: by
1 post views Thread by Animesh Sharma | last post: by
5 posts views Thread by Kevin Westhead | last post: by
6 posts views Thread by BLechmann | last post: by
1 post views Thread by rmgalante@yahoo.com | last post: by
3 posts views Thread by arochax@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.