BaseUri ignored for document function?
Added by Anonymous over 14 years ago
Legacy ID: #8325933 Legacy Poster: Jürgen Bayer (daraca)
I process a Schematron schema using the Schematron XSLT implementation and Saxon HE on .NET. The schema includes the document funtion to refer to an external document. The document function works as long as the external document is in the application folder. If it is in another folder instead and I set DocumentBuilder.BaseUri to that folder, I get the error "FODC0002: Exception thrown by URIResolver: Could not find file '<Filename in application folder!>". I reproduced this issue with an application doing just a simple XSL transformation using document. So it seems that the XSLT processor ignores BaseUri at least for the document function. Is that right? Here is my demo XSL: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="text" /> <xsl:template match="/"> <xsl:for-each select="Test/customerId"> Customer ID: <xsl:value-of select="."/> <xsl:if test="document('Customers.xml')/customers/customer/@id = ."> Found </xsl:if> </xsl:for-each> </xsl:template> </xsl:stylesheet> The XML file I checked against: 21 42 7 The Customers.Xml file: Magrathea Planet Building Ltd Megadodo Publications The code: string appPath = System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetEntryAssembly().Location); string xmlFileName = Path.Combine(appPath, "Test.xml"); string xslFileName = Path.Combine(appPath, "Test.xsl"); using (FileStream documentStream = new FileStream(xmlFileName, FileMode.Open, FileAccess.Read)) { using (FileStream xsltStream = new FileStream(xslFileName, FileMode.Open, FileAccess.Read)) { Processor processor = new Processor(); DocumentBuilder documentBuilder = processor.NewDocumentBuilder(); string baseFolder = Path.GetFullPath(Path.Combine(appPath, "..\..\")); documentBuilder.BaseUri = new Uri(baseFolder); XdmNode input = documentBuilder.Build(documentStream); var xsltCompiler = processor.NewXsltCompiler(); XsltTransformer transformer = xsltCompiler.Compile(xsltStream).Load(); transformer.InitialContextNode = input; Serializer serializer = new Serializer(); string resultFileName = Path.Combine(appPath, "Result.txt"); using (FileStream resultStream = new FileStream(resultFileName, FileMode.Create, FileAccess.Write)) { StreamWriter outputWriter = new StreamWriter(resultStream); serializer.SetOutputWriter(outputWriter); transformer.Run(serializer); } Process.Start(resultFileName); }
Replies (1)
RE: BaseUri ignored for document function? - Added by Anonymous over 14 years ago
Legacy ID: #8325959 Legacy Poster: Michael Kay (mhkay)
When the argument to document() is a relative URI supplied as a string, it is resolved relative to the base URI of the stylesheet. If you want it resolved relative to the base URI of the source document, use document('customers.xml', /). The second argument is a node whose base URI is used for resolving the first argument. Alternatively, control it yourself using doc(resolve-uri('customers.xml', base-uri(/))) [I hope I've got the arguments in the right order!].
Please register to reply