473,625 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

File formating with XSLT

Hello,
>From the source file, I need to create with XSLT a result file with
the following constraints:

source:
<Source>
<Item>AAA</Item>
<Item>BBBBBBBBB BB</Item>
<Item>CCCCCCCCC </Item>
<Item>DDDDDDDDD DDDDDD</Item>
<Item>EEEEE</Item>
<Item>FF</Item>
<Item>G</Item>
</Source>

result: (text file with line length = 16, with spaces)
AAA BBBBBBBBBBB
CCCCCCCCC
DDDDDDDDDDDDDDD
EEEEE FF G

Each line of the output file is composed of Item texts separated by
one space.
If by adding a new item text on the line we have more than 16
characters then pad the current line with spaces and copy the item
text to a new line.

I do not know if it is possible to do that in XSLT.
Thanks for your help.

Olivier

Jan 31 '07 #1
10 2309
On 31 Jan, 14:02, "olivier.scalb. ..@algosyn.com"
<olivier.scalb. ..@algosyn.comw rote:
I do not know if it is possible to do that in XSLT.
Dead easy, just add <xsl:output method="text" /to the top of your
stylesheet.
http://www.w3.org/TR/xslt#section-Text-Output-Method

Jan 31 '07 #2
Use the FXSL stylesheet file:

strSplit-to-Lines.xsl

And modify it appropriately for your case.

This transformation (just modified the above stylesheet, calling the
template with lineLength=16):

<xsl:styleshe et version="2.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
xmlns:f="http://fxsl.sf.net/"
xmlns:str-split2lines-func="f:str-split2lines-func"
exclude-result-prefixes="f str-split2lines-func"
>
<xsl:import href="str-foldl.xsl"/>

<!-- to be applied on text.xml -->

<str-split2lines-func:str-split2lines-func/>

<xsl:output indent="yes" omit-xml-declaration="ye s"/>

<xsl:template match="/">
<xsl:call-template name="str-split-to-lines">
<xsl:with-param name="pStr"
select="concat( normalize-space(/), ' ')"/>
<xsl:with-param name="pLineLeng th" select="16"/>
<xsl:with-param name="pDelimite rs" select="' '"/>
</xsl:call-template>
</xsl:template>

<xsl:template name="str-split-to-lines">
<xsl:param name="pStr"/>
<xsl:param name="pLineLeng th" select="60"/>
<xsl:param name="pDelimite rs" select="' '"/>

<xsl:variable name="vsplit2li nesFun"
select="documen t('')/*/str-split2lines-func:*[1]"/>

<xsl:variable name="vrtfParam s">
<delimiters><xs l:value-of select="$pDelim iters"/></delimiters>
<lineLength><xs l:copy-of select="$pLineL ength"/></lineLength>
</xsl:variable>

<xsl:variable name="vResult">
<xsl:call-template name="str-foldl">
<xsl:with-param name="pFunc" select="$vsplit 2linesFun"/>
<xsl:with-param name="pStr" select="$pStr"/>
<xsl:with-param name="pA0" select="$vrtfPa rams"/>
</xsl:call-template>
</xsl:variable>

<xsl:for-each select="$vResul t/line">
<xsl:for-each select="word">
<xsl:value-of select="concat( ., ' ')"/>
</xsl:for-each>
<xsl:value-of select="' '"/>
</xsl:for-each>
</xsl:template>

<xsl:template match="str-split2lines-func:*" mode="f:FXSL">
<xsl:param name="arg1" select="/.."/>
<xsl:param name="arg2"/>

<xsl:copy-of select="$arg1/*[position() &lt; 3]"/>
<xsl:copy-of select="$arg1/line[position() != last()]"/>

<xsl:choose>
<xsl:when test="contains( $arg1/*[1], $arg2)">
<xsl:if test="string($a rg1/word)">
<xsl:call-template name="fillLine" >
<xsl:with-param name="pLine" select="$arg1/line[last()]"/>
<xsl:with-param name="pWord" select="$arg1/word"/>
<xsl:with-param name="pLineLeng th" select="$arg1/*[2]"/>
</xsl:call-template>
</xsl:if>
</xsl:when>
<xsl:otherwis e>
<xsl:copy-of select="$arg1/line[last()]"/>
<word><xsl:valu e-of select="concat( $arg1/word, $arg2)"/></word>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<!-- Test if the new word fits into the last line -->
<xsl:template name="fillLine" >
<xsl:param name="pLine" select="/.."/>
<xsl:param name="pWord" select="/.."/>
<xsl:param name="pLineLeng th" />

<xsl:variable name="vnWordsIn Line" select="count($ pLine/word)"/>
<xsl:variable name="vLineLeng th" select="string-length($pLine) +
$vnWordsInLine"/>
<xsl:choose>
<xsl:when test="not($vLin eLength + string-length($pWord) >
$pLineLength)">
<line>
<xsl:copy-of select="$pLine/*"/>
<xsl:copy-of select="$pWord"/>
</line>
</xsl:when>
<xsl:otherwis e>
<xsl:copy-of select="$pLine"/>
<line>
<xsl:copy-of select="$pWord"/>
</line>
<word/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>

Produces the wanted results (the lines are truncated but padding each line
to 16 chars width is left as an exercise to the reader :o) )

AAA BBBBBBBBBBB
CCCCCCCCC
DDDDDDDDDDDDDDD
EEEEE FF G

Cheers,
Dimitre Novatchev.
<ol************ **@algosyn.comw rote in message
news:11******** **************@ v33g2000cwv.goo glegroups.com.. .
Hello,
>>From the source file, I need to create with XSLT a result file with
the following constraints:

source:
<Source>
<Item>AAA</Item>
<Item>BBBBBBBBB BB</Item>
<Item>CCCCCCCCC </Item>
<Item>DDDDDDDDD DDDDDD</Item>
<Item>EEEEE</Item>
<Item>FF</Item>
<Item>G</Item>
</Source>

result: (text file with line length = 16, with spaces)
AAA BBBBBBBBBBB
CCCCCCCCC
DDDDDDDDDDDDDDD
EEEEE FF G

Each line of the output file is composed of Item texts separated by
one space.
If by adding a new item text on the line we have more than 16
characters then pad the current line with spaces and copy the item
text to a new line.

I do not know if it is possible to do that in XSLT.
Thanks for your help.

Olivier

Jan 31 '07 #3
Dead easy, just add <xsl:output method="text" /to the top of your
stylesheet.http://www.w3.org/TR/xslt#section-Text-Output-Method
Thanks, but I do not understand your answer. How to limit the line
length to 16 chars ? How to group small item texts in one line ? How
to separate item text with space ?

Jan 31 '07 #4
On Jan 31, 4:02 pm, "olivier.scalb. ..@algosyn.com"
<olivier.scalb. ..@algosyn.comw rote:
<Source>
<Item>AAA</Item>
<Item>BBBBBBBBB BB</Item>
<Item>CCCCCCCCC </Item>
<Item>DDDDDDDDD DDDDDD</Item>
<Item>EEEEE</Item>
<Item>FF</Item>
<Item>G</Item>
</Source>

result: (text file with line length = 16, with spaces)
AAA BBBBBBBBBBB
CCCCCCCCC
DDDDDDDDDDDDDDD
EEEEE FF G

Each line of the output file is composed of Item texts
separated by one space.
If by adding a new item text on the line we have more
than 16 characters then pad the current line with spaces
and copy the item text to a new line.

I do not know if it is possible to do that in XSLT.
It certainly is possible, but fairly... perverse. XSLT is
not very good at stuff like that.

Just for the heck of it:

<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:param name="max-length" select="16"/>
<xsl:param name="pad-with" select="'.'"/>
<xsl:template match="Source">
<xsl:apply-templates select="Item[1]"/>
</xsl:template>
<xsl:template match="Item">
<xsl:param name="cur-position" select="0"/>
<xsl:variable name="cur-length"
select="string-length(.)"/>
<xsl:variable name="new-position">
<xsl:choose>
<xsl:when test="0 = $cur-position">
<xsl:value-of
select="$cur-position + $cur-length"/>
</xsl:when>
<xsl:otherwis e>
<xsl:value-of
select="$cur-position + $cur-length + 1"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:if
test=
"
($max-length &lt; $cur-length) or
(0 = $cur-length)
">
<xsl:message
terminate="yes" >Invalid entity.</xsl:message>
</xsl:if>
<xsl:choose>
<xsl:when test="$new-position &gt; $max-length">
<xsl:call-template name="pad-space">
<xsl:with-param name="position"
select="$cur-position"/>
</xsl:call-template>
<xsl:text>
</xsl:text>
<xsl:value-of select="."/>
<xsl:apply-templates
select="followi ng-sibling::Item[1]">
<xsl:with-param name="cur-position"
select="$cur-length"/>
</xsl:apply-templates>
<xsl:if test="not(follo wing-sibling::Item[1])">
<xsl:call-template name="pad-space">
<xsl:with-param name="position"
select="$cur-position"/>
</xsl:call-template>
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:when>
<xsl:otherwis e>
<xsl:if test="0 &lt; $cur-position">
<xsl:value-of select="$pad-with"/>
</xsl:if>
<xsl:value-of select="."/>
<xsl:apply-templates
select="followi ng-sibling::Item[1]">
<xsl:with-param name="cur-position"
select="$new-position"/>
</xsl:apply-templates>
<xsl:if test="not(follo wing-sibling::Item[1])">
<xsl:call-template name="pad-space">
<xsl:with-param name="position"
select="$new-position"/>
</xsl:call-template>
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="pad-space">
<xsl:param name="position"/>
<xsl:variable name="left"
select="$max-length - $position"/>
<xsl:if test="0 &lt; $left">
<xsl:value-of select="$pad-with"/>
<xsl:call-template name="pad-space">
<xsl:with-param name="position"
select="$positi on + 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

Not tested on anything but the sample document provided by
the OP. Note that on large documents it is likely to barf
all over the stack and die horrible death. So I would
strongly advise against deploying it in production
environments without some serious tinkering beforehand
(preferrably the tinkering would involve rewriting the
transformation in a language more suitable for the task).

--
Pavel Lepin

Jan 31 '07 #5
Thank you very much for your help.
As the input source file is also generated by an XSLT transformation,
do you think I can hook your function inside this transformation, and
avoid the intermediary file ?

Olivier

Jan 31 '07 #6
Thanks Pavel!

Jan 31 '07 #7

<ol************ **@algosyn.comw rote in message
news:11******** **************@ a34g2000cwb.goo glegroups.com.. .
Thank you very much for your help.
As the input source file is also generated by an XSLT transformation,
do you think I can hook your function inside this transformation, and
avoid the intermediary file ?

Olivier
Yes, this is a standard practice.

In XSLT 1.0 use the xxx:node-set() extension to convert the RTF results of
the first transformation to a regular tree.

In XSLT 2.0 this is not necessary.
Cheers,
Dimitre Novatchev
Feb 1 '07 #8
Note that on large documents it is likely to barf
all over the stack and die horrible death. So I would
strongly advise against deploying it in production

Not necessarily. One can use a DVC (Divide and Conquer) approach, which
requires as little as Log2(N) maximum stack depth.
Cheers,
Dimitre Novatchev

<p.*****@ctncor p.comwrote in message
news:11******** **************@ a34g2000cwb.goo glegroups.com.. .
On Jan 31, 4:02 pm, "olivier.scalb. ..@algosyn.com"
<olivier.scalb. ..@algosyn.comw rote:
><Source>
<Item>AAA</Item>
<Item>BBBBBBBBB BB</Item>
<Item>CCCCCCCCC </Item>
<Item>DDDDDDDDD DDDDDD</Item>
<Item>EEEEE</Item>
<Item>FF</Item>
<Item>G</Item>
</Source>

result: (text file with line length = 16, with spaces)
AAA BBBBBBBBBBB
CCCCCCCCC
DDDDDDDDDDDDDD D
EEEEE FF G

Each line of the output file is composed of Item texts
separated by one space.
If by adding a new item text on the line we have more
than 16 characters then pad the current line with spaces
and copy the item text to a new line.

I do not know if it is possible to do that in XSLT.

It certainly is possible, but fairly... perverse. XSLT is
not very good at stuff like that.

Just for the heck of it:

<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:param name="max-length" select="16"/>
<xsl:param name="pad-with" select="'.'"/>
<xsl:template match="Source">
<xsl:apply-templates select="Item[1]"/>
</xsl:template>
<xsl:template match="Item">
<xsl:param name="cur-position" select="0"/>
<xsl:variable name="cur-length"
select="string-length(.)"/>
<xsl:variable name="new-position">
<xsl:choose>
<xsl:when test="0 = $cur-position">
<xsl:value-of
select="$cur-position + $cur-length"/>
</xsl:when>
<xsl:otherwis e>
<xsl:value-of
select="$cur-position + $cur-length + 1"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:if
test=
"
($max-length &lt; $cur-length) or
(0 = $cur-length)
">
<xsl:message
terminate="yes" >Invalid entity.</xsl:message>
</xsl:if>
<xsl:choose>
<xsl:when test="$new-position &gt; $max-length">
<xsl:call-template name="pad-space">
<xsl:with-param name="position"
select="$cur-position"/>
</xsl:call-template>
<xsl:text>
</xsl:text>
<xsl:value-of select="."/>
<xsl:apply-templates
select="followi ng-sibling::Item[1]">
<xsl:with-param name="cur-position"
select="$cur-length"/>
</xsl:apply-templates>
<xsl:if test="not(follo wing-sibling::Item[1])">
<xsl:call-template name="pad-space">
<xsl:with-param name="position"
select="$cur-position"/>
</xsl:call-template>
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:when>
<xsl:otherwis e>
<xsl:if test="0 &lt; $cur-position">
<xsl:value-of select="$pad-with"/>
</xsl:if>
<xsl:value-of select="."/>
<xsl:apply-templates
select="followi ng-sibling::Item[1]">
<xsl:with-param name="cur-position"
select="$new-position"/>
</xsl:apply-templates>
<xsl:if test="not(follo wing-sibling::Item[1])">
<xsl:call-template name="pad-space">
<xsl:with-param name="position"
select="$new-position"/>
</xsl:call-template>
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="pad-space">
<xsl:param name="position"/>
<xsl:variable name="left"
select="$max-length - $position"/>
<xsl:if test="0 &lt; $left">
<xsl:value-of select="$pad-with"/>
<xsl:call-template name="pad-space">
<xsl:with-param name="position"
select="$positi on + 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

Not tested on anything but the sample document provided by
the OP. Note that on large documents it is likely to barf
all over the stack and die horrible death. So I would
strongly advise against deploying it in production
environments without some serious tinkering beforehand
(preferrably the tinkering would involve rewriting the
transformation in a language more suitable for the task).

--
Pavel Lepin

Feb 1 '07 #9
On Feb 1, 3:50 am, "Dimitre Novatchev"
<dimit...@tpg.c om.auwrote:
Note that on large documents it is likely to barf
all over the stack and die horrible death. So I would
strongly advise against deploying it in production

Not necessarily.
I was talking specifically about the implementation I
provided.
One can use a DVC (Divide and Conquer) approach, which
requires as little as Log2(N) maximum stack depth.
Wouldn't there be certain problems with implementing DVC
for this task? The position of every Item depends on all
previous Items after all. Tail recursion would seem like a
better approach to me, but I not sure there are any XSLT
processors to date that can optimize it away. (Are there,
by the way?)

--
Pavel Lepin

Feb 1 '07 #10

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

Similar topics

4
2436
by: Fredrik Henricsson | last post by:
Hey, I'm building an ontology in Protégé and I want to transform parts of it (e.g. the instances) to HTML with XSL. When I was transforming another file with 'simple' XML-tags like <author> before, I got it working, but the OWL-file is formatted differently and I don't know how to access the elements. I'll post the .owl file below, but this is basically what I want to get from the file (the course name): <Course...
2
3230
by: Marcelo | last post by:
Hi Guys, I have the following question. I have a Xerox DJDE File that I want to convert to PDF.
7
1776
by: Cerebrus99 | last post by:
Hi all, I am confused about how to sort an XML file. I mean how to *actually* sort the data in the physical file, not how to display sorted data. I am using a large XML file as a back-end database, and am making many inserts and updates using the XmlDocument class. But I need to make the XML file human readable too, and want to physically sort the data in the file, every time an insert is made. At present I'm having to use a tool like...
4
1729
by: Philipp Reiss | last post by:
Hello group, I'm new in this topic and I run into problems where google can't help me. I have a XML-file, wich is bound to a XSLT-file, and this works fine in a browser. How do I show the same XML-file with an other XSLT-file? Thanks for your help! Philipp
6
4978
by: Lenny Wintfeld | last post by:
Hi I'm attempting additions/changes to a Java program that (among other things) uses XSLT to transform a large (96 Mb) XML file. It runs fine on small XML files but generates OutOfMemory exceptions with large XML files. I tried a simple punt of -Xmx512MB but that didn't work. In the future, the input XML file may become considerably bigger than 96 MB, so even if it did work, it probably would be putting off the inevitable to some later...
1
1353
by: Max Evans | last post by:
I have a XML file, which contains itemid-elements, e.g.: <itemid>3</itemid> <itemid>12</itemid> Now I want to convert these IDs to the corresponding name via XSLT. I thought I could do it this way (XSLT): <xsl:apply-templates select="itemid" /> <xsl:template match="itemid"> <xsl:value-of select="itemnames" /> </xsl:template> And I'd like to have this itemnames-element also inside the XSLT file
11
2066
by: A.M | last post by:
Hi, I found print much more flexible that write method. Can I use print instead of file.write method? Thank you,
3
10981
by: J055 | last post by:
Hi I'd like to get a populated datatable, create an in memory xml document (file shouldn't be more than a couple of meg), load an XSLT file, do a transform and stream it to a browser. What am I Missing please? MemoryStream strm = new MemoryStream(); XmlWriter writer = XmlWriter.Create(strm);
2
9141
by: saritha2008 | last post by:
Hi, As part of transforming one form of xml to another form, i need to do the below mentioned transformation: My Input XML: <rss> <channel> <item> <assignee username="srinivas.rachakonda">Srinivas Rachakonda</assignee>
0
8253
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8189
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8635
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8354
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8497
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7182
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6116
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.