xsl-list
[Top] [All Lists]

Re: [xsl] Extracting Unique element names and attributes from a XML file [SEC=UNCLASSIFIED]

2008-10-28 18:41:36
In 2.0 you can use distinct-values() or xsl:for-each-group, but it
always good to learn this technique.

with  XSLT 2.0 using distinct-values and Saxon extensions (saxon:evaluate)
me and my colleague Nick Ardlie, just created this code below yesterday to
list all unique elements and attributes for each element:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:saxon="http://saxon.sf.net/";
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
    <xsl:output indent="yes" method="html"/>
    <xsl:variable name="ROOT" select="root()"/>
    <xsl:template match="/">
        <html>
            <body>
                <h1>Elements</h1>
                <ol>
                    <xsl:for-each select="distinct-values(//*/name())">
                        <xsl:sort order="ascending" select="."/>
                        <li>
                            <xsl:value-of select="."/>
                            <xsl:call-template name="LIST_ATTRIBUTES">
                                <xsl:with-param name="ELEMENT" select="."/>
                            </xsl:call-template>
                        </li>
                    </xsl:for-each>
                </ol>
            </body>
        </html>
    </xsl:template>
    <xsl:template name="LIST_ATTRIBUTES">
        <xsl:param name="ELEMENT"/>
        <xsl:variable name="XPATH_EXPR"
select="concat('$p1//*[name()=''',$ELEMENT,''']/@*/name()')"/>
        <xsl:if test="count(distinct-values(saxon:evaluate($XPATH_EXPR,
$ROOT))) &gt; 0">
            <h2>Attributes</h2>
            <ol>
                <xsl:for-each
select="distinct-values(saxon:evaluate($XPATH_EXPR, $ROOT))">
                    <xsl:sort order="ascending" select="."/>
                    <li>
                        <xsl:value-of select="."/>
                    </li>
                </xsl:for-each>
            </ol>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

Enrico Raymundo



                                                                           
             "Andrew Welch"                                                
             <andrew(_dot_)j(_dot_)welch(_at_)g                                 
            
             mail.com>                                                  To 
                                            
xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)co 
             28/10/2008 08:58               m                              
             PM                                                         cc 
                                                                           
                                                                   Subject 
             Please respond to              Re: [xsl] Extracting Unique    
             xsl-list(_at_)lists(_dot_)mu              element names and 
attributes   
              lberrytech.com                from a XML file                
                                                           Protective Mark 
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           



I need to list out all elements and attribute (unique) in a text file
for mapping with other XML file.

I am able to get all the elements and attributes but I am unable to
achieve the uniqueness. Can any body help on this.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">

If you are stuck with 1.0 then you need to use a key:

<xsl:key name="names" match="*|@*" use="name()"/>

with

<xsl:for-each select="//*|//*/@*">
  <xsl:if test="generate-id(.) = generate-id(key('names', name(.))[1])">
    <xsl:value-of select="local-name()"/>

In 2.0 you can use distinct-values() or xsl:for-each-group, but it
always good to learn this technique.



--
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--





--------------------------------------------------------------------
Important Notice: If you have received this email by mistake, please advise the 
sender and delete the message and attachments immediately.  This email, 
including attachments, may contain confidential, sensitive, legally privileged 
and/or copyright information.  Any review, retransmission, dissemination or 
other use of this information by persons or entities other than the intended 
recipient is prohibited.  DIAC respects your privacy and has obligations under 
the Privacy Act 1988.  The official departmental privacy policy can be viewed 
on the department's website at www.immi.gov.au. See: 
http://www.immi.gov.au/functional/privacy.htm

---------------------------------------------------------------------


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--