Discussion:
Extracting Unique element names and attributes from a XML file
Ganesh Babu N
2008-10-28 09:38:07 UTC
Permalink
Hai All,

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.

I have tested with <xsl:sort> and not(preceding-sibling::* =
current()). Still I am unable to get the uniqueness.

My XSL is follows:

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

<xsl:output method="text"/>

<xsl:template match="/">
<xsl:for-each select="//*">
<xsl:value-of select="local-name(.)"/>
<xsl:text> </xsl:text>
<xsl:choose>
<xsl:when test="@*">
<xsl:for-each select="@*">@<xsl:value-of
select="local-name(.)"/><xsl:text> </xsl:text>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>
</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Regards,
Ganesh

--~------------------------------------------------------------------
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-***@lists.mulberrytech.com>
--~--
Michael Kay
2008-10-28 09:51:27 UTC
Permalink
In XSLT 2.0 it's simply

distinct-values(//*/name())
distinct-values(//@*/name())

If you really need to do it with XSLT 1.0, eliminating duplicates is
essentially the same problem as grouping, and you can use the Muenchian
grouping approach.

The preceding-sibling grouping technique isn't going to work (a) because
your nodes are not siblings of each other, and (b) because it only works
where the grouping key is the string-value of the node, not where it is some
other function of the node (here, it's name). Muenchian grouping works for
any string-valued function of a node.

Michael Kay
http://www.saxonica.com/
-----Original Message-----
Sent: 28 October 2008 09:38
To: XSL
Subject: [xsl] Extracting Unique element names and attributes
from a XML file
Hai All,
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.
I have tested with <xsl:sort> and not(preceding-sibling::* =
current()). Still I am unable to get the uniqueness.
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:for-each select="//*">
<xsl:value-of select="local-name(.)"/>
<xsl:text> </xsl:text>
<xsl:choose>
<xsl:for-each
</xsl:text>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>
</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Regards,
Ganesh
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
--~--
--~------------------------------------------------------------------
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-***@lists.mulberrytech.com>
--~--
Andrew Welch
2008-10-28 09:58:21 UTC
Permalink
Post by Ganesh Babu N
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-***@lists.mulberrytech.com>
--~--
E***@immi.gov.au
2008-10-28 22:40:32 UTC
Permalink
Post by Andrew Welch
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"
<***@g
mail.com> To
xsl-***@lists.mulberrytech.co
28/10/2008 08:58 m
PM cc

Subject
Please respond to Re: [xsl] Extracting Unique
xsl-***@lists.mu element names and attributes
lberrytech.com from a XML file
Protective Mark
Post by Andrew Welch
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-***@lists.mulberrytech.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-***@lists.mulberrytech.com>
--~--
Michael Kay
2008-10-28 22:56:11 UTC
Permalink
Post by E***@immi.gov.au
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
saxon:evaluate seems quite unnecessary here (and will be a lot more
expensive than necessary)
Post by E***@immi.gov.au
<xsl:template name="LIST_ATTRIBUTES">
<xsl:param name="ELEMENT"/>
<xsl:variable name="XPATH_EXPR"
<xsl:if
test="count(distinct-values(saxon:evaluate($XPATH_EXPR,
Just do

<xsl:if test="exists($ROOT//*[name()=$ELEMENT]/@*)">

Note: count(X)>0 is equivalent to exists(X), and exists(distinct-values(X))
is equivalent to exists(X).
Post by E***@immi.gov.au
<xsl:for-each
select="distinct-values(saxon:evaluate($XPATH_EXPR, $ROOT))">
Here you can do

select="distinct-values($ROOT//*[name()=$ELEMENT]/@*/name())"

Michael Kay
http://www.saxonica.com/


--~------------------------------------------------------------------
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-***@lists.mulberrytech.com>
--~--
Wendell Piez
2008-10-29 14:45:29 UTC
Permalink
Post by Michael Kay
saxon:evaluate seems quite unnecessary here (and will be a lot more
expensive than necessary)....
Also, Mike earlier suggested using xsl:for-each-group, which offers a
nice solution:

<xsl:for-each-group select="//*" group-by="name()">
<xsl:text>&#xA;&#xA;</xsl:text><!-- two new lines -->
<xsl:value-of select="current-grouping-key()"/>
<xsl:for-each-group select="current-group()/@*" group-by="name()">
<xsl:text>&#xA; </xsl:text><!-- new line, indent -->
<xsl:value-of select="current-grouping-key()"/>
</xsl:for-each-group>
</xsl:for-each-group>

This is somewhat different from what was posted, in that it lists
distinct attributes that may appear with each element, but this is
usually useful information -- and if you do want a list of attributes
appearing absolutely, just do the element thing again, except
selecting "//@*". (Also, I haven't taken the trouble to make HTML out of it.)

This approach can be taken one more step to report values of
attributes, or repeating values of attributes (which sometimes makes
for less noise).

Cheers,
Wendell



======================================================================
Wendell Piez mailto:***@mulberrytech.com
Mulberry Technologies, Inc. http://www.mulberrytech.com
17 West Jefferson Street Direct Phone: 301/315-9635
Suite 207 Phone: 301/315-9631
Rockville, MD 20850 Fax: 301/315-8285
----------------------------------------------------------------------
Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================


--~------------------------------------------------------------------
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-***@lists.mulberrytech.com>
--~--

Continue reading on narkive:
Loading...