473,320 Members | 1,799 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,320 software developers and data experts.

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
4 3559
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Hugh Sparks | last post by:
When processing an xml document that contains elements such as: <element xmlns="goop"/> ... <element xmlns="gleep"/> I want to create two xsl stylesheet templates: one that matches the...
7
by: Rolf Kemper | last post by:
Dear All, somehow I remember that such or similar question was discussed already somewhere. But I can't find it anymore. I have a template calling itself. As long it goes deeper into the...
1
by: Animesh Sharma | last post by:
Hi, Following template matches correctly in MSXML4 but NOT with .Net XSL translator: <xsl:template match="//input]"> <--Do Some operation> </xsl:template>
5
by: Kevin Westhead | last post by:
I'm using XslTransform to apply a transform to an XML document, however I get validation problems when parsing the resulting XML document due to invalid whitespace. I'm passing in an XPathNavigator...
6
by: BLechmann | last post by:
Hello *, I'm trying to transform my DocBook document with the FO stylesheet from the DocBook XSL distribution (the transformation with the XHTML stylesheet works). I changed "$years + 1" to...
3
by: Steve | last post by:
Is there any way of specifying the startMode when using the xslTransform class? We are updating code which used msxml to the system.xml classes but can find no way to specify the startMode. We...
1
by: rmgalante | last post by:
Hello, I have a VB.Net component that uses the XslTransform object. I am using the FXSL randomizeList function in my XSL template. When I transform the XML in the VB.Net component, my...
3
by: arochax | last post by:
Hi. I'm using XslTransform to transform a XML resulting of a Dataset into a Excel spreadsheet. But the object is not releasing its resources while transforming and I'm getting OutOfMemory...
4
by: clover2411 | last post by:
Hi there, Apologies if I'm posting to the wrong group; this one looked to be the best match. I'm having trouble writing a bit of XSL/XPATH and wondered if someone would please shed some...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...

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.