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 August 13th, 2013, 12:43 PM
Authorized User
 
Join Date: Aug 2013
Posts: 30
Thanks: 9
Thanked 0 Times in 0 Posts
Default xsl:attribute

I have a further question:

In my input file I have a series of <lb/> elements

I would like to insert in each of them an attribute @n with a value that is a consecutive number starting from 1.
nevertheless, I would also like to insert 0 as the value when my element node <lb/> is followed by an element node <gap/>

is this possible? is xsl:attribute the right function to use?

Thank you very much

Sincerely

Pietro
 
Old August 13th, 2013, 01:16 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Sounds reasonable. You haven't explained what you want to happen to the consecutive numbers when you insert a zero into the sequence, but I would do it something like

Code:
<xsl:template match="lb">
  <lb>
   <xsl:attribute name="n"><xsl:number/></xsl:attribute>
  </lb>
</xsl:template>

<xsl:template match="lb[following-sibling::*[1][self::gap]]">
  <lb n="0"/>
</xsl:template>
__________________
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:
pietromaria.liuzzo (August 14th, 2013)
 
Old August 14th, 2013, 03:14 AM
Authorized User
 
Join Date: Aug 2013
Posts: 30
Thanks: 9
Thanked 0 Times in 0 Posts
Default

Thank you very much!

what I want to happen to the consecutive numbers is that they remain the same, like

Code:
<lb n="0"/><gap/>
<lb n="1"/>
<lb n="2"/>
<lb n="3"/>
<lb n="0"/><gap/>
I also would like to insert consecutive numbers into a div element which is an ancestor of my <lb/>

Code:
<div n="1">
<lb n="0"/><gap/>
<lb n="1"/>
<lb n="2"/>
<lb n="3"/>
<lb n="0"/><gap/>
</div>

<div n="2">
<lb n="0"/><gap/>
<lb n="1"/>
<lb n="2"/>
<lb n="3"/>
<lb n="0"/><gap/>
</div>
I have then added before the code you suggested me the following.

Code:
   <xsl:template match="div">
        
       <div>
            <xsl:attribute name="n"><xsl:number/></xsl:attribute>
        </div>
       
        <xsl:apply-templates/>
    </xsl:template>
what happens now is that my result looks like this

Code:
<div n="1"/>
<lb n="0"/>
<lb n="2"/>
<lb n="3"/>
<lb n="4"/>
<lb n="0"/>

<div n="2"/>
<lb n="0"/>
<lb n="2"/>
<lb n="3"/>
<lb n="4"/>
<lb n="0"/>
there must be still something wrong with my understanding of the basics...

thank you very much indeed for any further help

Last edited by pietromaria.liuzzo; August 14th, 2013 at 01:07 PM.. Reason: I have realized that my typos made the question ambiguous
 
Old August 14th, 2013, 03:59 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Firstly you need to put the xsl:apply-templates inside the output div:

Code:
<div>
   <xsl:attribute .. />
   <xsl:apply-templates/>
</div>
Then you just need to specify a "count" attribute for the xsl:number instruction, basiclaly the "not" of the template given to you above..

Code:
<xsl:template match="lb">
  <lb>
   <xsl:attribute name="n"><xsl:number count="lb[not(following-sibling::*[1][self::gap])]"/></xsl:attribute>
  </lb>
</xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
pietromaria.liuzzo (August 14th, 2013)
 
Old August 14th, 2013, 04:01 AM
Authorized User
 
Join Date: Aug 2013
Posts: 30
Thanks: 9
Thanked 0 Times in 0 Posts
Default

Thank you very much!
 
Old August 14th, 2013, 12:58 PM
Authorized User
 
Join Date: Aug 2013
Posts: 30
Thanks: 9
Thanked 0 Times in 0 Posts
Default

what do I have to do if I just want to add those attributes but want to get as a result my initial xml? with this code I am actually getting only my list of well numbered lb and divs.

Please, could you tell me which instruction should I look at?

thank you very much
Pietro
 
Old August 14th, 2013, 01:09 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Use
Code:
<xsl:template match="@* | node()">
  <xsl:copy>
     <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>
as the starting point to all such stylesheets, then add the templates to perform the transformations to specific nodes, like the ones already suggested to you in this thread.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old September 3rd, 2013, 02:33 PM
Authorized User
 
Join Date: Aug 2013
Posts: 30
Thanks: 9
Thanked 0 Times in 0 Posts
Default

Thank you!

I have tried on another file doing this

Code:
 <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="ab">
        <div type="edition">
                    <xsl:choose>
                        <xsl:when test="contains(.,'//')">
                        <xsl:for-each select="tokenize(.,'//')">
                        <div>
                    <xsl:attribute name="n" select="position()"/>
                    <xsl:attribute name="type">textpart</xsl:attribute>
                        <ab><lb/>
                            <xsl:variable name="brackets">
                                <xsl:call-template name="breakbrackets">
                                    <xsl:with-param name="textToBeProcessed" select="."/>
                                </xsl:call-template>
                            </xsl:variable>
                            <xsl:for-each select="$brackets">
                                <xsl:call-template name="upconversion"/>
                            </xsl:for-each>
                        </ab>
                    </div>
                    </xsl:for-each>
                    </xsl:when>
                    <xsl:otherwise>
                        <ab>
                        <lb/>
                            <xsl:variable name="brackets">
                            <xsl:call-template name="breakbrackets">
                                <xsl:with-param name="textToBeProcessed" select="."/>
                            </xsl:call-template>
                        </xsl:variable>
                        <xsl:for-each select="$brackets">
                            <xsl:call-template name="upconversion"/>
                        </xsl:for-each>
                    </ab>
                   </xsl:otherwise>
                    </xsl:choose>
        </div>
    </xsl:template>
but for some reason it returns the file to which I apply this as it is with no changes.

the source file looks like this

Code:
<TEI xmlns="http://www.tei-c.org/ns/1.0" xml:space="preserve" xml:lang="en">
    <teiHeader>
    ...    
    </teiHeader>
    <text>
        <body>
                       
            <div type="bibliography">
    </div>
            
            <div type="edition">
                 <head>Text</head>
                <ab>a lot of text to which I want to apply my templates</ab>
            </div>

            <div type="apparatus"><p> </p></div>

            <div type="translation" xml:lang="Spanish">
                <p>
                    <ref type="URI"/>
                </p>
            </div>
        </body>
    </text>
</TEI>
I do not understand what is that I have done wrong this time... thanks for any advise.





Similar Threads
Thread Thread Starter Forum Replies Last Post
xsl:function pass param to attribute of xsl:instruction bonekrusher XSLT 4 March 31st, 2009 09:06 AM
escaping of characters in xsl:attribute pramod Classic ASP XML 1 September 12th, 2008 09:11 AM
<xsl:attribute> vs {} berna_isct XSLT 1 April 6th, 2006 03:30 PM
XSL Attribute ID jonty XSLT 1 March 28th, 2006 03:53 PM
onClick as xsl attribute? mlaba XSLT 1 September 10th, 2004 02:36 AM





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