Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Help with identifying unique text elements

Reply
Thread Tools

Help with identifying unique text elements

 
 
daldridge@gmail.com
Guest
Posts: n/a
 
      10-09-2006
I have a unique-elements/sorting question (who doesn't?), but haven't
yet been able to get appropriate template/select/for-each processing
working. I don't fully grok the Muenchian technique yet (still an XSLT
n00b), but I'm not sure that's the way to go anyway...

What I'm trying to accomplish is generation of XML Schema output from a
given XML input, with "xs:import" elements in the output with the
"namespace" and "schemaLocation" attributes corresponding to a unique
set of text values found in the input file. (I don't need assistance
with the required <xsl:element> output, as I have a solution for that
figured out. Just the appropriate select/group/for-each processing, as
the xs:import's must be unique...)

If I were to put the select statement in words, I think this is what I
want to accomplish:
"determine the set of unique text values of all elements named
'PropertyClassName', which have a parent of
'PropertyClassSpec/PropertyClassVec/PropertyClassNames' or
'PropertyClassSpec/PropertyClass'".

Here is a sample input .XML file:

<?xml version="1.0" encoding="UTF-8"?>
<PropertyClassSpec xmlns="http://someDomain/PropertyClassSpec"
xmlnssi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://someDomain/PropertyClassSpec
PropertyClassSpec.xsd"
ID="12345" Name="_TestSpec">

<PropertyClass Index="0">
<Name>SomePropertyClass</Name>
<PropertyClassName>_TestSubSpec1</PropertyClassName>
</PropertyClass>

<PropertyClassVec Index="1">
<Name>VectorOfPropertyClasses</Name>
<PropertyClassNames>
<PropertyClassName>_TestSubSpec1</PropertyClassName>
<PropertyClassName>_TestSubSpec1</PropertyClassName>
<PropertyClassName>_TestSubSpec2</PropertyClassName>
<PropertyClassName>_TestSubSpec3</PropertyClassName>
</PropertyClassNames>
<ReserveSize>100</ReserveSize>
</PropertyClassVec>

<SomeOtherTopLevelElement>
<PropertyClassName>DontCatchThisOne</PropertyClassName>
</SomeOtherTopLevelElement>

<PropertyClassName>DontGrabThisOneEither</PropertyClassName>

</PropertyClassSpec>


Given that input, I'd want three xs:imports: one for _TestSubSpec1,
_TestSubSpec2, and _TestSubSpec3.


Here's the (not-quite-working) transform I have thus far (note: I'm
using Saxon8B, so feel free to offer XSLT 2.0 solutions/suggestions,
though v1.0 is fine, too):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlnssl="http://www.w3.org/1999/XSL/Transform"
xmlnss="http://www.w3.org/2001/XMLSchema"
version="2.0">

<xslutput method="xml" version="1.0" encoding="UTF-8"
indent="yes"/>

<!-- Consume any input we don't understand (no output generated) -->
<xsl:template match="*"/>

<!-- Root xs:schema element generation -->
<xsl:template match="PropertyClassSpec">
<xsl:element name="xs:schema">
<xsl:attribute name="elementFormDefault" select="'qualified'"/>
<xsl:attribute name="targetNamespace"
select="concat('http://someDomain/', @Name)"/>
<xsl:namespace name="xs"
select="'http://www.w3.org/2001/XMLSchema'"/>

<!-- I *think* this is an appropriate for-each selection...? -->
<xsl:for-each
select="PropertyClass/PropertyClassName |
PropertyClassVec/PropertyClassNames/PropertyClassName">
<xsl:sort order="ascending" select="text()"/>

<!-- Try using a remainder-based comparison I found via a
Google search. Maybe Muenchian would work, if I ever figure that out?
-->
<xsl:variable name="propertyClassName" select="text()"/>
<xsl:variable name="remainder"
select="following-sibling[text()=$propertyClassName]"/>
<xsl:if test="not($remainder)">
<xsl:element name="xs:import">
<xsl:attribute name="namespace"
select="concat('http://someDomain/',
$propertyClassName)"/>
<xsl:attribute name="schemaLocation"
select="concat($propertyClassName, '.xsd')"/>
</xsl:element>
</xsl:if>
</xsl:for-each>

<!-- The schema, as aside from the namespace and imports
determined above, has a single top-level element which is a sequence.
All further template matching/process will generate elements for this
sequence. -->
<xsl:element name="xs:element">
<xsl:attribute name="name" select="@Name"/>
<xsl:element name="xs:complexType">
<xsl:element name="xs:sequence">
<xsl:apply-templates/>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:template>

<... other template matching and processing here, to generate child
elements under the sequence element generated above ...>

</xsl:stylesheet>

Thank you for any suggestions/solutions you might be able to provide!

Cheers,
David

 
Reply With Quote
 
 
 
 
roy axenov
Guest
Posts: n/a
 
      10-09-2006

wrote:
> I have a unique-elements/sorting question (who doesn't?),
> but haven't yet been able to get appropriate
> template/select/for-each processing working. I don't
> fully grok the Muenchian technique yet (still an XSLT
> n00b), but I'm not sure that's the way to go anyway...
>
> What I'm trying to accomplish is generation of XML Schema
> output from a given XML input, with "xs:import" elements
> in the output with the "namespace" and "schemaLocation"
> attributes corresponding to a unique set of text values
> found in the input file. (I don't need assistance with
> the required <xsl:element> output, as I have a solution
> for that figured out. Just the appropriate
> select/group/for-each processing, as the xs:import's must
> be unique...)


> Here is a sample input .XML file:


[XML]

> Here's the (not-quite-working) transform I have thus far
> (note: I'm using Saxon8B, so feel free to offer XSLT 2.0
> solutions/suggestions, though v1.0 is fine, too):


You seem to have xmlns problems, among others. Anyway, try
toying with the following, it just might do what you need:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:f="http://someDomain/PropertyClassSpec"
xmlnssl="http://www.w3.org/1999/XSL/Transform"
xmlnss="http://www.w3.org/2001/XMLSchema"
version="1.0">
<xslutput
method="xml"
version="1.0"
encoding="UTF-8"/>
<xsl:template match="/">
<xsl:element name="xs:schema">
<xsl:attribute
name="elementFormDefault">qualified</xsl:attribute>
<xsl:attribute
name="targetNamespace"><xsl:value-of select="
concat('http://someDomain/',@Name)
"/></xsl:attribute>
<xsl:apply-templates select="
descendant::fropertyClass/fropertyClassName|
descendant::fropertyClassVec/fropertyClassNames
/fropertyClassName">
<xsl:sort order="ascending" select="text()"/>
</xsl:apply-templates>
</xsl:element>
</xsl:template>
<xsl:template
match="//fropertyClass/fropertyClassName|
//fropertyClassVec/fropertyClassNames
/fropertyClassName">
<xsl:if
test="generate-id(.)=
generate-id((//fropertyClass/fropertyClassName|
//fropertyClassVec/fropertyClassNames
/fropertyClassName)[text()=current()/text()][1])">
<xsl:element name="xs:import">
<xsl:attribute
name="namespace"><xsl:value-of select="
concat('http://someDomain/',text())
"/></xsl:attribute>
<xsl:attribute name="schemaLocation"><xsl:value-of
select="concat(text(), '.xsd')"/></xsl:attribute>
</xsl:element>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

--
roy axenov

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a unique method in python to unique a list? Token Type Python 9 09-09-2012 02:13 PM
list question... unique values in all possible unique spots ToshiBoy Python 6 08-12-2008 05:01 AM
Laptop has a unique identifying code number -- what is this number called? Radium Computer Support 23 07-08-2007 06:38 PM
Laptop has a unique identifying code number Radium Computer Support 36 07-02-2007 08:33 PM
Please respond with reasonable answers. [Laptop has a unique identifying code number -- what is this number called?] Radium Computer Support 9 07-01-2007 11:22 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57