473,414 Members | 1,631 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

XSLT preceding problem

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
Jul 20 '05 #1
2 5446
Hi Sven,

I think you are almost there...

If you change...
<xsl:value-of select="normalize-space(preceding::ChargingID[1])"/>
to...
<xsl:value-of select="preceding::ChargingID[normalize-space()][1]"/>

Although rather than two opposing <xsl:if>'s you might be better with a
<xsl:choose>, e.g.

<charging_id>
<xsl:choose>
<xsl:when test="normalize-space(ChargingID)">
<xsl:value-of select="ChargingID"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of
select="preceding::ChargingID[normalize-space()][1]"/>
</xsl:otherwise>
</xsl:choose>
</charging_id>

Or you could roll it into one XPath expression, something like...

<charging_id>
<xsl:value-of select="(ChargingID[normalize-space()] |
preceding-sibling::Detail[normalize-space(ChargingID)][1]/ChargingID)[last()
]"/>
</charging_id>

BTW, if you replace this bit of code...

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

with...

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

you won't have to run all the lines of XSLT code together in order to avoid
CR/LFs from appearing in the <datum_record> output element value.

Hope this helps
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator
"Sven" <sv**@it4us.nl> wrote in message
news:6a**************************@posting.google.c om...
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 nCod
e,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

Jul 20 '05 #2
Hi Marrow,

Thank you very much. It works excellent !! Also thanx for the addiditional remarks.

greets,
Sven

"Marrow" <marrow-NO-@-SPAM-marrowsoft.com> wrote in message news:<GX***************@newsfep3-gui.server.ntli.net>...
Hi Sven,

I think you are almost there...

Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jorn W Janneck | last post by:
hello everyone. i have the sort of question that makes me feel like i am missing the forest for the trees, so apologies if i am missing the blatantly obvious here. i am using saxon, and mostly...
1
by: Juho Jussila | last post by:
Hi How can I easily ensure that Xml document has elements in certain order? I think it can be done with Xml schema, but I'd like to use Xslt, because the validation is a part of Xslt...
1
by: Patrick.O.Ige | last post by:
I have a xml file and i want to format it using XSL My XSL file and XML below I needed to do a distinct which is ok on the first node "Code" For the "programDescription" i did below which gets the...
1
by: arnold | last post by:
Hi, I've been knocking my head against the wall trying to create an XSL transform to perform "normalizations" of a set of XML files that have a common structure. % XML file before transform
0
by: DAnne | last post by:
I'm trying to do a simple count function in xslt and it's turning out to be extremely painful. I have a report that brings back a set of responses for each question per section in an Audit. I...
8
by: Hercules Dev. | last post by:
Hi all, I'm new in xslt and xpath, so my question might be simple but i'm learning. I have an XML document and need to transform it into another XML, I use xslt and it works, but there is a...
2
by: Pandikkannan | last post by:
I am new to XSLT. The requirement i have is to convert an XML to another XML format. My source XML looks something like: <QryLMLCheck>...
3
by: super.raddish | last post by:
Greetings, I am relatively new to, what I would call, advanced XSLT/XPath and I am after some advice from those in the know. I am attempting to figure out a mechanism within XSLT to compare the...
2
jkmyoung
by: jkmyoung | last post by:
Here's a short list of useful xslt general tricks that aren't taught at w3schools. Attribute Value Template Official W3C explanation and example This is when you want to put dynamic values...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.