I have been using Apache ANT within Eclipse to run XSLT. The advantage to using ANT is you can easily setup a processing pipeline utilizing various XSLT filters.
Example Ant Target:
I have been using Apache ANT within Eclipse to run XSLT. The advantage to using ANT is you can easily setup a processing pipeline utilizing various XSLT filters.
Example Ant Target:
/
()
@
comment()
processing-instruction()
ancestor::
ancestor-or-self::
attribute::(or @)
child::(or nothing)
descendant::
descendant-or-self::
following::
following-sibling::
namespace::
parent::or (or ..)
preceding::
preceding-sibling::
self:: or (.)
Note that the double colon "::" immediately follows the use of an axis name.
Predicates are used to qualify expressions (i.e filter nodes). Predicates are specifiable on any node set and appear in sqare brackets after the node test.
Example:
version[3] |
last()
- When processing a list of nodes, if the nodes are numbered from one, last() returns the last number.
position()
- returns the value of the context position
name()
- returns the name of a node. Good for getting things like the element name for tests.
concat()
- take two or more arguments and joins the results together end-to-end.
count()
- returns the number of nodes present in the node-set
not()
- returns the negation of its argument.
Actually reading the XPath Expressions is probably the easiest way to learn them. Keep in mind that from any position in the document you can write an XPath expression to get to any other node in that document.
para |
matches all para children in the current context |
para/emphasis |
matches all emphasis elements that have a parent of para |
//title |
matches all title elements anywhere in the document |
.//title |
matches all title elements that are descendants of the current context |
item[1] |
matches the "first" item |
item[position()=last()] |
matches the last item (in a given list) |
topic[@level='basic'] |
matches topic elements with level attributes whose value is basic |
//chapter[2]/title |
The title of the second chapter |
preceding::partNumber |
all preceding partNumber elements |
<?xml version="1.0" encoding="UTF-8" ?> <!-- Document : Create Copy of Document Created on : April 3, 2006 Author : sgmlxmldotnet Comment Demo file for Creating an Exact Copy of the document. --> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- template rule matching source root element --> <xsl:template match="/|*|comment()"> <xsl:call-template name="do_copy"/> </xsl:template> <!-- Add additional template matches here --> <!-- Named Templates --> <!-- Copy current element and attribute nodes --> <xsl:template name="do_copy"> <xsl:copy> <xsl:for-each select="@*"> <xsl:copy/> </xsl:for-each> <xsl:apply-templates/> </xsl:copy> </xsl:template> </xsl:stylesheet> |
Altova charges big bucks for all of this functionality. The equivalent functionality in <oXygen> for 1/4 the price, cross platform to boot? Summary good deal!
NOTE: If you are running <oXygen> under Linux you do need a Sun JVM. The JVM that comes with the Fedora Core 4 & 5 distributions is not supported. To make it work in Fedora Core, rename the supplied /usr/bin/java binary to java.old and then include a link called java to the SUN JVM.
When your XSLT stylesheet seems to be doing strange things remember to
check the root element for a namespace. I banged my head against the
wall tonight for a good hour trying to figure out why I was getting
such strange results from simple template matches. If I did a match on
a wildcard like * it worked fine but if I did a specific match or xpath
expression nothing was coming back!
Example:
<myroot element xmlns="http://bogus.com/xmlbeans">
<some_elements/>
</myroot>
Once you remember to check the root for a namespace life is fairly
simple just declare the namespace:
Stylesheet:
<xsl:stylesheet
xmlns:bogus="http://bogus.com/xmlbeans"
xmlns="http://bogus.com/xmlbeans">
</xsl:stylsheet>
Note that I declare the namespace twice. Once so I can address
it in the input file as "bogus:<some_element>" and then as the
default namespace for the resulting file so I don't have namespaces
declared all over the place.
Javascript for changeing the color of span elements with a particular class attribute.
<script type="text/javascript">
function changecolor() {
for (i=0;i<document.getElementsByTagName("span").length; i++) {
if (document.getElementsByTagName("span").item(i).className == "myclass"){
document.getElementsByTagName("span").item(i).style.color = "green";
}
}
}
</script>
.......
<a href="#" onclick="changecolor();">Change Color</a>
.......
<span class="myclass">Hi There</span>
"convert" apparently expects to find an ATI processing instruction in
xsl stylesheets before it will execute HTML transformations. Otherwise
it only produces XML.
The PI should take the following form and be inserted as follows:
<?xml version="1.0"?><?APT StylesheetID Title="FOOBAR" CompositionTypes="htmlfile"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
Example:
<xsl:key name="descriptionlookup" match="Descriptions/Description" use="@name"/> <xsl:variable name="desc" select="document('desc.xml')"/>Later....
<xsl:template match="InsertDescription"> <xsl:variable name="key_value" select="@lookup"/> <xsl:for-each select="$desc"> <xsl:copy-of select="key('descriptionlookup',$key_value)/Para"/> </xsl:for-each> </xsl:template>
I was working on a stylesheet today that begs for a regular expression.
The Problem:
<file path='connector\fig\82675-48040.svgz'/>
I want to use parts of the path attribute but since the length varies I
can't use the string() function. It appears there is no good way to do
it in XSLT 1.0
The XSLT 2.0 spec is a working draft at this point. It appears that in
2.0 something like this will be available:
<xsl:value-of select='matches([REG EXP])'/>
CA

This module provides templates for formatting and parsing date/time strings:
<![CDATA[ <?xml version="1.0" encoding="UTF-8"?> <mydoc> <title>Introduction</title> <title>Introduction2</title> <title>Bogus</title> </mydoc> ]]> |
<![CDATA[ <?xml version="1.0" encoding="UTF-8"?> <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:book="books.uri" exclude-result-prefixes="book"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <xsl:apply-templates/> </xsl:template> <xsl:template match="mydoc"> <xsl:apply-templates/> </xsl:template> <xsl:template match="title"> <xsl:value-of select="document('')/*/book:category[@desc=current()]/@code"/> </xsl:template> <book:category code="1" desc="Introduction"/> <book:category code="1" desc="Introduction2"/> <book:category code="2" desc="Bogus"/> </xsl:transform> ]]> |
Powered by WordPress