473,756 Members | 9,662 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XSLT Transform Vector to Array

Hi,

I'm having trouble with an xslt transform. I'm trying to transform a
vector into an array of width N. For example here is my vector:

<data>
<x id="1">1.2</x>
<x id="2">.2</x>
<x id="3">.87</x>
<x id="4">.333</x>
<x id="5">.4442</x>
<x id="6">1.1</x>
<x id="7">.8</x>
<x id="8">.6543</x>
<x id="9">.8761</x>
<x id="10">1</x>
<x id="11">.99</x>
<x id="12">.2</x>
</data>

and given N=4 I want to transform it to

<array>
<row id="1">
<x id="1">1.2</x>
<x id="2">.2</x>
<x id="3">.87</x>
<x id="4">.333</x>
</row>
<row id="2">
<x id="1">.4442</x>
<x id="2">1.1</x>
<x id="3">.8</x>
<x id="4">.6543</x>
</row>
<row id="3">
<x id="1">.8761</x>
<x id="2">1</x>
<x id="3">.99</x>
<x id="4">.2</x>
</row>
</array>

I can successfully use @id mod $N to identify each <row> but I have
not found a way to properly create the output. Here is a failed
attempt:

<xsl:template match="x">
<xsl:choose>
<xsl:when test="(@id mod 4)=1">
<row>
<x><xsl:value-of select="."/></x>
</row>
</xsl:when>
<xsl:otherwis e>
<x><xsl:value-of select="."/></x>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

The "otherwise" <x> elements are not properly nested in the <row> and
I don't know how to make them children of the newly created <row>.

Should I be using an axis here? Or am I missing an obvious solution?

Thx,

Gary H.
Jul 20 '05 #1
6 4872
> The "otherwise" <x> elements are not properly nested in the <row> and
I don't know how to make them children of the newly created <row>.


I see two solutions :

1/ The quick'n dirty one : use text output, and CDATA section

Thus you will be able to <![CDATA[</row><row>]]> :

<?xml version="1.0"?>
<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:output method = "text" indent="yes" />
<xsl:strip-space elements="*" />

<xsl:template match="/">
<![CDATA[<row>]]>
<xsl:apply-templates />
<![CDATA[</row>]]>
</xsl:template>

<xsl:template match="x">
<xsl:choose>
<xsl:when test="(@id mod 4)=0">
<!--<row> -->
<![CDATA[<x>]]><xsl:value-of select="."/><![CDATA[</x>]]>
<![CDATA[</row>
<row>]]>
</xsl:when>
<xsl:otherwis e>
<![CDATA[<x>]]><xsl:value-of select="."/><![CDATA[</x>]]>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>

Improvements should be done to remove the trailing <row></row> if it is
unacceptable.
2/ Another way, based on modes

<?xml version="1.0"?>
<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">

<xsl:variable name="N">4</xsl:variable>

<xsl:template match="/">
<array>
<xsl:apply-templates />
</array>
</xsl:template>

<xsl:template match="x">
<xsl:if test="(@id mod $N) = 1">
<row>
<!-- Use this for ordered contents, quicker
<xsl:apply-templates select="current ()" mode="display" />
<xsl:apply-templates select="followi ng-sibling::*[position() &lt;
$N]" mode="display" />
-->
<!-- Use this for non-ordered contents -->
<xsl:apply-templates select="../x[(@id &gt;=current ()/@id) and (@id
&lt; current()/@id + $N)]" mode="display" />
</row>
</xsl:if>
</xsl:template>

<xsl:template match="x" mode="display">
<x><xsl:apply-templates /></x>
</xsl:template>

</xsl:stylesheet>

Hth

--
Rémi Peyronnet
Jul 20 '05 #2
Hi Gary,

Try something like...

<?xml version="1.0"?>
<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="array_wid th" select="4"/>

<xsl:template match="data">
<array>
<xsl:apply-templates select="x[(position() mod $array_width = 1) or
($array_width = 1)]" mode="row-start"/>
</array>
</xsl:template>

<xsl:template match="x" mode="row-start">
<row id="{position() }">
<xsl:copy-of select=". | following-sibling::x[position() &lt;
$array_width]"/>
</row>
</xsl:template>

</xsl:stylesheet>
HTH
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator

"Gary Huntress" <ga***********@ gmail.com> wrote in message
news:fb******** *************** ***@posting.goo gle.com...
Hi,

I'm having trouble with an xslt transform. I'm trying to transform a
vector into an array of width N. For example here is my vector:

<data>
<x id="1">1.2</x>
<x id="2">.2</x>
<x id="3">.87</x>
<x id="4">.333</x>
<x id="5">.4442</x>
<x id="6">1.1</x>
<x id="7">.8</x>
<x id="8">.6543</x>
<x id="9">.8761</x>
<x id="10">1</x>
<x id="11">.99</x>
<x id="12">.2</x>
</data>

and given N=4 I want to transform it to

<array>
<row id="1">
<x id="1">1.2</x>
<x id="2">.2</x>
<x id="3">.87</x>
<x id="4">.333</x>
</row>
<row id="2">
<x id="1">.4442</x>
<x id="2">1.1</x>
<x id="3">.8</x>
<x id="4">.6543</x>
</row>
<row id="3">
<x id="1">.8761</x>
<x id="2">1</x>
<x id="3">.99</x>
<x id="4">.2</x>
</row>
</array>

I can successfully use @id mod $N to identify each <row> but I have
not found a way to properly create the output. Here is a failed
attempt:

<xsl:template match="x">
<xsl:choose>
<xsl:when test="(@id mod 4)=1">
<row>
<x><xsl:value-of select="."/></x>
</row>
</xsl:when>
<xsl:otherwis e>
<x><xsl:value-of select="."/></x>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

The "otherwise" <x> elements are not properly nested in the <row> and
I don't know how to make them children of the newly created <row>.

Should I be using an axis here? Or am I missing an obvious solution?

Thx,

Gary H.

Jul 20 '05 #3

<xsl:variable name="N" select="4"/>

<xsl:template match="data">
<array>
<xsl:for-each select="x[position() mod $N = 1]">
<row>
<xsl:for-each select=".|follo wing-sibling::x[position() &lt; $N]">
<x id="position()" ><xsl:value-of select="."/></x>
</xsl:for-each>
</row>
</xsl:for-each>
</array>
</xsl:template>
Jul 20 '05 #4
Rémi Peyronnet <re*******@via. ecp.fr> wrote in message news:<cf******* **@smilodon.ecp .fr>...
The "otherwise" <x> elements are not properly nested in the <row> and
I don't know how to make them children of the newly created <row>.


I see two solutions :

1/ The quick'n dirty one : use text output, and CDATA section

Thus you will be able to <![CDATA[</row><row>]]> :


Sorry to slur your coding style, sir, but AAARGH! KILL IT!

Use the mod operator to select every n'th <x> element, then sweep over
"self::x|follow ing-sibling::x[position() &lt; $n]" to produce each
row.
--
Robin Johnson
Lead Developer, enCircle Solutions Ltd.
first initial last name at encircle dot co dot uk
Jul 20 '05 #5
Robin Johnson a écrit :
1/ The quick'n dirty one : use text output, and CDATA section
Sorry to slur your coding style, sir, but AAARGH! KILL IT!


Of course, you're right ; that is why there was a second and recommended
solution in my post :-)
Use the mod operator to select every n'th <x> element, then sweep over
"self::x|follow ing-sibling::x[position() &lt; $n]" to produce each
row.


Does only work if elements are sorted, as stated in my second solution :
"
<!-- Use this for ordered contents, quicker
<xsl:apply-templates select="current ()" mode="display" />
<xsl:apply-templates select="followi ng-sibling::*[position() &lt;
$N]" mode="display" />
-->
<!-- Use this for non-ordered contents -->
<xsl:apply-templates select="../x[(@id &gt;=current ()/@id) and (@id
&lt; current()/@id + $N)]" mode="display" />
"

But I still think the first solution has to be thought for very special
cases much more complicated with the second version, and for codes only
used once. Wasn't the case here, I agree.

--
Rémi Peyronnet
Jul 20 '05 #6
On Tue, 17 Aug 2004 20:37:33 +0200, Rémi Peyronnet
<re*******@via. ecp.fr> wrote:
Robin Johnson a écrit :
Use the mod operator to select every n'th <x> element, then sweep over
"self::x|follow ing-sibling::x[position() &lt; $n]" to produce each
row.
Does only work if elements are sorted, as stated in my second solution :


Noted.
But I still think the first solution has to be thought for very special
cases much more complicated with the second version, and for codes only
used once.


Yes, I've done the same myself in a few cases (notably when
suppressing output escaping in attribute values*), but I'm not proud.

* There's a saxon extension for that now, of course.
--
Robin Johnson
Lead Developer, enCircle Solutions Ltd.
first initial last name at encircle dot co dot uk
Jul 20 '05 #7

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

Similar topics

8
1917
by: Ola Natvig | last post by:
Anybody out there who knows if the 4suite implementation of XSLT are a threadsafe one? -- -------------------------------------- Ola Natvig <ola.natvig@infosense.no> infoSense AS / development
1
2256
by: Brian McGuinness | last post by:
I have a question about using the STL transform algorithm in a function. What I want to do is define a group of array classes to represent APL-style arrays (arrays in which the number of dimensions and the length of any dimension can be changed at any time). What I currently plan is to have an abstract base class at the top, to allow polymorphism, e.g.: #include "basedefs.h" // Basic data types, e.g. typedef long Integer
0
2357
by: Christopher M. Lauer | last post by:
I have done my best to answer this question but can not find the proper set of commands. I would like to transform an xml file (in code behind) and display its output in a specific html tag, such as a div with a runat=server. I can somewhat do this if I create a server control and include the control within the html div tag but this method (borrowed from ASP.NET Website Programming by Wrox press thanks guys) does not give me the full...
4
4823
by: schneider | last post by:
Anyone know if there is a way to dynamicly create a Xslt template/s and use them as an xml transform with-out use files for the Xslt? All the methods I see use files. I want to create a Xslt transform via code? Any examples would be great.
2
2261
by: David Laub | last post by:
A transform that works fine in XMLSPy fails when I run it under dot net. I've run many other transforms successfully in dot net so there is something "special" about this transform I just installed dot net framework 1.1 SP1 - but I still get the same error What makes this transform more complex than others is: 1) It uses an MSXSL extension function to evaluate Xpaths at run time 2) It uses an include to include the extension function 3)...
7
2868
by: Harolds | last post by:
The code below worked in VS 2003 & dotnet framework 1.1 but now in VS 2005 the pmID is evaluated to "" instead of what the value is set to: .... xmlItems.Document = pmXML // Add the pmID parameter to the XSLT stylesheet XsltArgumentList xsltArgList = new XsltArgumentList(); xsltArgList.AddParam("pmID", "", pmID); xmlItems.TransformArgumentList = xsltArgList;
3
2010
by: thomas.porschberg | last post by:
Hi, I want to read records from a database and export it in an arbitrary format. My idea was to feed a class with a String array fetched from the database and let this class fire SAX events as processor input. The basic class hierarchy is:
1
2743
by: Nick | last post by:
I am working on a website for a client and one of their requirements was to have a mailing list. I decided to XSLT to transform "templates" to HTML so that editing was very easy and less time consuming. I keep getting the following error when I submit my email to their site. System.Xml.Xsl.XslTransformException: Cannot find the script or external object that implements prefix 'ext:MailingList'. at...
0
9482
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...
1
9878
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
9728
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
8733
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
7282
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
6551
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
5167
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...
0
5322
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3827
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.