Im not sure, but if you are looking for children of a particular node, eg data
you could use : for each instance of data look for children (*) which have @value
so
data/*/@value
should be the right xpath
MM
Main Topics
Browse All TopicsDear All,
I have some XML as follows:
<FormData>
<Data>
<Date value="14 Nov 2005"/>
<Location value="1001"/>
<InStation1 value="Y"/>
<T1 value="A"/>
<InStation2 value="N"/>
<T2 value="B"/>
<InStation3 value="Y"/>
<T3 value="B"/>
</Data>
<Data>
<Date value="14 Nov 2005"/>
<Location value="1001"/>
<InStation1 value="Y"/>
<T1 value="A"/>
<InStation2 value="N"/>
<T2 value="B"/>
<InStation3 value="Y"/>
<T3 value="B"/>
</Data>
</FormData>
I can't change the XML, but rather than having to write out XSLT for InStation1 - InStation3 and T1 - T3, I was wondering if there was a way to get the XSLT to loop through them so I only need to write a few lines of XSLT (of course, in reality I have more than just the three!). At the moment, I am doing:
<xsl:value-of select="InStation1/@value"
<xsl:value-of select="T1/@value"/>
<xsl:value-of select="InStation2/@value"
<xsl:value-of select="T2/@value"/>
<xsl:value-of select="InStation3/@value"
<xsl:value-of select="T3/@value"/>
Any help much appreciated!
Regards,
Ben.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
webtechy,
It might be that you want to exclude "Date" and "Location"
Here is the loop that takes all the "Data" children that have a name starting with 'T' or 'InStation'
<xsl:for-each select="*[starts-with(name
<xsl:value-of select="@value"/>###
</xsl:for-each>
both this answer and the previous assume you are doing this in the context of "Data"
safe assumption since your <xsl:value-of select="InStation1/@value"
seems to work
cheers
webtechy,
To take away all confusion
select="T[*]"
is the element with name 'T' that has a subelement (name '*', so undefined)
before the [] you have the path of the node you are looking for
the [] (called predicate) creates a specific condition
what you wrote in pseudocode
is *[starts-with(name(), 'T')] in real XPath
the element '*' (means can be any name)
that has a name that starts with a 'T'
there is a way to guarantuee that the remainder is an integer but that is a bit more complex
If you are sure you don't have an element like "Tate" you don't won't to address,
I would leave it like this
cheers
webtechy,
> <Data>
> <Date value="14 Nov 2005"/>
> <Location value="1001"/>
> <InStation1 value="Y"/>
> <T1 value="A"/>
> <InStation2 value="N"/>
> <T2 value="B"/>
> <InStation3 value="Y"/>
> <T3 value="B"/>
> </Data>
as you said, you can't change the XML, you probably realise this is very bad modelling practice.
With the element names you should try to give some sort of meaningfull structure
dynamic data belongs in an attribute value or the content, not in ever changing element names.
XML created this way, is very hard to process or validate
or very difficult to document
On top of that I have the feeling this XML tries to combine values based on the number in the tagname
I would definitely go for an XML like this
<Data>
<Date value="14 Nov 2005"/>
<Location value="1001"/>
<Station id="1">
<InStation>Y</InStation>
<T>A</T>
</Station>
<Station id="2">
<InStation>N</InStation>
<T>B</T>
</Station>
<Station id="3">
<InStation>Y</InStation>
<T>B</T>
</Station>
</Data>
This gives you an explicit grouping of "Instation" and "T"
and a clear model of Station
but perhaps you already knew
cheers
Geert
Business Accounts
Answer for Membership
by: GertonePosted on 2005-11-21 at 02:38:13ID: 15332744
<xsl:for-each select="*[@value]">
<xsl:value-of select="@value"/>
</xsl:for-each>
loop over all th eelements that have an attribute value