BaseX: where to declare the XML document on which to perform a query - xslt

With the program BaseX I was able to use XPath and XQuery in order to query an XML document located at my home directory, but I have a problem with doing the same in XSLT.
The document I'm querying is BookstoreQ.xml.
XPath version, running totally fine:
doc("/home/ioannis/Desktop/BookstoreQ.xml")/Bookstore/Book/Title
XSLT code which I want to execute:
<xsl:stylesheet version = "2.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:output method= "xml" indent = "yes" omit-xml-declaration = "yes" />
<xsl:template match = "Book"></xsl:template>
</xsl:stylesheet>
I read BaseX' documentation on XSLT, but didn't manage to find a solution. How can I run given XSLT?

BaseX has no direct support for XSLT, you have to call it using XQuery functions (which is easy, though). There are two functions for doing this, one for returning XML nodes (xslt:transform(...)), one for returning text as a string (xslt:transform-text(...)). You need the second one.
xslt:transform-text(doc("/home/ioannis/Desktop/BookstoreQ.xml"),
<xsl:stylesheet version = "2.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:output method= "xml" indent = "yes" omit-xml-declaration = "yes" />
<xsl:template match = "Book"></xsl:template>
</xsl:stylesheet>
)
Both can either be called with the XSLT as nodes (used here), by passing it as a string or giving a path to a file containing the XSLT code.

Related

Passing a node as parameter to a XSL stylesheet

I need to pass a node as a parameter to an XSL stylesheet. The issue is that the parameter gets sent as a string. I have seen the several SO questions regarding this topic, and I know that the solution (in XSLT 1.0) is to use an external node-set() function to transform the string to a node set.
My issue is that I am using eXist DB I cannot seem to be able to get its XSLT processor to locate any such function. I have tried the EXSLT node-set() from the namespace http://exslt.org/common as well as both the Saxon and Xalan version (I think eXist used to use Xalan but now it might be Saxon).
Are these extensions even allowed in the XSLT processor used by eXist? If not, is there something else I can do?
To reference or transform documents from the database, you should pass the path as a parameter to the transformation, and then refer to it using a parameter and variable
(: xquery :)
let $path-to-document := "/db/test/testa.xml"
let $stylesheet :=
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="source" required="no"/>
<xsl:variable name="error"><error>doc not available</error></xsl:variable>
<xsl:variable name="theDoc" select="if (doc-available($source)) then doc($source) else $error"/>
<xsl:template match="/">
<result><xsl:value-of select="$source"/> - <xsl:value-of select="node-name($theDoc/*)"/></result>
</xsl:template>
</xsl:stylesheet>
return transform:transform(<dummy/>,$stylesheet, <parameters><param name="source" value="xmldb:exist://{$path-to-document}"/></parameters>)
As per Martin Honnen's comments I don't think it is possible to pass an XML node via the <parameters> structure of the transform:transform() function in eXist. The function seems to strip away any XML tags passed to it as a value.
As a workaround I will wrap both my input XML and my parameter XML into a root element and pass that as input to the transform function.

XSLT transformation passing parameters

I am trying to pass parameters during an XSLT transformation. Here is the xsl stylesheet.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="param1" select="'defaultval1'" />
<xsl:param name="param2" select="'defaultval2'" />
<xsl:template match="/">
<xslttest>
<tagg param1="{$param1}"><xsl:value-of select="$param2" /></tagg>
</xslttest>
</xsl:template>
</xsl:stylesheet>
The following in the java code.
File xsltFile = new File("template.xsl");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document stylesheet = builder.parse("template.xsl");
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer xsltTransformer = transformerFactory.newTransformer(new DOMSource(stylesheet));
//Transformer xsltTransformer = transformerFactory.newTransformer(new StreamSource(xsltFile));
xsltTransformer.setParameter("param1", "value1");
xsltTransformer.setParameter("param2", "value2");
StreamResult result = new StreamResult(System.out);
xsltTransformer.transform(new DOMSource(builder.newDocument()), result);
I get following errors:-
ERROR: 'Variable or parameter 'param1' is undefined.'
FATAL ERROR: 'Could not compile stylesheet'
However, if i use the following line to create the transformer everything works fine.
Transformer xsltTransformer = transformerFactory.newTransformer(new StreamSource(xsltFile));
Q1. I just wanted to know whats wrong in using a DOMSource in creating a Transformer.
Q2. Is this one of the ideal ways to substitute values for placeholders in an xml document? If my placeholders were in a source xml document is there any (straightforward) way to substitute them using style sheets (and passing parameters)?
Q1: This is a namespace awareness problem. You need to make the DocumentBuilderFactory namespace aware:
factory.setNamespaceAware(true);
Q2: There are several ways to get the values from an external xml file. One way to do this is with the document function and a top level variable in the document:
<!-- Loads a map relative to the template. -->
<xsl:variable name="map" select="document('map.xml')"/>
Then you can select the values out of the map. For instance, if map.xml was defined as:
<?xml version="1.0" encoding="UTF-8"?>
<mappings>
<mapping key="value1">value2</mapping>
</mappings>
You could remove the second parameter from your template, then look up the value using this line:
<tagg param1="{$param1}"><xsl:value-of select="$map/mappings/mapping[#key=$param1]"/></tagg>
Be aware that using relative document URIs will require that the stylesheet has a system id specified, so you will need to update the way you create your DOMSource:
DOMSource source = new DOMSource();
source.setNode(stylesheet);
source.setSystemId(xsltFile.toURL().toString());
In general, I suggest looking at all of the options that are available in Java's XML APIs. Assume that all of the features available are set wrong for what you are trying to do. I also suggest reading the XML Information Set. That specification will give you all of the definitions that the API authors are using.

How can I get the 'key' of a keyword from an XSLT TBB?

I am working on the XSLT TBB (using the XSLT Mediator on Tridion 2011 SP1) to retrieve the key Value from the Keyword.
My Keyword looks like this.
Value: Some Value
Key: Its ID is 123
It's a normal Keyword.
I have created a schema with a field. The values will be selected from List and from Category.
The component Source looks like this:
This is the Component source directly taken from the component of Tridion UI.
<Content xmlns="Some Name space">
<keywordlink xlink:href="tcm:202-9737-1024" xlink:title="Some Value"
xmlns:xlink="http://www.w3.org/1999/xlink">Some Value</keywordlink>
</Content>
When I observed the tcm:Component source from template builder, I observed that there are no attributes present for the field.
<Content xmlns="Some Name space">
<keywordlink>Some Value</keywordlink>
</Content>
I want to retrieve the Key value of the keyword.
I wrote a XSLT TBB like this. I am using XSLT mediator to execute XSLT TBBs.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:simple="Some Name space"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:tcm="http://www.tridion.com/ContentManager/5.0"
xmlns:xh="http://www.w3.org/1999/xhtml"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns:transform-ext="urn:tridion:transform-ext"
xmlns="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="#default simple xh">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:apply-templates
select="tcm:Component/tcm:Data/tcm:Content/simple:Content" />
</xsl:template>
<xsl:template match="simple:Content">
<xsl:value-of select="simple:keywordlink/#*"/>
<xsl:value-of select=document(simple:keywordlink/#xlink:href)/>
</xsl:template>
<xsl:stylesheet>
I am getting blank output. I want to get the key value of a keyword.
I am getting blank output because in tcm:Component XML, there are no attributes.
I am not sure how can I navigate to that keyword.
I should retrieve the value of Key i.e. "Its ID is 123".
Can any one help how to do this?
It seems it's impossible to get an xlink:href to the referred keyword in a Keyword field using just the XSLT Mediator.
To overcome this I created a .NET compound that "inflates" the extra keyword info in the XML. You'll have to place this compound just before the XSLT compound.
Code:
namespace ContentManagement.TBB.Templates
{
[TcmTemplateTitle("Inflate Keyword Info")]
public class GetExtendedComponent : TemplateBase
{
public override void Transform(Engine engine, Package package)
{
Initialize(engine, package);
Component component = GetComponent();
XmlElement componentXml = component.ToXml();
XmlNamespaceManager ns = new XmlNamespaceManager(componentXml.OwnerDocument.NameTable);
ns.AddNamespace("ns", component.Schema.NamespaceUri);
ItemFields fields = new ItemFields(component.Content, component.Schema);
InflateKeywords(fields, (XmlElement)componentXml.SelectSingleNode(String.Format("//ns:{0}", component.Schema.RootElementName), ns));
ItemFields metaFields = new ItemFields(component.Metadata, component.MetadataSchema);
InflateKeywords(metaFields, (XmlElement)componentXml.SelectSingleNode("//ns:Metadata", ns));
Item xmlItem = package.CreateStringItem(ContentType.Component, componentXml.OuterXml);
package.Remove(package.GetByName(Package.ComponentName));
package.PushItem(Package.ComponentName, xmlItem);
}
private void InflateKeywords(ItemFields fields, XmlElement element)
{
XmlNamespaceManager ns = new XmlNamespaceManager(element.OwnerDocument.NameTable);
ns.AddNamespace("ns", element.NamespaceURI);
Logger.Debug("NS: " + element.NamespaceURI);
foreach (ItemField field in fields)
{
if (field is KeywordField)
{
KeywordField keywordField = (KeywordField)field;
XmlNodeList nodes = element.SelectNodes(String.Format("./ns:{0}", keywordField.Name), ns);
foreach (XmlNode node in nodes)
{
XmlElement kwelement = (XmlElement)node;
Logger.Debug(String.Format("Keyword titel: {0}", keywordField.Value.Title));
Logger.Debug(String.Format("Keyword Element Value: {0}", kwelement.InnerText));
kwelement.SetAttribute("href", "http://www.w3.org/1999/xlink", keywordField.Values.First(v => v.Title.Equals(kwelement.InnerText)).Id);
kwelement.SetAttribute("type", "http://www.w3.org/1999/xlink", "simple");
kwelement.SetAttribute("title", "http://www.w3.org/1999/xlink", kwelement.InnerText);
}
}
else if (field is EmbeddedSchemaField)
{
EmbeddedSchemaField embedField = (EmbeddedSchemaField)field;
XmlNodeList nodes = element.SelectNodes(String.Format("./ns:{0}", embedField.Name), ns);
int i = 0;
foreach (XmlNode node in nodes)
{
XmlElement embedElement = (XmlElement)node;
InflateKeywords(embedField.Values[i], embedElement);
i++;
}
}
}
}
}
}
The Key of the Keyword is not stored in the link (which really only contains the minimal information needed to look up the Keyword). So you'll have to load the Keyword and read it from there.
Yoav showed how to read other items from within your XSLT here:
http://yoavniran.wordpress.com/2009/07/11/implementing-the-xslt-mediator-part-1/
This snippet seems relevant:
<xsl:attribute name="alt">
<xsl:value-of select="document(simple:image/#xlink:href)/tcm:Component/tcm:Data/tcm:Metadata/image:Metadata/image:altText"/>
</xsl:attribute>
So the document() call loads the linked item (in this case a multimedia component) and the rest of the select then finds the value they are looking for.
A keyword XML looks like this:
<?xml version="1.0"?>
<tcm:Keyword xmlns:transform-ext="urn:tridion:transform-ext"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:tcm="http://www.tridion.com/ContentManager/5.0"
IsEditable="false" ID="tcm:1-233-1024">
<tcm:Context>
<tcm:Publication xlink:title="000 Parent Publication" xlink:href="tcm:0-1-1"
xlink:type="simple"/>
<tcm:OrganizationalItem xlink:title="Places" xlink:href="tcm:1-37-512"
xlink:type="simple"/>
</tcm:Context>
<tcm:Info>
<tcm:LocationInfo>
<tcm:WebDAVURL>/webdav/000%20Parent%20Publication/Places/New%20Keyword.tkw</tcm:WebDAVURL>
<tcm:Path>\000 Parent Publication\Places</tcm:Path>
</tcm:LocationInfo>
<tcm:BluePrintInfo>
<tcm:OwningPublication xlink:title="000 Parent Publication" xlink:href="tcm:0-1-1" xlink:type="simple"/>
<tcm:IsShared>false</tcm:IsShared>
<tcm:IsLocalized>false</tcm:IsLocalized>
</tcm:BluePrintInfo>
<tcm:VersionInfo>
<tcm:CreationDate>2012-06-11T09:09:03</tcm:CreationDate>
<tcm:RevisionDate>2012-06-11T09:09:03</tcm:RevisionDate>
<tcm:Creator xlink:title="TCMHOSTNAME\Administrator"
xlink:href="tcm:0-11-65552" xlink:type="simple"/>
</tcm:VersionInfo>
<tcm:AllowedActions>
<tcm:Actions Managed="0" Deny="96" Allow="268560384"/>
</tcm:AllowedActions>
</tcm:Info>
<tcm:Data>
<tcm:Title>New Keyword</tcm:Title>
<tcm:Description>New Keyword</tcm:Description>
<tcm:Key>Key</tcm:Key>
<tcm:IsAbstract>false</tcm:IsAbstract>
<tcm:ParentKeywords/>
<tcm:RelatedKeywords/>
<tcm:MetadataSchema xlink:title="" xlink:href="tcm:0-0-0" xlink:type="simple"/>
<tcm:Metadata/>
<tcm:IsRoot>true</tcm:IsRoot>
</tcm:Data>
</tcm:Keyword>
Implement this way:-
To get the Value field:-
document(simple:keywordlink/#xlink:href)/tcm:Keyword/tcm:Data/tcm:Title/text()
<xsl:value-of select="document(simple:keywordlink/#xlink:href)/tcm:Keyword/tcm:Data/tcm:Title/text()" />
Result:
Some Value
To get the Key field:-
document(simple:keywordlink/#xlink:href)/tcm:Keyword/tcm:Data/tcm:Key/text()
<xsl:value-of select="document(simple:keywordlink/#xlink:href)/tcm:Keyword/tcm:Data/tcm:Key/text()" />
Result:
Its ID is 123

Can an XSLT insert the current date?

A program we use in my office exports reports by translating a XML file it exports with an XSLT file into XHTML. I'm rewriting the XSLT to change the formatting and to add more information from the source XML File.
I'd like to include the date the file was created in the final report. But the current date/time is not included in the original XML file, nor do I have any control on how the XML file is created. There doesn't seem to be any date functions building into XSLT that will return the current date.
Does anyone have any idea how I might be able to include the current date during my XSLT transformation?
XSLT 2
Date functions are available natively, such as:
<xsl:value-of select="current-dateTime()"/>
There is also current-date() and current-time().
XSLT 1
Use the EXSLT date and times extension package.
Download the date and times package from GitHub.
Extract date.xsl to the location of your XSL files.
Set the stylesheet header.
Import date.xsl.
For example:
<xsl:stylesheet version="1.0"
xmlns:date="http://exslt.org/dates-and-times"
extension-element-prefixes="date"
...>
<xsl:import href="date.xsl" />
<xsl:template match="//root">
<xsl:value-of select="date:date-time()"/>
</xsl:template>
</xsl:stylesheet>
Do you have control over running the transformation? If so, you could pass in the current date to the XSL and use $current-date from inside your XSL. Below is how you declare the incoming parameter, but with knowing how you are running the transformation, I can't tell you how to pass in the value.
<xsl:param name="current-date" />
For example, from the bash script, use:
xsltproc --stringparam current-date `date +%Y-%m-%d` -o output.html path-to.xsl path-to.xml
Then, in the xsl you can use:
<xsl:value-of select="$current-date"/>
For MSXML parser, try this:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:my="urn:sample" extension-element-prefixes="msxsl">
<msxsl:script language="JScript" implements-prefix="my">
function today()
{
return new Date();
}
</msxsl:script>
<xsl:template match="/">
Today = <xsl:value-of select="my:today()"/>
</xsl:template>
</xsl:stylesheet>
Also read XSLT Stylesheet Scripting using msxsl:script and Extending XSLT with JScript, C#, and Visual Basic .NET
...
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:local="urn:local" extension-element-prefixes="msxsl">
<msxsl:script language="CSharp" implements-prefix="local">
public string dateTimeNow()
{
return DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssZ");
}
</msxsl:script>
...
<xsl:value-of select="local:dateTimeNow()"/>
Late answer, but my solution works in Eclipse XSLT. Eclipse uses XSLT 1 at time of this writing. You can install an XSLT 2 engine like Saxon. Or you can use the XSLT 1 solution below to insert current date and time.
<xsl:value-of select="java:util.Date.new()"/>
This will call Java's Data class to output the date. It will not work unless you also put the following "java:" definition in your <xsl:stylesheet> tag.
<xsl:stylesheet [...snip...]
xmlns:java="java"
[...snip...]>
I hope that helps someone. This simple answer was difficult to find for me.
format-date(current-date(), '[M01]/[D01]/[Y0001]') = 09/19/2013
format-time(current-time(), '[H01]:[m01] [z]') = 09:26 GMT+10
format-dateTime(current-dateTime(), '[h1]:[m01] [P] on [MNn] [D].') = 9:26 a.m. on September 19.
reference: Formatting Dates and Times using XSLT 2.0 and XPath

Xslt transform on special characters

I have an XML document that needs to pass text inside an element with an '&' in it.
This is called from .NET to a Web Service and comes over the wire with the correct encoding &
e.g.
T&O
I then need to use XSLT to create a transform but need to query SQL server through a SP without the encoding on the Ampersand e.g T&O would go to the DB.
(Note this all has to be done through XSLT, I do have the choice to use .NET encoding at this point)
Anyone have any idea how to do this from XSLT?
Note my XSLT knowledge isn’t the best to say the least!
Cheers
<xsl:text disable-output-escaping="yes">&<!--&--></xsl:text>
More info at: http://www.w3schools.com/xsl/el_text.asp
If you have the choice to use .NET you can convert between an HTML-encoded and regular string using (this code requires a reference to System.Web):
string htmlEncodedText = System.Web.HttpUtility.HtmlEncode("T&O");
string text = System.Web.HttpUtility.HtmlDecode(htmlEncodedText);
Update
Since you need to do this in plain XSLT you can use xsl:value-of to decode the HTML encoding:
<xsl:variable name="test">
<xsl:value-of select="'T&O'"/>
</xsl:variable>
The variable string($test) will have the value T&O. You can pass this variable as an argument to your extension function then.
Supposing your XML looks like this:
<root>T&O</root>
you can use this XSLT snippet to get the text out of it:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="root"> <!-- Select the root element... -->
<xsl:value-of select="." /> <!-- ...and extract all text from it -->
</xsl:template>
</xsl:stylesheet>
Output (from Saxon 9, that is):
T&O
The point is the <xsl:output/> element. The defauklt would be to output XML, where the ampersand would still be encoded.