473,545 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Lo ad(xsltFilename );

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

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

// Transform the file
xslTransform.Tr ansform(xPathDo cument, null, transformedXmlS tream, null);

// Convert output MemoryStream to a string
ASCIIEncoding ascii = new ASCIIEncoding() ;
string outputXmlString = ascii.GetString (transformedXml Stream.GetBuffe r());
my XML string is,

<?xml version="1.0" encoding="utf-8" ?>
<CalculatorPara meters xmlns="http://www.kettley.com/CalculatorParam eters">
<ChildNode>Text Value</ChildNode>
</CalculatorParam eters>
my XSD is,

<?xml version="1.0"?>
<xs:schema id="NewDataSet "
targetNamespace ="http://www.kettley.com/CalculatorParam eters"
xmlns:mstns="ht tp://tempuri.org/XMLFile1.xsd"
xmlns="http://tempuri.org/XMLFile1.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="u rn:schemas-microsoft-com:xml-msdata"
attributeFormDe fault="qualifie d"
elementFormDefa ult="qualified" >
<xs:element name="Calculato rParameters">
<xs:complexType >
<xs:sequence>
<xs:element name="ChildNode " type="xs:string " minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="NewDataSe t" msdata:IsDataSe t="true"
msdata:EnforceC onstraints="Fal se">
<xs:complexType >
<xs:choice maxOccurs="unbo unded">
<xs:element ref="Calculator Parameters" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
and my XSLT is,

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:styleshe et 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="Calculat orParameters">
<AnyResult />
</xsl:template>

</xsl:stylesheet>
My problem is that the root CalculatorParam eters node is only matched if I
remove its
xmlns="http://www.kettley.com/CalculatorParam eters"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<AnyRe sult />"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 3581
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:styleshe et 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="Calculat orParameters">
<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:styleshe et version="1.0"
xmlns:k="http://www.kettley.com/CalculatorParam eters"
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:Calcul atorParameters" >
<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******** ******@TK2MSFTN GP11.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.Lo ad(xsltFilename );

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

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

// Transform the file
xslTransform.Tr ansform(xPathDo cument, null, transformedXmlS tream, null);

// Convert output MemoryStream to a string
ASCIIEncoding ascii = new ASCIIEncoding() ;
string outputXmlString =
ascii.GetString (transformedXml Stream.GetBuffe r());
my XML string is,

<?xml version="1.0" encoding="utf-8" ?>
<CalculatorPara meters xmlns="http://www.kettley.com/CalculatorParam eters">
<ChildNode>Text Value</ChildNode>
</CalculatorParam eters>
my XSD is,

<?xml version="1.0"?>
<xs:schema id="NewDataSet "
targetNamespace ="http://www.kettley.com/CalculatorParam eters"
xmlns:mstns="ht tp://tempuri.org/XMLFile1.xsd"
xmlns="http://tempuri.org/XMLFile1.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="u rn:schemas-microsoft-com:xml-msdata"
attributeFormDe fault="qualifie d"
elementFormDefa ult="qualified" >
<xs:element name="Calculato rParameters">
<xs:complexType >
<xs:sequence>
<xs:element name="ChildNode " type="xs:string " minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="NewDataSe t" msdata:IsDataSe t="true"
msdata:EnforceC onstraints="Fal se">
<xs:complexType >
<xs:choice maxOccurs="unbo unded">
<xs:element ref="Calculator Parameters" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
and my XSLT is,

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:styleshe et 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="Calculat orParameters">
<AnyResult />
</xsl:template>

</xsl:stylesheet>
My problem is that the root CalculatorParam eters node is only matched if I
remove its
xmlns="http://www.kettley.com/CalculatorParam eters"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<AnyRe sult />"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:styleshe et
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.


MemoryStrea m transformedXmlS tream = new MemoryStream();

// Transform the file
xslTransform. Transform(xPath Document, null, transformedXmlS tream, null);

// Convert output MemoryStream to a string
ASCIIEncodi ng ascii = new ASCIIEncoding() ;
string outputXmlString =
ascii.GetStri ng(transformedX mlStream.GetBuf fer());


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/frlrfSystemIOSt ringWriterClass Topic.asp>
<http://msdn.microsoft. com/library/default.asp?url =/library/en-us/cpref/html/frlrfSystemXmlX slXslTransformC lassTransformTo pic14.asp>
e.g. pseudo code
StringWriter stringResult = new StringWriter();
xslTransform.Tr ansform(xPathDo cument, null, stringResult, null);
string result = stringResult.To String();
stringResult.Cl ose()

--

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*******@yaho o.de> wrote in message
news:%2******** ********@TK2MSF TNGP14.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:styleshe et
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.


MemoryStre am transformedXmlS tream = new MemoryStream();

// Transform the file
xslTransform .Transform(xPat hDocument, null, transformedXmlS tream, null);

// Convert output MemoryStream to a string
ASCIIEncodin g ascii = new ASCIIEncoding() ;
string outputXmlString =
ascii.GetStr ing(transformed XmlStream.GetBu ffer());


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/frlrfSystemIOSt ringWriterClass Topic.asp>
<http://msdn.microsoft. com/library/default.asp?url =/library/en-us/cpref/html/frlrfSystemXmlX slXslTransformC lassTransformTo pic14.asp>
e.g. pseudo code
StringWriter stringResult = new StringWriter();
xslTransform.Tr ansform(xPathDo cument, null, stringResult, null);
string result = stringResult.To String();
stringResult.Cl ose()

--

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******** ******@TK2MSFTN GP15.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*******@yaho o.de> wrote in message
news:%2******** ********@TK2MSF TNGP14.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:styleshe et
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.


MemoryStrea m transformedXmlS tream = new MemoryStream();

// Transform the file
xslTransfor m.Transform(xPa thDocument, null, transformedXmlS tream, null);

// Convert output MemoryStream to a string
ASCIIEncodi ng ascii = new ASCIIEncoding() ;
string outputXmlString =
ascii.GetSt ring(transforme dXmlStream.GetB uffer());


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/frlrfSystemIOSt ringWriterClass Topic.asp>
<http://msdn.microsoft. com/library/default.asp?url =/library/en-us/cpref/html/frlrfSystemXmlX slXslTransformC lassTransformTo pic14.asp>
e.g. pseudo code
StringWriter stringResult = new StringWriter();
xslTransform.Tr ansform(xPathDo cument, null, stringResult, null);
string result = stringResult.To String();
stringResult.Cl ose()

--

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
1959
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 elements that contain the namespace declaration for "goop" and one that matches the elements that contain the namespace declaration for "gleep".
7
2497
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 hierarchy (by the key) I can set the CurrentY parameter by itself + some constant correctly. Hence which each call the CurrentY gets bigger. But when...
1
1378
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
1831
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 for the input to the Transform method and specifying a Stream for the output. Here is an example of a transform and a document (it's the...
6
2399
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 "number($years) + 1" two times in common.xsl. But the transformation doesn't work anyway. The funny thing is, that if I use the msxsl command line...
3
2260
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 use this so that we can specify different templates to be used by the same xml node. Know that we could use global params, but would rather not...
1
1500
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 application hangs. When I load the XML in a browser with a reference to the XSL template, it works fine. Here is my XML:
3
1791
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 errors. The memory consumed by the process exceeds the size of 306 MB and the IIS process is recycled, resulting in a "Server Application Unavailable"...
4
3906
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 light. My XML is generated by InfoPath and of the structure: ....
0
7470
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7811
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5975
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5334
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4949
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3455
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1887
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1019
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.