SGMLXML.net A place for SGML and XML application developers.

July 23, 2014

Using Apache ANT to run XSLT

Filed under: ANT,Eclipse,XML,XSL,XSLT — cangione @ 5:38 pm

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:

	
	    
	   
                
	   	
	   
	   

May 3, 2006

XSLT vs XQuery

Filed under: XSLT — @ 5:44 pm







Blog item that examines XQuery vs XSLT.



I would add to the insights here that XSLT is designed for transformations while XQuery was designed to find things that are not necessarily in the current instance you are working with but also not necessarily in a CMS or database.



An additional article from Michael Kay on the same subject.






April 6, 2006

Quick tour of XPath

Filed under: XSLT — cangione @ 3:43 pm







Getting lots of questions about XPath on the #XML IRC channel so figured I'd write a quick cheat sheet. Sometimes the hardest part of learning something is knowing what it's called.

There are 7 Types of Nodes:

  1. Root Node
    /
  2. Element Node
  3. Text Node text
    ()
  4. Attribute Node
    @
  5. Comment Node
    comment()
  6. Processing Instruction
    processing-instruction()
  7. Namespace Node

There are 13 AXIS Specifiers from your current node:

  1. ancestor::
  2. ancestor-or-self::
  3. attribute::(or @)
  4. child::(or nothing)
  5. descendant::
  6. descendant-or-self::
  7. following::
  8. following-sibling::
  9. namespace::
  10. parent::or (or ..)
  11. preceding::
  12. preceding-sibling::
  13. self:: or (.)

Note that the double colon "::" immediately follows the use of an axis name.

Predicates:

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]

Functions:

  • 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.

Examples:

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


April 3, 2006

Create an Exact Copy of a Document using XSLT

Filed under: XSLT — cangione @ 1:59 pm







I get this request all the time so here is an example. It also shows you how to handle cases where you want to do something specific to certain elements while doing the copy.

<?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>


March 23, 2006

oXygen Development Enviornment for XML a multi platform alternative to Altova XML Spy

Filed under: Eclipse,Software,XML,XSL,XSLT — cangione @ 9:44 pm







Downloaded the latest version of the <oXygen> XML IDE last night and was impressed with what I saw. <oXygen> is cross platform and supports both Linux and Windows as well as the Eclipse IDE. One things that I have always like about the Altova XML Spy suite (that they remove from their community edition) is the ability to collapse an element in the edit interface. This comes in really handy when working with complex documents. <oXygen> also has a nice Schema interface, XPATH 2.0 interface and diffing tool.

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.

December 21, 2005

Strange things when you forget the namespace!

Filed under: XSLT — cangione @ 9:49 pm

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.

December 20, 2005

Toggle colors on html span element using Javascript

Filed under: CSS,DOM,XSLT — cangione @ 11:31 pm

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>

December 2, 2005

Using XSLT Stylesheets with Arbortext E3 for HTML Output

Filed under: Software,XSLT — cangione @ 5:30 pm

"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">

July 22, 2005

Looking up keys in a separate xml file

Filed under: XSLT — cangione @ 2:26 pm







Was recently working on an XSLT filter that extracted the text portion of SVG graphics into another file for translation. Once translated I needed a mechanism to merge the text back into the graphic. The document function did nicely.

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>


March 23, 2005

Regular Expressions and XSLT

Filed under: XSLT — cangione @ 12:56 pm

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

March 11, 2005

Date/Time Processing Package for XSLT

Filed under: XSLT — @ 1:56 pm







This module provides templates for formatting and parsing date/time strings:


http://xsltsl.sourceforge.net/date-time.html


 

Example of XSLT document() function to create a lookup table

Filed under: XSLT — cangione @ 1:56 pm




 This technique can be used to add metadata to incoming source documents.< ?xml:namespace prefix = o />

 Source:

<![CDATA[

<?xml version="1.0" encoding="UTF-8"?>

<mydoc>

            <title>Introduction</title>

            <title>Introduction2</title>

            <title>Bogus</title>

</mydoc>

]]>

 Stylesheet:

<![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>

]]>

 

 

Older Posts »

Powered by WordPress