help with element deletion using xslt

W

WideBoy

Hi,

I have a software generated schema which creates a few empty elements,
e.g. <xsd:sequence/>. I would like to be able to delete these elements
from the schema post-generation using XSLT.

I've tried using the identity pattern to copy the whole schema except
these rogue elements but I seem to be getting stuck on how to
correctly choose this particular 'sequence' element to ignore during
copying.

I would really appreciate some pointers on how I could do this.

regards,

Wideboy
 
P

Pavel Lepin

WideBoy said:
I have a software generated schema which creates a few
empty elements, e.g. <xsd:sequence/>. I would like to be
able to delete these elements from the schema
post-generation using XSLT.

I've tried using the identity pattern to copy the whole
schema except these rogue elements but I seem to be
getting stuck on how to correctly choose this particular
'sequence' element to ignore during copying.

Assuming you've bound the XML Schema namespace to the xsd
prefix in your transformation:

xsd:sequence[not(*)]
 
W

WideBoy

I have a software generated schema which creates a few
empty elements, e.g. <xsd:sequence/>. I would like to be
able to delete these elements from the schema
post-generation using XSLT.
I've tried using the identity pattern to copy the whole
schema except these rogue elements but I seem to be
getting stuck on how to correctly choose this particular
'sequence' element to ignore during copying.

Assuming you've bound the XML Schema namespace to the xsd
prefix in your transformation:

xsd:sequence[not(*)]

Pavel,

Thanks for your advice, unfortunately, being a total novice it did not
work for me.

Here's my input test schema and xslt that I'm using.
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:element name="TestSchema">
<xsd:annotation>
<xsd:documentation>Comment describing your root element</
xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:complexType name="VarCharStructure">
<xsd:annotation>
<xsd:documentation>A characteristic that can vary over time. The
time at which the characteristic is recorded is given by the activity
or occurrence of information to which it is linked.</
xsd:documentation>
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="SuperClassStructure">
<xsd:sequence/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="SuperClassStructure">
<xsd:annotation>
<xsd:documentation>Provides a referencable ID element for all class
objects</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="SuperClassURN" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

Processed using this XSLT:
<xsl:eek:utput method="xml" version="1.0" encoding="UTF-8" indent="yes"/ <!-- copy through any comments or processing instructions found in
the input schema -->
<xsl:template match="comment()|processing-instruction()">
<xsl:copy/>
</xsl:template>
<!-- copy any other elements and associated attributes and values -->
<xsl:template match="*">
<xsl:if test=".//xsd:sequence[not(*)]"/>
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

Produces the same output as the input demonstrating my total lack of
understanding of xslt.

best regards,

W.
 
M

Martin Honnen

WideBoy said:
Produces the same output as the input demonstrating my total lack of
understanding of xslt.

You simply need

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

<xsl:template match="xs:sequence[not(node())]"/>

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

The second template above is the identity transformation template, the
first template ensures that empty xs:sequence elements are not copied.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top