473,803 Members | 3,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[xslt] problem

i have xml like this:

<data>
<item type="a">
1
</item>
<item type="a">
2
</item>
<item type="b">
3
</item>
<item type="a">
4
</item>
<item type="a">
5
</item>
<item type="b">
6
</item>
<item type="a">
7
</item>
</data>

i want some xsl toget this for $type=a:

<output>
<row>
<data>1</data>
<data>2</data>
</row>
<row>
<data>4</data>
<data>5</data>
</row>
<row>
<data>7</data>
<data></data>
</row>
</output>

and for $type=b this as output:

<output>
<row>
<data>3</data>
<data>6</data>
</row>
</output>
my xsl sofar:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:styleshe et version="1.0">

<xsl:param name="type"/>

<xsl:template match="/data">
<xsl:for-each select="item[xc:type=$type][(position() mod 2)=1]">
<row>
<xsl:apply-templates select="."/>
<xsl:apply-templates select="followi ng-sibling::*[position() &lt; 2]"/>
</row>
</xsl:for-each>
</xsl:template>

<xsl:template match="xc:medew erker">
<data>
<xsl:value-of select="."/>
</data>
</xsl:template>

<xsl:stylesheet >
But this does not work, because some times items of type b
are chosen when $type equals a.

please help
Aug 16 '05 #1
3 1382
> But this does not work, because some times items of type b
are chosen when $type equals a.

please help


Try

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

<xsl:param name="type" select="'a'"/>

<xsl:template match="/data">
<xsl:for-each select="item[@type=$type][position() mod 2 = 1]">
<row>
<data><xsl:appl y-templates select="."/></data>
<data><xsl:appl y-templates
select="followi ng-sibling::*[@type=$type][position() &lt;2]"/>$
</row>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

(what you following-sibling saw was not the successors in the list
selected in for-each, but just the siblings of the current node...)

Soren
Aug 16 '05 #2
Tjerk Wolterink schrieb:
<xsl:template match="/data">
<xsl:for-each select="item[xc:type=$type][(position() mod 2)=1]">
<row>
<xsl:apply-templates select="."/>
<xsl:apply-templates select="followi ng-sibling::*[position()
&lt; 2]"/>
</row>
</xsl:for-each>
</xsl:template>

Soren was quicker!

However here is my solution without foreach loops, empty element at the
list and last but not least only one representation of the item template.
Hope it helps!
<?xml version="1.0" encoding="UTF-8"?>
<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="xml" />

<xsl:param name="type">a</xsl:param>
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>

<xsl:template match="data">
<xsl:element name="output">
<!-- simply select every second element -->
<xsl:apply-templates select="item[@type=$type][not(position() mod
2=0)]" mode="loop"/>
</xsl:element>
</xsl:template>
<!-- item builds a group (remember only every second element is
selected) -->
<xsl:template match="item" mode="loop">
<xsl:element name="group">
<!-- buliding the data element -->
<xsl:apply-templates select="." mode="item" />
<!-- if there is no next item bulild an empty data element -->
<xsl:if test="not(follo wing-sibling::item[@type=$type][1])">
<xsl:element name="data"/>
</xsl:if>
<!-- now we apply a template for exactly the next item -->
<xsl:apply-templates
select="followi ng-sibling::item[@type=$type][1]" mode="item"/>

</xsl:element>
</xsl:template>

<!-- our item template -->
<xsl:template match="item" mode="item">
<xsl:element name="data">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>

</xsl:stylesheet>
Aug 16 '05 #3
Hi Soren,
<xsl:for-each select="item[@type=$type][position() mod 2 = 1]">
<row>
<data><xsl:appl y-templates select="."/></data>
<data><xsl:appl y-templates
select="followi ng-sibling::*[@type=$type][position() &lt;2]"/>$
Oops, how did that final $ sign end up there? It's not supposed to be
there, must be a typo.
Soren

Soren
Aug 17 '05 #4

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

Similar topics

1
1597
by: Ola Natvig | last post by:
Hi all I'm working with a long running, threaded server which serves HTTP requests with content which are passed through a XSLT processor. The XSLT processor I'm using is the Pyana processor. I have one compiled stylesheet which I uses to process all responses. This way I only need to read and compile the stylesheet once. When serving a rather small page 404-page I get the server to process
6
2750
by: Pete | last post by:
I am just getting to grips with XML and I was wondering if you could help me with something that no-one seems able or willing to help with.. I have an XSLT file which should be transforming a straight XML file http://www.discovertravelandtours.com/test/templates/test.xml?Location=Germany To another XML file http://www.discovertravelandtours.com/test/templates/test2.xml?Location=Germany
6
2935
by: Ramon M. Felciano | last post by:
Helo all -- I'm trying to gain a deeper understand for what type of semi-declarative programming can be done through XML and XPath/XSLT. I'm looking at graph processing problems as a testbed for this, and came across a problem that I haven't been able to solve elegantly. The problem is to find "linker" vertexes that a pair of verteces from a pre-defined set. For example, if the graph verteces represent cities and edges represent flights...
0
1466
by: Mike | last post by:
I'm generating an XSLT document programatically in VB.Net. I'm then trying to apply that XSLT against a cXML document to generate my own internally developed XML document. I'm using RichTextBox controls to dump out the XSLT, cXML and internal XML. The following code works fine in Visual Studio 2002. However, after upgrading to VS 2003, this same code no longer works
4
2174
by: Moogy | last post by:
I'm pulling my hair out here. First, I'm new to XML, so that doesn't help, but none of this makes any sense to me. All I'm trying to do is take a simple source XML file and translate it with an XSLT to produce HTML code. The problem I have is that no matter what translation it runs through, it ALWAYS includes data that I don't match in the XSLT!! All I want to do is extract specific fields from the XML. Here's the XML source....
2
5288
by: chris | last post by:
Hi there, I create an XML file from a dataset like this: System.IO.StreamWriter xmlSW = new System.IO.StreamWriter(FILENAME); dsUserData1.WriteXml(xmlSW, XmlWriteMode.WriteSchema); xmlSW.Close(); Which gives me this XML file: <NewDataSet>
7
2872
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
2012
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:
3
1689
by: RC | last post by:
Let's say: if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { // Now I got an XML object here var xmlDocument = XMLHttpRequestObject.responseXML; // next I have dealing with XML file tags used DOM. var options = xmlDocument.getElementsByTagNameNS(null,"myXMLTag"); doMyFunctionWithHTMLDOM(options);
0
1231
by: ManWithNoName | last post by:
The following question is related to this thread: XSLT, document() function, filename, read non-standard/English characters (like µ) I have a XML file with a non-English character in its name (e.g. "µ.xml"). The file is loaded in a XSLT file with the XSLT function document(). While the XSLT document is being processed and the result shown (e.g. background colour, title, text in the XSLT document) , the document ("µ.xml") and its content...
0
9703
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
9566
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
10555
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10317
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
10300
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
10069
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
9127
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...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
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.