Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
Hope someone can assist me as I'm stuck at the moment.
I have this XML (shortcut/edited version):

XML
<!-- Line -->
    <buttonNumber>1</buttonNumber>
    <buttonTypeId>1</buttonTypeId>
    <speedDialDisplay/>
    <speedDialNumber/>
    <sipProxy>proxy.com.au</sipProxy>
    <endpoint>
      <phoneNumber>09900001</phoneNumber>
      <buttonDisplayName>09900001</buttonDisplayName>
      <callerDisplayName>09900001</callerDisplayName>
    </endpoint>
  </builtInButtons>
<!-- Monitor -->
    <buttonNumber>2</buttonNumber>
    <buttonTypeId>2</buttonTypeId>
    <speedDialDisplay/>
    <speedDialNumber/>
    <sipProxy>proxy.com.au</sipProxy>
    <endpoint>
      <phoneNumber>09900000</phoneNumber>
      <buttonDisplayName>09900000</buttonDisplayName>
      <callerDisplayName>09900000</callerDisplayName>
    </endpoint>
<!-- Speed Dial -->
<buttonNumber>3</buttonNumber>
    <buttonTypeId>3</buttonTypeId>
    <speedDialDisplay>Speed Dial Name</speedDialDisplay>
    <speedDialNumber>12345678</speedDialNumber>
    <sipProxy>proxy.com.au</sipProxy>
<!-- Empty -->
      <buttonNumber>4</buttonNumber>
      <buttonTypeId>2</buttonTypeId>
      <speedDialDisplay/>
      <speedDialNumber/>
      <sipProxy>proxy.com.au</sipProxy>
    </buttons>


The values I'm after are to meet this conditions:
- buttonTypeId either 2 or 3, and speedDialNumber or phoneNumber are not Empty
So using this XSL, I AM ABLE TO GET THE VALUES I'M AFTER

XML
<xsl:for-each select="builtInButtons">
   <xsl:if test="buttonTypeId = '2' or buttonTypeId = '3'">
    <xsl:if test="endpoint/phoneNumber != '' or speedDialNumber != ''">


The issue i have is DISPLAYING THEM, AS THE OUTPUT NEEDS TO BE INCREMENTAL:
e.g
if there are 2 items, satisfies the conditions, output as:

XML
item.1=<value>
item.2=<value>
if there are 3 items,
item.1=<value>
item.2=<value>
item.3=<value>


I'm able to get the values im after, but displaying it is the problem. using counters, would have fixed the issue easy, however i found the hard way that XSL doesnt do counters.
there should be other way of approaching this, please help ?


Thanks for your help.
Posted
Updated 24-Feb-11 14:19pm
v3
Comments
Abdul Quader Mamun 24-Feb-11 20:19pm    
Edited for qustion improvment.

1 solution

Maybe try playing around with the number element and position() function? You may have to do some stuff with XPath to get the right set of nodes first.

For example,

XML
<xsl:template match="/">
  <html>
  <body>
  <xsl:for-each select="/builtInButtons/button[(buttonTypeId='2' or buttonTypeId='3') and speedDialNumber!=''] | /builtInButtons/button[buttonTypeId='2' or buttonTypeId='3']/endpoint[phoneNumber!='']/ancestor::button">
    item.<xsl:number value="position()" format="1" />=button <xsl:value-of select="buttonNumber"/><br />
  </xsl:for-each>
  </body>
  </html>
</xsl:template>


gives me this as the result with your XML:

item.1=button 2
item.2=button 3


See Example #4 on http://www.w3schools.com/xsl/el_number.asp[^]
 
Share this answer
 
v3
Comments
sleepy.head28 28-Feb-11 17:21pm    
Hi, thanks for that.
the first node I'm after is not always the first item in the xml, in fact its usually at the end of xml.
any thoughts ?
Kythen 28-Feb-11 17:35pm    
That's where the XPath query comes in. I made some assumptions about your XML data, but the query in my example should do the same thing as the if elements in your XSLT. Change it to whatever you need.
sleepy.head28 28-Feb-11 17:49pm    
Ahm sorry to ask this, but knowing that the xml nodes I'm after are usually at the end of xml, can u give me a sample of what this XPath query may look like ?
Kythen 4-Mar-11 12:28pm    
Sorry, I can't really give you anything beyond the example from my answer. I have no idea what criteria you are using to find the nodes you want. That w3schools site has some decent tutorials on XPath queries. I can also recommend using a tool called Sketchpath from http://qutoric.com/sketchpath/ that can be a lot of help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900