473,372 Members | 1,103 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,372 software developers and data experts.

xsl Nodename occurs more than once on the same level

hello,
I am new to Bytes and I am not sure if this is the right plattform. but I found several posts regarding XSL. (If this is not the rigth forum, I apologize. Could you point me to another one then please.)

I have an xsl stylesheet which reads an xml input file and produces an xml output which shows the structure of the source xml file.

I want to include the following information in the output:
which of the output-nodes has any siblings with the same name:

But I have difficulties modifying the stylesheet to achieve this.


example of the source xml:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <book>
  3. <chapter>
  4. <title/>
  5. <lines></lines>
  6. <lines></lines>
  7. </chapter>
  8. <chapter>
  9. <title/>
  10. <lines></lines>
  11. </chapter>
  12. </book>
  13.  
the output:
Expand|Select|Wrap|Line Numbers
  1. <book>
  2. <chapter>
  3. <title/>
  4. <lines/>
  5. </chapter>
  6. </book>
the stylesheet - which I copied from the net:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3. <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
  4. <xsl:template match="/" name="subtree">
  5.   <xsl:param name="parents" select="."/>
  6.   <xsl:for-each-group select="$parents/*" group-by="name()">
  7.   <xsl:copy>
  8.   <xsl:call-template name="subtree">
  9.   <xsl:with-param name="parents" select="current-group()"/>
  10.   </xsl:call-template>
  11.   </xsl:copy>
  12.   </xsl:for-each-group>
  13. </xsl:template>
  14. </xsl:stylesheet>
thanks in advance for any hints
Markus
Mar 26 '09 #1
6 3902
jkmyoung
2,057 Expert 2GB
Sorry, do you mean which of the elements repeat?

<xsl:if test="count(current-group() &gt; 1)">
Multiple elements of this type.

Or do you mean which of the elements repeat within the same element? Probably something like
<xsl:if test="current-group[local-name() = local-name(following-sibling::*)]">
Mar 27 '09 #2
thanks very much for your answer.

I apologize that I am slow to answer.

To answer your question:
I want to know if elements share the same parent element.


So I tried your idea with looking for a following-sbiling with identical name.
But I cannot get it figured out. If I write:
following-sibling::*[name()=name(.)]

I get all siblings of the current node not only the ones with identical name.
I obviously misunderstand the syntax of xpath but cannot see where.

I also do not understand part of your solution:
local-name(following-sibling::*)
is this valid xpath? how can I ask for the local name of all following-siblings??


thanks Markus
Apr 2 '09 #3
Dormilich
8,658 Expert Mod 8TB
@Ohlenroth
yes, it's valid XPath and it gives you the local name (i.e. tag name without namespace prefix) of the next sibling, regardless of the sibling's tag name.

you can't easily get the local name of all siblings (I guess it involves sorting), because they may be different and local-name() accepts only a single node.
Apr 2 '09 #4
thanks for your help.

I was puzzeld about the xpath expression because in the program I use it is a legal xpath expression if the debugger is in the top template. But on the next template the debugger says: "Error in xpath expression...." I seem to fail to understand important things in xpath.


In order to find a solution to my question, I tried out to find an xpath expression which finds out if the current node has any siblings:

I have the following input:

Expand|Select|Wrap|Line Numbers
  1. <artikel>
  2. <Lemma>Lemma</Lemma>
  3. <Variante code="1">V1</Variante>
  4. <Variante code="2">V2</Variante>
  5. <Variante code="3">V3</Variante>
  6. <Grammatik>gramma</Grammatik>
  7. </artikel>
I ask - once the current node is <Variante/> -:
count(parent::*/*[name()='Variante'])
this gives me as I had hoped "3" as answer.

But if I write instead
count(parent::*/*[name()=local-name()])
I get 5 as answer:
although xpath: local-name() gives me 'Variante' as name:

Why should I get a different answer? I seem to fail to understand important parts in XPATH.
Apr 2 '09 #5
Dormilich
8,658 Expert Mod 8TB
@Ohlenroth
it gives you 5 because the parent element has 5 children and you test, whether the tag name contains a namespace prefix because name() and local-name() are pointing to the same node (Lemma, Grammatik, 3x Variante).

try count(parent::*/*[local-name(current())=local-name()]). if that's not giving 3 I can only think of using a variable.
Apr 2 '09 #6
Yes, your expression gives the correct answer.
I shall fit it in my program
thanks for your help and time.
Markus
Apr 2 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
4
by: John Bailo | last post by:
AJAX demo: http://texeme.com/ajaxtest.html -- Texeme Construct http://texeme.com
54
by: zhaoyandong | last post by:
In one of my interview, some people asked me why C is faster C++, and tell me to illustrate at least two reasons. I can't find the answer in the web. I'll appreciate any suggestion on this....
116
by: Mike MacSween | last post by:
S**t for brains strikes again! Why did I do that? When I met the clients and at some point they vaguely asked whether eventually would it be possible to have some people who could read the data...
109
by: sonugeetha | last post by:
Hi, Please help me in this regard... All the kernel level programs are written in C... (i.e: Open Source LINUX)... Why are they not using C++... I personally feel that C++ is more easy to code...
8
by: Brian Tkatch | last post by:
Server: DB2/SUN 8.1.6 Client: DB2 Connect Personal Edition (No 11) <URL:ftp://ftp.software.ibm.com/ps/products/db2/fixes2/english-us/db2winIA32v8/fixpak/FP11_WR21365/FP11_WR21365_CONPE.exe> ...
9
by: master | last post by:
Actually, it is not only the record locking, what I need, and nobody seems to descibe this. Imagine the following scenario. There is a database with, say 10000 records with some unvalidated...
3
by: Eric Layman | last post by:
Hi, If an E-coupon is limited to one person, are there ways to prevent it from printing more than once? Since on the print dialog, user can print multiple copies. Regards
14
by: juanpolotto | last post by:
Hi. I'm write a "passthru" application which is reading from one socket and writing to a pool of outcoming sockets. From time to time I have date duplicated in the socket and when I debug the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.