[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

XML XSLT return values for duplicate/repeating elements

Posted on 2006-03-31
8
Medium Priority
?
678 Views
Last Modified: 2013-11-19
Very new to XML so excuse if I don't have my terms right yet.

I've got some XML looks  like this:

<port protocol="tcp" portid="21"><state state="open" /><service name="ftp" method="table" conf="3" /></port>
<port protocol="tcp" portid="53"><state state="open" /><service name="domain" method="table" conf="3" /></port>
<port protocol="tcp" portid="80"><state state="open" /><service name="http" method="table" conf="3" /></port>
<port protocol="tcp" portid="135"><state state="open" /><service name="msrpc" method="table" conf="3" /></port>


I'm want to return all values for "service name" and display in a single cell of a table (i.e.,  ftp, domain, http, msrpc).

All I get with <td><xsl:value-of select="ports/port/service/@name" /></td> is the first value, "ftp".

What am I trying to do (by that I mean, what is the operation called)? And, better yet, how do I do it?








0
Comment
Question by:tnapolitano
  • 4
  • 3
7 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 16348546
Hi tnapolitano,

This XSLT will make you a table with all the values

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <html>
            <body>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="ports">
        <table border="1">
            <xsl:apply-templates select="port"/>
        </table>
    </xsl:template>
    <xsl:template match="port">
        <tr>
            <td><xsl:value-of select="service/@name" /></td>
        </tr>
    </xsl:template>
</xsl:stylesheet>

Cheers!
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 16348557
tnapolitano,

I have templates (rules based nature of XSLT) for the different elements
and inside one template I push nodesets to the templates with apply-templates
like that I create a very hierarchical model for processing the XML

the stylesheet starts working with the template match="/"
There I create the html and body tag and in the middle of that I push all the chilnodes to the templates
The element "ports" gets picked up by the template for "ports"
and there I create a table and push the port nodes to the templates
As you see now, I have created a table inside the html/body

the template match="port" will be executed for every port element inside ports
this is how I create a number of rows , one for each port

If you would want only one row, this is what you would do

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <html>
            <body>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="ports">
        <table border="1">
            <tr>
                <xsl:apply-templates select="port"/>
            </tr>
         </table>
    </xsl:template>
    <xsl:template match="port">
             <td><xsl:value-of select="service/@name" /></td>
    </xsl:template>
</xsl:stylesheet>

take your time to understand the examples
note that this way of programming XSLT is superior over using for-each loops

cheers

Geert
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 16348566
tnapolitano,

from your title I assume that you only want unique values

here would be the XSLT that ignores a duplicate value "ftp" and only shows it want
I do this by being selective on the port-nodes that I push to the port template
(it should not have a value of service/@name that a preceding port element has)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <html>
            <body>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="ports">
        <table border="1">
            <tr>
                <xsl:apply-templates select="port[not(service/@name = preceding::port/service/@name)]"/>
            </tr>
         </table>
    </xsl:template>
    <xsl:template match="port">
             <td><xsl:value-of select="service/@name" /></td>
    </xsl:template>
</xsl:stylesheet>

I hope this is enough to get you started

Geert
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Author Comment

by:tnapolitano
ID: 16348820
Gertone, thanks for all that. Let me try this out and I'll get back to you.
0
 
LVL 3

Author Comment

by:tnapolitano
ID: 16352050
Great answer, gertone. Just what I needed. Both the code to get the job done and explanations of what's going on. Thanks again.
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 16353076
welcome
0
 
LVL 3

Author Comment

by:tnapolitano
ID: 16633036
I accept this answer. You can close it out.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question