Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 17th, 2013, 04:12 PM
Registered User
 
Join Date: Apr 2013
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default XSLT 2.0 function to insert attributes and values

Greetings,

I am new to XSLT 2.0 development and am trying to understand how I can write functions that modify the attributes of a text node. I would like to write a function that accepts four string values. I would like the function to then insert four pre-defined attributes, the values of which correspond with the string values the function received when it was called.

I have managed to write and invoke a function that does something similar to this as part of an xsl:copy instruction. The problem is that this results in a duplicate copy of the element and its text, when all I want to do is modify the node’s attribute set.

I attempted to call the function within an xsl:value-of select=”my-function-call”, however this construct only returns the text of the current node and does not make any changes to the node’s attributes.

Is it possible to write a function that performs this operation in XSLT 2.0?

Most of my work has been in other languages. I would typically approach this problem by calling a method that adds the desired attributes and attribute values to a given XML node. I am obviously confused by the parameter passing and return mechanism in XSLT 2.0.

Thanks in advance,

Martin
 
Old April 17th, 2013, 05:47 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It looks as if you haven't understood the XSLT processing model. XSLT transforms an input document to create an output document. It's a very clean functional language (the stylesheet is a function whose input is the input document and whose result is the output document). As such there is no mutation of state.

If you are having difficulty seeing how to achieve a transformation in this style, then if you describe the requirements I'm sure we can help you.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old April 17th, 2013, 06:18 PM
Registered User
 
Join Date: Apr 2013
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default update

Hi Michael,

Thank you for your reply.

My objective is to add attributes and attribute values to an XML source document that will specify pre-defined typographic styles to apply when the modified source is submitted to a high-end page composition system.

I can add the necessary attributes and attribute values directly in the template as follows:
Code:
   <xsl:template match="act-name">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:attribute name="BaseFormat" select="string('6201')"/>
            <xsl:attribute name="Style" select="string('40')"/>
            <xsl:attribute name="Typeface" select="string('32')"/>
            <xsl:attribute name="Variant" select="string('1')"/>
        <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
However, for the sake of readability, and ease of maintenance, I would rather perform the same operation through a function call. Also, rather than specify the names of the attributes each time, I would rather have the function supply the attribute names (they are always the same) and simply pass in the values (which change).

So my function call would look something like:
Code:
<xsl:value-of select="myfunction:add-attributes('6201','40','32','1'))"/>
Except I haven't been able to get this to work.

The reason for this madness, by way of background, is to move the intelligence out of the composition system up into the web server that is receiving and submitting jobs to the system. We will use the Saxon .Net component to perform the transform. Each typographic style is encoded in XML and maintained in a database under revision control. When a job is submitted, the source will be transformed, the appropriate styles fetched from the database, and the entire package submitted for composition.

I hope this helps clarify things.

(I am reading your XSLT 2.0 and XPath 2.0 4th ed. book and find it most helpful.)

Thanks,

Martin
 
Old April 17th, 2013, 06:39 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Named templates and functions are pretty well identical in what they can do, the only difference is in how they are called. You could write your function as

Code:
<xsl:function match="f:act-name" as="element()">
<xsl:param name="e" as="element()"/>
<xsl:param name="b" as="xs:string"/>
<xsl:param name="s" as="xs:string"/>
<xsl:param name="t" as="xs:string"/>
<xsl:param name="v" as="xs:string"/>
<xsl:for-each select="$e">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:attribute name="BaseFormat" select="$e"/>
            <xsl:attribute name="Style" select="$s"/>
            <xsl:attribute name="Typeface" select="$t"/>
            <xsl:attribute name="Variant" select="$v"/>
        <xsl:apply-templates/>
        </xsl:copy>
</xsl:for-each>
</xsl:function>
You have to pass the element as a parameter because functions (unlike templates) aren't passed the context item as an implicit argument.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
NVAudiophile (April 17th, 2013)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Modify Attributes using XSLT Kanini XSLT 10 October 21st, 2010 08:18 AM
XSLT:How to loop through nodes and retrieve attributes values based on some condition smandeh XSLT 2 March 24th, 2009 01:50 PM
xslt grouping with attributes Jimi XSLT 4 October 31st, 2007 10:56 AM
Grouping based on attributes values Chamkaur XSLT 4 June 21st, 2006 05:51 AM
Help:Recursive Function - attributes into elements spencer.clark XSLT 1 June 10th, 2005 07:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.