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

Urgent XSL question, please help, thanks

I have xml tree like

<root>
<field name="l1">
<field name="l11">
<field name="l111"/>
<field name="l112"/>
</field>
<field name="l2" stop="true">
<field name="l21">
<field name="l211"/>
<field name="l212"/>
</field>
<field name="l22"/>
</field>
<field name="l3"/>
</root>

The tree could have many levels, I need a for-each to collect all node name
under current context, the key is the current context could be on any node,
for example, it could be the "l1", it could be "l2" or node under "l2". I
tried to use code like

<xsl:for-each select=".//field[not(@stop)]/field">
<xsl:value-of select="@name"/>,
</xsl:for-each>

It do the recursive loop, the problem is if the current context is "l2", it
will not work. The idea is it check all childnodes without "stop", not the
start node (the current context). I have no way to know if the node is top
node in // , and I don't know how to get current level ( I remember there
isn't such function).

Any idea?

Thanks!
Sep 26 '06 #1
4 1224
davidw wrote:
I have xml tree like

<root>
<field name="l1">
<field name="l11">
<field name="l111"/>
<field name="l112"/>
</field>
<field name="l2" stop="true">
<field name="l21">
<field name="l211"/>
<field name="l212"/>
</field>
<field name="l22"/>
</field>
<field name="l3"/>
</root>
Your XML is not well-formed: it's missing a </fieldend-tag before the
</rootend-tag.
The tree could have many levels, I need a for-each to collect all node name
under current context, the key is the current context could be on any node,
for example, it could be the "l1", it could be "l2" or node under "l2". I
tried to use code like
I'm not clear what you want to do, create a node-set of all the @name
attributes of field elements which are descendants of the context
element, or list the @name values. Here is an XSLT script which lists
the values. No need for for-each: use templates.

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

<xsl:output method="text"/>
<xsl:strip-space elements="*"/>

<xsl:template match="field">
<xsl:value-of select="@name"/>
<xsl:text>: </xsl:text>
<xsl:apply-templates select="descendant::field" mode="sub"/>
<xsl:text>&#xA;</xsl:text>
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="field" mode="sub">
<xsl:value-of select="@name"/>
<xsl:text</xsl:text>
</xsl:template>

</xsl:stylesheet>

It's also not clear what you want to do with the element with a stop
value: cease processing, or simply exclude it (and its descendants)
from the recursion?

///Peter
--
XML FAQ: http://xml.silmaril.ie/
///Peter
Sep 26 '06 #2
Hi,

Thanks.

The XML has a type error. It just a fake data to demo what I want.

It seems I didn't express clearly. What I need is from any node, I can get
all names in its subtree, but exclude those with a stop attribute. check
this xml

<field name="n0">
<field name="n1">
<field name="n11">
<field name="n111"/>
<field name="n112"/>
</field>
</field>
<field name="n2" stop="true">
<field name="n21">
<field name="n211"/>
<field name="n212"/>
</field>
<field name="n22"/>
</field>
<field name="n3"/>
</field>

For example, it the current node is n0, I will get
n11,n111,n112,n2,n3 -- it will not go into n2 since it has a stop
if the current node is n2, I should get n21,n211,n212,n22 -- it should not
affected by the stop, since it only filter childnodes with stop attribute.

I get used to use for-each. Is there a way to do that with for-each?

Thanks!

"Peter Flynn" <pe********@m.silmaril.iewrote in message
news:4n************@individual.net...
davidw wrote:
I have xml tree like

<root>
<field name="l1">
<field name="l11">
<field name="l111"/>
<field name="l112"/>
</field>
<field name="l2" stop="true">
<field name="l21">
<field name="l211"/>
<field name="l212"/>
</field>
<field name="l22"/>
</field>
<field name="l3"/>
</root>

Your XML is not well-formed: it's missing a </fieldend-tag before the
</rootend-tag.
The tree could have many levels, I need a for-each to collect all node
name
under current context, the key is the current context could be on any
node,
for example, it could be the "l1", it could be "l2" or node under "l2".
I
tried to use code like

I'm not clear what you want to do, create a node-set of all the @name
attributes of field elements which are descendants of the context
element, or list the @name values. Here is an XSLT script which lists
the values. No need for for-each: use templates.

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

<xsl:output method="text"/>
<xsl:strip-space elements="*"/>

<xsl:template match="field">
<xsl:value-of select="@name"/>
<xsl:text>: </xsl:text>
<xsl:apply-templates select="descendant::field" mode="sub"/>
<xsl:text>&#xA;</xsl:text>
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="field" mode="sub">
<xsl:value-of select="@name"/>
<xsl:text</xsl:text>
</xsl:template>

</xsl:stylesheet>

It's also not clear what you want to do with the element with a stop
value: cease processing, or simply exclude it (and its descendants)
from the recursion?

///Peter
--
XML FAQ: http://xml.silmaril.ie/
///Peter

Sep 27 '06 #3
Hi,

Thanks.

The XML has a type error. It just a fake data to demo what I want.

It seems I didn't express clearly. What I need is from any node, I can get
all names in its subtree, but exclude those with a stop attribute. check
this xml

<field name="n0">
<field name="n1">
<field name="n11">
<field name="n111"/>
<field name="n112"/>
</field>
</field>
<field name="n2" stop="true">
<field name="n21">
<field name="n211"/>
<field name="n212"/>
</field>
<field name="n22"/>
</field>
<field name="n3"/>
</field>

For example, it the current node is n0, I will get
n11,n111,n112,n2,n3 -- it will not go into n2 since it has a stop
if the current node is n2, I should get n21,n211,n212,n22 -- it should not
affected by the stop, since it only filter childnodes with stop attribute.

I get used to use for-each. Is there a way to do that with for-each?

Thanks!

"Peter Flynn" <pe********@m.silmaril.iewrote in message
news:4n************@individual.net...
davidw wrote:
I have xml tree like

<root>
<field name="l1">
<field name="l11">
<field name="l111"/>
<field name="l112"/>
</field>
<field name="l2" stop="true">
<field name="l21">
<field name="l211"/>
<field name="l212"/>
</field>
<field name="l22"/>
</field>
<field name="l3"/>
</root>

Your XML is not well-formed: it's missing a </fieldend-tag before the
</rootend-tag.
The tree could have many levels, I need a for-each to collect all node
name
under current context, the key is the current context could be on any
node,
for example, it could be the "l1", it could be "l2" or node under "l2".
I
tried to use code like

I'm not clear what you want to do, create a node-set of all the @name
attributes of field elements which are descendants of the context
element, or list the @name values. Here is an XSLT script which lists
the values. No need for for-each: use templates.

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

<xsl:output method="text"/>
<xsl:strip-space elements="*"/>

<xsl:template match="field">
<xsl:value-of select="@name"/>
<xsl:text>: </xsl:text>
<xsl:apply-templates select="descendant::field" mode="sub"/>
<xsl:text>&#xA;</xsl:text>
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="field" mode="sub">
<xsl:value-of select="@name"/>
<xsl:text</xsl:text>
</xsl:template>

</xsl:stylesheet>

It's also not clear what you want to do with the element with a stop
value: cease processing, or simply exclude it (and its descendants)
from the recursion?

///Peter
--
XML FAQ: http://xml.silmaril.ie/
///Peter

Sep 27 '06 #4
davidw wrote:
The XML has a type error. It just a fake data to demo what I want.
The problem is that if you want someone to work on what you provide,
it's a courtesy to provide data that can be used.
It seems I didn't express clearly. What I need is from any node, I can get
all names in its subtree, but exclude those with a stop attribute. check
this xml

<field name="n0">
<field name="n1">
<field name="n11">
<field name="n111"/>
<field name="n112"/>
</field>
</field>
<field name="n2" stop="true">
<field name="n21">
<field name="n211"/>
<field name="n212"/>
</field>
<field name="n22"/>
</field>
<field name="n3"/>
</field>

For example, it the current node is n0, I will get
n11,n111,n112,n2,n3 -- it will not go into n2 since it has a stop
if the current node is n2, I should get n21,n211,n212,n22 -- it should not
affected by the stop, since it only filter childnodes with stop attribute.
OK, now I get it.
I get used to use for-each.
Don't. The for-each is normally kept for processing nodes out of
document sequence, or for processing nodes sorted into a different
order.
Is there a way to do that with for-each?
Possibly, but that's procedural thinking. XSLT isn't like that.

This seems to do the trick:

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

<xsl:output method="text"/>
<xsl:strip-space elements="*"/>

<xsl:template match="field">
<xsl:value-of select="@name"/>
<xsl:text>: </xsl:text>
<xsl:apply-templates select="child::field[not(@stop='true')]"
mode="sub"/>
<xsl:text>&#xA;</xsl:text>
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="field" mode="sub">
<xsl:value-of select="@name"/>
<xsl:text</xsl:text>
<xsl:apply-templates select="child::field[not(@stop='true')]"
mode="sub"/>
</xsl:template>

</xsl:stylesheet>

///Peter
Sep 27 '06 #5

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

Similar topics

2
by: js | last post by:
Hi all, I currently encounter a problem and it is urgent to me. After calling the MsgBox.Show(), the message box is shown with non-modal mode, what is the possible reason??? This only happen...
12
by: Vibhajha | last post by:
Hi friends, My sister is in great problem , she has this exam of C++ and she is stuck up with some questions, if friends like this group provides some help to her, she will be very grateful....
2
by: learner | last post by:
Hi, In a page, i have many links. I have some functions in a global file which is included in all linked pages. I want to have a reference to a window which is to be opened on clicking one link...
8
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is...
5
by: Vishal | last post by:
Hello, I already asked this question in the ASP.NET forums, but no help came. So I am hoping that somebody can help me out. This is really very URGENT me. For my e-commerce application, I...
3
by: ricolee99 | last post by:
Hi everyone, I have a problem that i have been trying to solve for awhile. I'm given a code where the purpose is to create a general dataset mapper. Given any dataset, i have a class,...
17
by: Saps | last post by:
Hi all. Can anyone help me here. I have loads of .sql files and i need a way to call these from my asp page so the user can run them from the browser. Meaning i have a page with a list of all...
3
by: Mike | last post by:
Hi, I'm wondering if I can do this with ASP.NET?
1
by: sachinv1821 | last post by:
hi all , i am getting this Problem i am Unable to Load the DLL/ACM, the Problem with this (DLL /ACM ) is its a C code but this DLL using the C++ library i written a wrapper for C++ library and...
0
by: imran haq | last post by:
Hi All, I have 3 rather Long Questions that are causing alot of trouble: I would appreciate all the help i can get and tried to use A post sent to atli in the past but it did not help... !) I...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.