Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help for an easy XSLT 1

Status
Not open for further replies.

gbordel

Programmer
Dec 6, 2006
4
ES
Hi,
I am looking for examples to implement what I think should be a simple XSLT, but I am not succeding. This is my first attempt to use an XSLT. ¿Could anybody help me?. I would be very grateful. Thanks.


from:

<segment>
<sync time="0" />
sentence one
<sync time="23,98" />
sentence two
<sync time="33,65" />
sentence three
<sync time="45,34" />
</segment>


to:
<sentence offset="0" length="23.98"> sentenec one </sentence>
<sentence offset="23.98" length="9.67"> sentenec one </sentence>
<sentence offset="33.65" length="11,39"> sentenec one </sentence>
 
Just don't understand where do you get 9.67 and 11.39? and why every line contains sentence one and not varying one, two, three?
 
Upon re-read, I see the numbers being the difference between the successive time. Okay, no question there.
 
Thank you tsuji for your comments.

Yes, you are right, I made a cut&paste mistake writing the target code. It should read:

<sentence offset="0" length="23.98"> sentence one </sentence>
<sentence offset="23.98" length="9.67"> sentence two</sentence>
<sentence offset="33.65" length="11,39"> sentence three</sentence>

 
The main difficulty is that [tt]sentence one[/tt] and [tt]sentence two[/tt] and [tt]sentence three[/tt] are all in the text node of the <segment> element. Can you restructure the XML document as follows?
Code:
<segment>
  <sync time="0">sentence one</sync>
  <sync time="23,98">sentence two</sync>
  <sync time="33,65">sentence three</sync>
  <sync time="45,34" />
</segment>
If you can restructure in this manner, then it would be much easier than your example, since each 'sentence' is a text node of the associated <sync> element. In your original example, this is not the case.

Tom Morrison
 
The output asked for is not well formed xml document. This is how you produce one, well-formed, with the root called <root>. I use and change the xml to digit everywhere as decimal separator for convenience of my os local setting to save myself some further manipulation.
[tt]
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="<xsl:eek:utput method="xml" encoding="utf-8" indent="yes" />
<xsl:template match="/">
<root>
<xsl:apply-templates select="node()" />
</root>
</xsl:template>
<xsl:template match="segment">
<xsl:apply-templates select="text()" />
</xsl:template>
<xsl:template match="text()">
<xsl:variable name="voffset_behind">
<xsl:value-of select="preceding-sibling::*[position() = 1]/@time" />
</xsl:variable>
<xsl:variable name="voffset_ahead">
<xsl:value-of select="following-sibling::*[position() = 1]/@time" />
</xsl:variable>
<sentence>
<xsl:attribute name="offset">
<xsl:value-of select="$voffset_behind" />
</xsl:attribute>
<xsl:attribute name="length">
<xsl:value-of select="format-number(number($voffset_ahead) - number($voffset_behind),'#.00')" />
</xsl:attribute>
<xsl:value-of select="normalize-space()" />
</sentence>
</xsl:template>
</xsl:stylesheet>
[/tt]
 
Thanks k5tm.
My second option is to write a specific piece of Java parsing code, but I think a single XSLT is a more neat approach. I will try it for a while.

I am new to XML processing and learning slowly...
I will learn about what I suppose you say: there is only one text node and the sentences-elements sequece is lost.

Than you.
 
Thank you very much tsuji.

I know the target is not well formed. In fact what I posted
is a "canonic" example of the part from a much longer file where I am having problems to find a procedure.

I just read your code and do not perfectly understand it yet, but I find there the answers to the problems a was not finding solutions from examples in the web (access to previous siblings, arithmetic, etc).

I am very grateful to you. I will use your code as soon as possible.

Kind Regards.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top