Click to See Complete Forum and Search --> : xsl rules


manishrathi
09-29-2009, 12:13 AM
<?xml version="1.0">
<?xml-stylesheet type="text/xsl" href="article1.xsl"?>

<article>
<logo href="/rocket/images/logo.gif" />

<headline>Blowing XML Bubbles</headline>
<deck>when will bubble burst ? </deck>
<byline href="http://www.beyondhtml.com/default.asp?/rocket/bio.xml>
Michael Floyd</byline>
<pubDate>March 2000</pubDate>

<aBody>
<para>
<dropCap>A</dropCap>t one time or other, everyone from marketing professors to analyst companies has proposed various models to describe the cycle of high flying technologies. Most picture a seed point followed a phase of tremendous hyping</para>
<para>such a model prompted author to direct , on the <bold>editorial page</bold>of the <bold>June 1996 issue of<italic>Web Techniques</italic></bold>, a coolong period for the Web. </para>
<para>Michael is the publisher of BeyondHTML.com an <italic>Web Techniques</italic>editor at large</para>
</aBody>
<copyright>Copyright Michael Floyd. 1998 - 2000. All Rights Reserved</copyright>
</article>


Following rules are applied to this xml document

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

whats meant by this code ? wherever, "bold" is found in xml document , xsl processor will apply B after it. Does it mean that processor will extract text after the "bold" and apply "B" brfore and after it ? Once "B" is encountered, processor extracts text from tyhere on, but how much text will it extract ? upto</bold> ? when </bold> is encountered, does tyhe processor place</B> before it or after it ?

what does the "apply-templates" do ? How is it different from <xsl:value-of select=""> ? If I place <xsl:value-of select=""> instead of <xsl:apply-templates/> , I will get the same result. What templates are applied when I use <xsl:apply-templates > ? If I dont use, <xsl:apply-templates > in the above example , the will it just <B> be applied after <bold> ?

I have done good enough reading and then I posted the question here. So pls dont point to ant article and pls explain.

thanks

rnd me
10-01-2009, 08:49 PM
<xsl:template match="bold">
<B>
<xsl:apply-templates/>
</B>
</xsl:template>

whats meant by this code ? wherever, "bold" is found in xml document , xsl processor will apply B after it. Does it mean that processor will extract text after the "bold" and apply "B" brfore and after it ? Once "B" is encountered, processor extracts text from tyhere on, but how much text will it extract ? upto</bold> ? when </bold> is encountered, does tyhe processor place</B> before it or after it ?

what does the "apply-templates" do ? How is it different from <xsl:value-of select=""> ? If I place <xsl:value-of select=""> instead of <xsl:apply-templates/> , I will get the same result. What templates are applied when I use <xsl:apply-templates > ? If I dont use, <xsl:apply-templates > in the above example , the will it just <B> be applied after <bold> ?

I have done good enough reading and then I posted the question here. So pls dont point to ant article and pls explain.

thanks

your code would probably output something like:
<B>editorial page</B>
<B>June 1996 issue of Web Techniques</B>

it just matches the text inside of <bold>.


<xsl:value-of select=""> either silently fails, or inserts a blank string, so it's little wonder you see no diff.


<xsl:apply-templates/> invokes all templates that match any part of the current node. it's used to combine, isolate, and recycle partial-page transformations.

given the xml:
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Example document</title>
</head>
<body>
<p>Example paragraph</p>
<p>Another Example</p>
</body>
</html>


you can select and uniquely transform different chunks instead of the whole thing.

for example, lets say we were generating a site summery and wanted to catalog the title and paragraph content of this web page.
we want to get something like:

<page>
<label>my page</label>
<content>content here/<content>
<page>



here is the XSL that uses a couple template matches to do this:



EDIT: ran out of time, have to leave, i will post the XSL later.

rnd me
10-03-2009, 07:10 PM
Ok, forgot about this, sorry about that.

here is the code we need.
note that to better demonstrate a couple extra ways of doing things in xslt, i changed the example html slightly.

i think going backwards (turning HTML into xml) provides a more intuitive example than would starting with abstract data.

the exmaple is extremely commented, and the coloring on this forum seems to break with it.
i suggest you load it into a real editor, it's actually a very simple xsl that only looks long and here...


output:
<page caption="Document Title" size="101">
<content label="Example">
Example paragraph
Another Example
One More Thing
<!--image from: http://www.webdeveloper.com/forum/images/smilies/biggrin.gif-->
<!--image from: http://www.webdeveloper.com/forum/images/smilies/mad.gif-->
</content>
</page>



data.xml
<?xml-stylesheet type="text/xml" href="style.xsl"?>
<html>
<head>
<title>Document Title</title>
</head>
<body class="info">
<h1>Example</h1>
<p>Example paragraph</p>

<p>Another Example</p>
<p>One More Thing</p>
<img src="http://www.webdeveloper.com/forum/images/smilies/biggrin.gif" />
<img src="http://www.webdeveloper.com/forum/images/smilies/mad.gif" />

</body>
</html>




style.xsl
<xsl:stylesheet
version="1.0"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="html" />


<!--define a line-break so we can make the code pretty-->
<xsl:variable name="R"><xsl:text>
</xsl:text></xsl:variable>



<!--main template, one match, produces containers and fetches guts-->
<xsl:template match="/html"> <!--MATCHES: the data document's root -->
<page caption="{head/title}" size="{string-length()}" > <!--the new doc's root tag-->

<content label="{body/h1}"><!-- a wrapper tag for the content, labeled by the first H1 tag in data doc-->
<xsl:apply-templates select="body" /> <!--MATCHES: data docs' body tag-->
</content>

</page>
</xsl:template>
<!-- end main template,
#########################################
( start defining repetitive sub-transforms ) -->

<!--since we want only text content,
all kids of body should spit out blank text by default: -->
<xsl:template match="body/*" /><!--MATCHES: all kids of the data doc's body tag-->

<!--we can't omit the kids completly though, they have sub-kids and content we need.
we still need find more, closer-matching tags.
to make that to happen, let's process the kids of body now: -->
<xsl:template match="body"> <!--MATCHES: the data doc's body tag-->
<xsl:apply-templates select="*" /> <!--MATCHES: all kids of the data doc's body tag-->
</xsl:template>



<!--Images were not part of our specified backup data structure, but i don't want to lose them.
insert the source URL of any image into the new doc as commented text: -->
<xsl:template match="*/img"><!--MATCHES: all image kids of body or one of it's kids -->
<xsl:value-of select='$R' /><!--insert a line-break-->
<!-- create a new comment section in the output, and fill it with the src attrib of the image: -->
<xsl:comment>image from: <xsl:value-of select='@src' /></xsl:comment>

</xsl:template>



<!--the content we want is in paragraphs. proccess them next: -->
<xsl:template match="*/p"><!--MATCHES: all paragraph kids of body or one of it's kids -->
<xsl:value-of select='$R' /> <!--insert a line-break-->
<xsl:value-of select='.' /> <!-- insert the text-only version of each paragraph-->
</xsl:template>


<!-- End of repetitive matching section, End of file-->
</xsl:stylesheet>