XSLT

T

Tony Johansson

Hello!
I have the code and an xsl file below. I have also listed the class
BookUtils
When I run this code I get run time exception when this statement
trans.Load("booksarg.xsl");
is executed and generates this exception
"XslLoadException was unhandled
The Prefix BookUtils is not defined

So I assume that I have done some mistake when defining that the
BookUtils:ShowText() method should be called in the booksarg.xsl file

Does anybody know how I should write this to make it work ?

private void button5_Click(object sender, EventArgs e)
{
XPathDocument doc = new XPathDocument("books.xml");
XslCompiledTransform trans = new XslCompiledTransform();
trans.Load("booksarg.xsl");
XmlWriter writer = new XmlTextWriter("argSample.xml", null);
XsltArgumentList argBook = new XsltArgumentList();
BookUtils bu = new BookUtils();
argBook.AddExtensionObject("urn:XslSample", bu);
XPathNavigator nav = doc.CreateNavigator();
trans.Transform(nav, argBook, writer);
writer.Close();
string s = AppDomain.CurrentDomain.BaseDirectory + "argSample.xml";
webBrowser1.Navigate(s);
}

public class BookUtils
{
public BookUtils() {}

public string ShowText()
{
return "This came from the ShowText method!";
}
}


// File: booksarg.xsl
//**********
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:blush:utput method="xml" indent="yes"/>

<xsl:template match="/">
<xsl:element name="books">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>

<xsl:template match="bookstore">
<xsl:apply-templates select="book"/>
</xsl:template>


<xsl:template match="book">
<xsl:element name="discbook">
<xsl:element name="booktitle">
<xsl:value-of select="title"/>
</xsl:element>
<xsl:element name="ShowText">
<xsl:value-of select="BookUtils:ShowText()"/>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

//Tony
 
A

Arne Vajhøj

Hello!
I have the code and an xsl file below. I have also listed the class
BookUtils
When I run this code I get run time exception when this statement
trans.Load("booksarg.xsl");
is executed and generates this exception
"XslLoadException was unhandled
The Prefix BookUtils is not defined

So I assume that I have done some mistake when defining that the
BookUtils:ShowText() method should be called in the booksarg.xsl file

Does anybody know how I should write this to make it work ?

private void button5_Click(object sender, EventArgs e)
{
XPathDocument doc = new XPathDocument("books.xml");
XslCompiledTransform trans = new XslCompiledTransform();
trans.Load("booksarg.xsl");
XmlWriter writer = new XmlTextWriter("argSample.xml", null);
XsltArgumentList argBook = new XsltArgumentList();
BookUtils bu = new BookUtils();
argBook.AddExtensionObject("urn:XslSample", bu);
XPathNavigator nav = doc.CreateNavigator();
trans.Transform(nav, argBook, writer);
writer.Close();
string s = AppDomain.CurrentDomain.BaseDirectory + "argSample.xml";
webBrowser1.Navigate(s);
}

public class BookUtils
{
public BookUtils() {}

public string ShowText()
{
return "This came from the ShowText method!";
}
}


// File: booksarg.xsl
//**********
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:blush:utput method="xml" indent="yes"/>

<xsl:template match="/">
<xsl:element name="books">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>

<xsl:template match="bookstore">
<xsl:apply-templates select="book"/>
</xsl:template>


<xsl:template match="book">
<xsl:element name="discbook">
<xsl:element name="booktitle">
<xsl:value-of select="title"/>
</xsl:element>
<xsl:element name="ShowText">
<xsl:value-of select="BookUtils:ShowText()"/>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

Just change:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

to:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:BookUtils="urn:XslSample">

to glue the namespace to the id given in the C# code.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top