Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 7th, 2003, 08:49 AM
Registered User
 
Join Date: Jul 2003
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to sven.ansem@sogeti.nl
Default preceding and empty tag

Hi

I have the following XML & XSLT:

XML:
<?xml version="1.0" encoding="utf-8"?>

  <Source Source="IBM">
    <Detail>
      <UserID></UserID>
      <Time>20030610135531</Time>
      <ActionCode>IBM421</ActionCode>
      <ActionText>Start sessie</ActionText>
      <ChargingID>0750490000011325</ChargingID>
    </Detail>

    <Detail>
      <UserID>31622000658</UserID>
      <Time>20030610135532</Time>
      <ActionCode>IBM420</ActionCode>
      <ActionText>Succesvolle account balance wijziging</ActionText>
      <ChargingID>0750490000011325</ChargingID>
    </Detail>

    <Detail>
      <UserID>time-trigger</UserID>
      <Time>20030613235901</Time>
      <ActionCode>IBM440</ActionCode>
      <ActionText>rapport 1</ActionText>
      <ChargingID></ChargingID>
    </Detail>

    <Detail>
      <UserID>time-trigger</UserID>
      <Time>20030613235902</Time>
      <ActionCode>IBM443</ActionCode>
      <ActionText>Rapport 3</ActionText>
      <ChargingID></ChargingID>
    </Detail>

  </Source>


XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" version="1.0" encoding="ISO-8859-1"
indent="yes"/>

        xsl:param name="action_codes" select="',420,421,422,430,431,432,'"/>
        <xsl:template match="/">

                <xsomething>
                        <data_details>
                                <xsl:for-each
select="Source/Detail[(contains($action_codes,concat(',',substring(Actio nCode,4,3),',')))]">
                                        <xsl:sort select="Time" order="ascending"/>

                                        <data_detail>
                                                <xsl:if test="string-length(UserID) != 0">
                                                        <user_id><xsl:value-of select="UserID"/></user_id>
                                                </xsl:if>

                                                <datum_record>
                                                        <xsl:value-of select="substring(Time,1, 4)"/>-<xsl:value-of
select="substring(Time, 5, 2)"/>-<xsl:value-of select="substring(Time,
7, 2)"/>T<xsl:value-of select="substring(Time, 9, 2)"/>:<xsl:value-of
select="substring(Time, 11, 2)"/>:<xsl:value-of
select="substring(Time, 13, 2)"/>
                                                </datum_record>

                                                <action_code><xsl:value-of select="ActionCode"/></action_code>

                                                <action_tekst><xsl:value-of select="ActionText"/></action_tekst>

                                                <xsl:if test="string-length(ChargingID) != 0">
                                                        <charging_id><xsl:value-of select="ChargingID"/></charging_id>
                                                </xsl:if>

                                                <xsl:if test="string-length(ChargingID) = 0">
                                                        <charging_id><xsl:value-of
select="normalize-space(preceding::ChargingID[1])"/></charging_id>
                                                </xsl:if>
                                        </data_detail>

                                </xsl:for-each>
                        </data_details>
                </xsomething>

        </xsl:template>
</xsl:stylesheet>

The goal of my xslt is, that when the tag CHARGING_ID is missing that
the previously known charging_id will be used. Sometimes it will be
the previous another time it will be 10 'records' back.

Unfortunately the XSLT above is not working correctly, because when
the value of Charging_id is not present the tag will be supplied (I
cannot change it).

Is there another way to achieve my goal, by ie. changing this piece of
code 'preceding::ChargingID[1]'?
I have tried recursive templates but I didnt figured it out ?

Anyone an idea ?

greets
Sven


Reply With Quote
  #2 (permalink)  
Old July 8th, 2003, 10:55 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Nor Hachin, Kotayk, Armenia.
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Hi,
It seems that sorting may be the reason of confusing: the XPath expression "preceding::ChargingID[1]" must work properly.

To not to output the tags "charging_id" when respective element's content is empty, try to replace the code
Code:
                                                <xsl:if test="string-length(ChargingID) != 0">
                                                        <charging_id><xsl:value-of select="ChargingID"/></charging_id>
                                                </xsl:if>                

                                                <xsl:if test="string-length(ChargingID) = 0">
                                                        <charging_id><xsl:value-of
select="normalize-space(preceding::ChargingID[1])"/></charging_id>
                                                </xsl:if>
with

Code:
                                                                    <xsl:choose>
                                                                        <xsl:when test="ChargingID != ''"><charging_id><xsl:value-of select="ChargingID"/></charging_id></xsl:when>
                                                                        <xsl:when test="ChargingID = '' and not(preceding::ChargingID[. != ''])"/>
                                                                        <xsl:otherwise><charging_id><xsl:value-of
select="normalize-space(preceding::ChargingID[. != ''][1])"/></charging_id></xsl:otherwise>
                                                                    </xsl:choose>
However, there is an issue here: if you want to use "previous", selected from the whole XML source, then this will work, otherwise, if you want to use such "previous" charging IDs which are in the set you filter using action codes, some XPath expressions must be changed...

Regards,
Armen

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inner empty tag jdzsolt XSLT 2 August 8th, 2008 03:49 PM
Help with Preceding Franky83 XSLT 1 November 12th, 2007 06:20 PM
HTML tag from C# or ASP.NET tag from javascript angshujit ASP.NET 2.0 Basics 3 February 16th, 2007 10:07 AM
How to create end tag on empty elements rjonk XSLT 1 July 21st, 2006 01:18 PM
problem with empty block tag in xsl-fo and fop enT XSLT 0 October 14th, 2003 08:09 AM



All times are GMT -4. The time now is 05:33 AM.


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