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:

<target name="Run-XSLT-Filter" >	
	   <xslt style="myfile.xsl" in="example.xml"  out="example-reloaded.xml"> 
	   <classpath>
                <!--add your favorite XSLT Processor here-->
	   	<pathelement location="classes/saxon.jar"></pathelement>
	   </classpath>
	   </xslt>
</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.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;

&lt;!--
    Document   : Create Copy of Document
    Created on : April 3, 2006
    Author     : sgmlxmldotnet
    Comment
       Demo file for Creating an Exact Copy of the document.
--&gt;

&lt;xsl:stylesheet version=&quot;1.0&quot;
    xmlns:xsl=&quot;<a HREF="http://www.w3.org/1999/XSL/Transform">http://www.w3.org/1999/XSL/Transform</a>&quot;&gt;
    &lt;!-- template rule matching source root element --&gt;
    &lt;xsl:template match=&quot;/|*|comment()&quot;&gt;
       &lt;xsl:call-template name=&quot;do_copy&quot;/&gt;
    &lt;/xsl:template&gt;

&lt;!-- Add additional template matches here --&gt;

&lt;!-- Named Templates --&gt;
	&lt;!-- Copy current element and attribute nodes --&gt;
	&lt;xsl:template name=&quot;do_copy&quot;&gt;
	    &lt;xsl:copy&gt;
        	&lt;xsl:for-each select=&quot;@*&quot;&gt;
				&lt;xsl:copy/&gt;
			&lt;/xsl:for-each&gt;
			&lt;xsl:apply-templates/&gt;
        &lt;/xsl:copy&gt;
    &lt;/xsl:template&gt;

&lt;/xsl:stylesheet&gt;


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:

&lt;myroot element xmlns=<a class="moz-txt-link-rfc2396E" href="http://bogus.com/xmlbeans">"http://bogus.com/xmlbeans"</a>&gt;
&lt;some_elements/&gt;
&lt;/myroot&gt; 

Once you remember to check the root for a namespace life is fairly
simple just declare the namespace:

Stylesheet:

&lt;xsl:stylesheet
                        xmlns:bogus="http://bogus.com/xmlbeans"
                        xmlns="http://bogus.com/xmlbeans"&gt;
&lt;/xsl:stylsheet&gt;

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.

<tt>&lt;<span class="start-tag">script</span><span class="attribute-name"> type</span>=<span class="attribute-value">"text/javascript"</span>&gt;</tt><br />
  <tt>function changecolor() {</tt><br />
  <tt>    for (i=0;i&lt;document.getElementsByTagName("span").length;
i++) {</tt><br />
  <tt>        if
(document.getElementsByTagName("span").item(i).className == "myclass"){</tt><br />
  <tt>           
document.getElementsByTagName("span").item(i).style.color = "green";</tt><br />
  <tt>        }</tt><br />
  <tt>    }</tt><br />
  <tt>}</tt><br />
  <tt>&lt;/<span class="end-tag">script</span>&gt;</tt><br />
  <br />
  <tt>.......</tt><br />
  <br />
  <tt>&lt;<span class="start-tag">a</span><span class="attribute-name">
href</span>=<span class="attribute-value">"#" </span><span class="attribute-name">onclick</span>=<span class="attribute-value">"changecolor();"</span>&gt;Change
Color&lt;/<span class="end-tag">a</span>&gt;</tt><br />
  <br />
  <tt>.......</tt><br />
  <br />
  <tt>&lt;span class="myclass"&gt;Hi There&lt;/span&gt;</tt><br />

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:

&lt;?xml version="1.0"?&gt;
&lt;?APT StylesheetID Title="FOOBAR" CompositionTypes="htmlfile"?&gt;
&lt;xsl:stylesheet version="1.0" xmlns:xsl=<a class="moz-txt-link-rfc2396E" href="http://www.w3.org/1999/XSL/Transform">"http://www.w3.org/1999/XSL/Transform"</a>&gt;

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:

&lt;xsl:key name=&quot;descriptionlookup&quot; 
match=&quot;Descriptions/Description&quot; use=&quot;@name&quot;/&gt;
&lt;xsl:variable name=&quot;desc&quot; select=&quot;document('desc.xml')&quot;/&gt;

Later….

&lt;xsl:template match=&quot;InsertDescription&quot;&gt;
	&lt;xsl:variable name=&quot;key_value&quot; select=&quot;@lookup&quot;/&gt;
	&lt;xsl:for-each select=&quot;$desc&quot;&gt;
	&lt;xsl:copy-of 
        select=&quot;key('descriptionlookup',$key_value)/Para&quot;/&gt;
	&lt;/xsl:for-each&gt;
&lt;/xsl:template&gt;


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:

    &lt;file path='connector\fig\82675-48040.svgz'/&gt;

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:

    &lt;xsl:value-of select='matches([REG EXP])'/&gt;



CA

Older Posts »

Powered by WordPress