469,627 Members | 943 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 1875

"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Luc Tremblay | last post: by
5 posts views Thread by Michael Hill | last post: by
reply views Thread by Bret Pehrson | last post: by
6 posts views Thread by Gaz | last post: by
1 post views Thread by jehugaleahsa | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.