472,336 Members | 1,306 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Extracting custom data with an XSLT?


I'm pulling my hair out here. First, I'm new to XML, so that doesn't
help, but none of this makes any sense to me. All I'm trying to do is
take a simple source XML file and translate it with an XSLT to produce
HTML code.

The problem I have is that no matter what translation it runs through,
it ALWAYS includes data that I don't match in the XSLT!! All I want to
do is extract specific fields from the XML. Here's the XML source.
Simple...

<?xml version="1.0"?>
<rss version="0.92">
<channel>

<docs>http://backend.userland.com/rss092</docs>
<title>Comics: Calvin and Hobbes</title>
<description>Comics: Calvin and
Hobbes</description>

<managingEditor>cr*******@livejournal.com</managingEditor>

<link>http://www.ucomics.com/calvinandhobbes/</link>
<lastBuildDate>Sat, 27 Dec 2003 01:00:02
GMT</lastBuildDate>
<item>
<title>Comics: Calvin and Hobbes</title>

<link>http://images.ucomics.com/comics/ch/1992/ch921226.gif</link>
<description>&lt;img

src=&quot;http://images.ucomics.com/comics/ch/1992/ch921226.gif";
border=&quot;0&quot;
alt=&quot;Today&apos;s Calvin and
Hobbes&quot;&gt;
</description>
</item>
</channel>
</rss>

Now, here's the VB.NET code that I use to apply my XSLT:

Dim xslt As New XslTransform
Dim doc As New XmlDocument
Dim writer As XmlTextWriter = New
XmlTextWriter("c:\temp\transform.html", Nothing)

doc.Load("c:\temp\XMLtest.xml")

Try
xslt.Load("c:\temp\XMLtest.xslt")
xslt.Transform(doc, Nothing, writer, Nothing)

Catch ex As Xsl.XsltException
Console.WriteLine("Transform Failed")

Catch ex As Exception
Console.WriteLine(ex.Message & " " & ex.StackTrace)

End Try

MsgBox("DONE!")

Now, I have tried SO many variations of an XSLT I don't even not what
to post here. I'm taking from 3 books, and the problem is that the
exported HTML ALWAYS includes the data associated with the items that
I'm not attempting to match!

Here's my latest failure of an XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" encoding="utf-8" />

<xsl:template match="channel">
<xsl:text>
CORE CHANNEL:
</xsl:text>

<xsl:apply-templates/>
</xsl:template>

<xsl:template match="channel/item">
TITLE <xsl:value-of select="title"/>
</xsl:template>

</xsl:stylesheet>

And here's the result:

CORE CHANNEL: http://backend.userland.com/rss092Comics: Calvin and
HobbesComics: Calvin and
Hobbescr*******@livejournal.comhttp://www.ucomics.com/calvinandhobbes/Sat,
27 Dec 2003 01:00:02 GMT TITLE Comics: Calvin and Hobbes

PLEASE help me get around this pathetic problem. Obviously there's
some unnamed rule, but the 3 books that I have on the subject mention
NOTHING about this because all the examples contain data that's all
used; no throw away data.

So, how do I throw away the data I don't want when transforming with an
XSLT?

Thanks!
Moogy
------------------------------------------------------------------------
Posted via http://www.mcse.ms
------------------------------------------------------------------------
View this thread: http://www.mcse.ms/message217830.html

Nov 12 '05 #1
4 2049

"Moogy" <Mo*********@mail.mcse.ms> wrote in message
news:Mo*********@mail.mcse.ms...

So, how do I throw away the data I don't want when transforming with an
XSLT?


Read about "built-in templates" and deafault processing in XSLT.

As a rule, whenever unwanted elements are being processed and their text
node descendents appear in the output, this means that you have an
xsl:apply-templates instruction, which selects for processing some nodes,
for which you haven't provided a matching template. In such a case the XSLT
processor uses its built-in templates. The net result is that all text-node
descendents get copied to the output.

In your case the solution is simple: just match a text node with an empty
rule like this:

<xsl:template match="text()"/>
Hope this helped.

Dimitre Novatchev.
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html
Nov 12 '05 #2
Hi,
I'm pulling my hair out here. First, I'm new to XML, so that doesn't
help, but none of this makes any sense to me.


There is a great article explaining RSS Feeds/XML at
http://www.softwaremarketingresource.com/article18.html
that will likely help you understand a little more.

Best of luck
Rachel Small

Nov 12 '05 #3
Moogy,

Dimitre explained this well, but there is a cheap and dirty fix if it helps.

The default processing starts at the root and goes down each branch in your
XML looking for matching templates (with no mode). When it finds a match,
portions of your document that are closer to the leaf are considered
handled.

Thus you can include a root node that forces a single entry point into your
processing. You can explicilty control all further processing through
xsl:apply-templates and mode values. It's approches heresy, but it's very
effective.

--
Kathleen Dollard
Microsoft MVP
Author "Code Generation in Microsoft .NET"
"Moogy" <Mo*********@mail.mcse.ms> wrote in message
news:Mo*********@mail.mcse.ms...

I'm pulling my hair out here. First, I'm new to XML, so that doesn't
help, but none of this makes any sense to me. All I'm trying to do is
take a simple source XML file and translate it with an XSLT to produce
HTML code.

The problem I have is that no matter what translation it runs through,
it ALWAYS includes data that I don't match in the XSLT!! All I want to
do is extract specific fields from the XML. Here's the XML source.
Simple...

<?xml version="1.0"?>
<rss version="0.92">
<channel>

<docs>http://backend.userland.com/rss092</docs>
<title>Comics: Calvin and Hobbes</title>
<description>Comics: Calvin and
Hobbes</description>

<managingEditor>cr*******@livejournal.com</managingEditor>

<link>http://www.ucomics.com/calvinandhobbes/</link>
<lastBuildDate>Sat, 27 Dec 2003 01:00:02
GMT</lastBuildDate>
<item>
<title>Comics: Calvin and Hobbes</title>

<link>http://images.ucomics.com/comics/ch/1992/ch921226.gif</link>
<description>&lt;img

src=&quot;http://images.ucomics.com/comics/ch/1992/ch921226.gif";
border=&quot;0&quot;
alt=&quot;Today&apos;s Calvin and
Hobbes&quot;&gt;
</description>
</item>
</channel>
</rss>

Now, here's the VB.NET code that I use to apply my XSLT:

Dim xslt As New XslTransform
Dim doc As New XmlDocument
Dim writer As XmlTextWriter = New
XmlTextWriter("c:\temp\transform.html", Nothing)

doc.Load("c:\temp\XMLtest.xml")

Try
xslt.Load("c:\temp\XMLtest.xslt")
xslt.Transform(doc, Nothing, writer, Nothing)

Catch ex As Xsl.XsltException
Console.WriteLine("Transform Failed")

Catch ex As Exception
Console.WriteLine(ex.Message & " " & ex.StackTrace)

End Try

MsgBox("DONE!")

Now, I have tried SO many variations of an XSLT I don't even not what
to post here. I'm taking from 3 books, and the problem is that the
exported HTML ALWAYS includes the data associated with the items that
I'm not attempting to match!

Here's my latest failure of an XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" encoding="utf-8" />

<xsl:template match="channel">
<xsl:text>
CORE CHANNEL:
</xsl:text>

<xsl:apply-templates/>
</xsl:template>

<xsl:template match="channel/item">
TITLE <xsl:value-of select="title"/>
</xsl:template>

</xsl:stylesheet>

And here's the result:

CORE CHANNEL: http://backend.userland.com/rss092Comics: Calvin and
HobbesComics: Calvin and
Hobbescr*******@livejournal.comhttp://www.ucomics.com/calvinandhobbes/Sat,
27 Dec 2003 01:00:02 GMT TITLE Comics: Calvin and Hobbes

PLEASE help me get around this pathetic problem. Obviously there's
some unnamed rule, but the 3 books that I have on the subject mention
NOTHING about this because all the examples contain data that's all
used; no throw away data.

So, how do I throw away the data I don't want when transforming with an
XSLT?

Thanks!
Moogy
------------------------------------------------------------------------
Posted via http://www.mcse.ms
------------------------------------------------------------------------
View this thread: http://www.mcse.ms/message217830.html

Nov 12 '05 #4

"Kathleen Dollard" <Ka******@mvps.org> wrote in message
news:Oz**************@TK2MSFTNGP10.phx.gbl...
Moogy,
[snip]
Thus you can include a root node that forces a single entry point into your processing. You can explicilty control all further processing through
xsl:apply-templates and mode values. It's approches heresy, but it's very
effective.


Sure, this is a very appropriate advice.

It is not "heresy" :o) -- only there are some rare cases when one has to
write completely generic stylesheets in pure push style, that should work
with any, unknown in advance source xml document -- in any such case
disregarding certain types of nodes can be guaranteed by specifying an empty
template rule.

One can also argue that "push style" is more maintainable than "pull
style" -- I am not a fanatic at all, but it is good to list all pros and
cons when giving a recommendation.
Happy New Year!
Dimitre Novatchev.
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html
Nov 12 '05 #5

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

Similar topics

7
by: Luc Tremblay | last post by:
Given the typical following code: void Listener::HandleEvent(const Event& event) { // handling code } In a "clean" fashion, how is it...
5
by: Michael Hill | last post by:
Hi, folks. I am writing a Javascript program that accepts (x, y) data pairs from a text box and then analyzes that data in various ways. This is...
0
by: suresh | last post by:
hi, how do we define the custom data using solutionXML tag in Visio XML file. I have opened the already existing XML file and defined the data...
0
by: Bret Pehrson | last post by:
I'm using a custom data source for my (WinForms) datagrid: public class MyDataSource : IList { // ... } each row is a: public class...
5
by: Richard Bysouth | last post by:
Hi I am trying to create an object that I can use to bind to a grid in my Win app. All of the examples that I have found on custom data sources...
6
by: Gaz | last post by:
Hi guys. I've been lookig for this in the numpy pdf manual, in this group and on google, but i could not get an answer... Is there a way to...
2
by: renatois | last post by:
DropDownList with custom Data Display in a GridView Hi all, I have a GridView that has a STATUS template field showing a DropDownList on Edit Mode...
1
by: jehugaleahsa | last post by:
Hello: I am experiencing performance related issues when my custom data structures work with value types. I use generics to prevent boxing...
0
by: napstar | last post by:
I would also like to know how to create a custom data set in SSRS
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

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.