xslt

  • Thread starter Thread starter Tony
  • Start date Start date
T

Tony

Hello!

Here is the C# code that tranform the xml file into html.
Below is both the Xml file and the xslt file.
I just wonder if somebody could show me with a simple example how to use the
anchestor with my Xml file.

protected void Page_Load(object sender, EventArgs e)
{
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(@"C:\Users\Tony\programmering\Websites\slask5\personData.xsl");

// Execute the transform and output the results to a file
xslt.Transform(@"C:\Users\Tony\programmering\Websites\slask5\personData.xml",
@"C:\Users\Tony\programmering\Websites\slask5\personData.html");
}

Here is the Xml file
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="book.xsl"?>
<Books>
<Book>
<Chapter number="1">This is the first chapter</Chapter>
<Chapter number="2">This is the second chapter</Chapter>
<Chapter number="3">This is the third chapter</Chapter>
<Chapter number="4">This is the fourth chapter</Chapter>
<Chapter number="5">This is the fifth chapter</Chapter>
</Book>
</Books>

Here is the xslt file
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<head>
<title>This shows the context position and context size</title>
</head>
<body>
<h3>Context position and context size demo.</h3>
<xsl:apply-templates select="Books/Book/Chapter" />
</body>
</html>
</xsl:template>

<xsl:template match="Chapter">
<xsl:if test="position()=2">

<p>When the context node is the second <b>Chapter</b> element node
then </p>
<p>the context position is <xsl:value-of select="position()" /></p>
<p>and the context size is <xsl:value-of select="last()" /></p>
<p>The text the <b>Chapter</b> element node contain is <xsl:value-of
select="." /></p>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

//Tony
 
Here is the C# code that tranform the xml file into html.
Below is both the Xml file and the xslt file.
I just wonder if somebody could show me with a simple example how to use
the anchestor with my Xml file.

Use what how??
protected void Page_Load(object sender, EventArgs e)
{
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(@"C:\Users\Tony\programmering\Websites\slask5\personData.xsl");

// Execute the transform and output the results to a file
xslt.Transform(@"C:\Users\Tony\programmering\Websites\slask5\personData.xml",
@"C:\Users\Tony\programmering\Websites\slask5\personData.html");
}

Here is the Xml file
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="book.xsl"?>
<Books>
<Book>
<Chapter number="1">This is the first chapter</Chapter>
<Chapter number="2">This is the second chapter</Chapter>
<Chapter number="3">This is the third chapter</Chapter>
<Chapter number="4">This is the fourth chapter</Chapter>
<Chapter number="5">This is the fifth chapter</Chapter>
</Book>
</Books>

Here is the xslt file
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<head>
<title>This shows the context position and context size</title>
</head>
<body>
<h3>Context position and context size demo.</h3>
<xsl:apply-templates select="Books/Book/Chapter" />
</body>
</html>
</xsl:template>

<xsl:template match="Chapter">
<xsl:if test="position()=2">

<p>When the context node is the second <b>Chapter</b> element node then
</p>
<p>the context position is <xsl:value-of select="position()" /></p>
<p>and the context size is <xsl:value-of select="last()" /></p>
<p>The text the <b>Chapter</b> element node contain is <xsl:value-of
select="." /></p>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

Arne
 
Hello!

When I use the ancestor Axis I don't get the expected result from the xslt
file.
Here is all my files that I use.

Here is the Xml file.
***************
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="book2.xsl"?>
<Books>
<Book>
<Chapter number="1">
<Section>This is the first section in chap 1</Section>
<Section>This is the second section in chap 1</Section>
</Chapter>

<Chapter number="2">
<Section>This is the first section in chap 2</Section>
<Section>This is the second section in chap 2</Section>
</Chapter>
</Book>
</Books>

Here is the xslt file
**************
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<head>
<title></title>
</head>

<body>
<h3>Ancestor axis demo.</h3>
<xsl:apply-templates select="/Books/Book/Chapter[1]/Section[2]"/>
</body>

</html>
</xsl:template>

<xsl:template match="Section">
<xsl:for-each select="ancestor::*" >
<p>
<xsl:value-of select="name(.)"/>
which contains the text
<xsl:value-of select="." />
</p>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Here is the result html document
************************
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
</head>
<body>
<h3>Ancestor axis demo.</h3>
<p>Books
which contains the text



This is the first section in chap 1
This is the second section in chap 1



This is the first section in chap 2
This is the second section in chap 2


</p>
<p>Book
which contains the text


This is the first section in chap 1
This is the second section in chap 1



This is the first section in chap 2
This is the second section in chap 2

</p>
<p>Chapter
which contains the text

This is the first section in chap 1
This is the second section in chap 1
</p>
</body>
</html>

Here is the output from the html when running in the browser
**********************************************
Ancestor axis demo.

Books which contains the text This is the first section in chap 1 This is
the second section in chap 1 This is the first section in chap 2 This is the
second section in chap 2

Book which contains the text This is the first section in chap 1 This is the
second section in chap 1 This is the first section in chap 2 This is the
second section in chap 2

Chapter which contains the text This is the first section in chap 1 This is
the second section in chap 1


What I don't understand here is that according to my understanding about
ancestor should this xslt file have given the following
the Chapter element, which has a number attribute node with a value of 1,
the Book element node and the Books element node and the root node.

So as a summary I can't understand how my xslt with the usage of the
ancestor axis can give the result as it does ?

//Tony
 
Back
Top