Excluding xsl selection for elements with missing attributes

T

Terry Chilvers

In XSLT I want to select elements and output the value of a particular
attribute. However I don't want to process the element if it doesn't contain
this attribute. I'm using this to generate some code from an xsd i.e. I'm
traeting the xsd as a source xml file.
e.g.
<xs:element name="prompt" substitutionGroup="tech.char.class">
<xs:complexType mixed="true">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:group ref="smallcptr.char.mix"/>
<xs:element ref="co"/>
</xs:choice>
<xs:attributeGroup ref="moreinfo.attrib"/>
<xs:attributeGroup ref="common.attrib"/>
<xs:attributeGroup ref="prompt.role.attrib"/>
</xs:complexType>
</xs:element>
Here I want to output a line if the attribute "substitutionGroup" exists,
and nothing if it doesn't.
I've looked for examples, but no joy. Any ideas please?
Here's a snippet from the xslt:
<xsl:for-each select="//xs:element">
element: <xsl:value-of select="@name"/>
SubstGroup: <xsl:value-of select="@substitutionGroup"/>
but it always outputs a "SubstGroup:" line for every element.

Thanks
Terry
 
M

Mats Kindahl

Terry Chilvers said:
In XSLT I want to select elements and output the value of a particular
attribute. However I don't want to process the element if it doesn't contain
this attribute. I'm using this to generate some code from an xsd i.e. I'm
traeting the xsd as a source xml file.
e.g.
<xs:element name="prompt" substitutionGroup="tech.char.class">
<xs:complexType mixed="true">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:group ref="smallcptr.char.mix"/>
<xs:element ref="co"/>
</xs:choice>
<xs:attributeGroup ref="moreinfo.attrib"/>
<xs:attributeGroup ref="common.attrib"/>
<xs:attributeGroup ref="prompt.role.attrib"/>
</xs:complexType>
</xs:element>
Here I want to output a line if the attribute "substitutionGroup" exists,
and nothing if it doesn't.
I've looked for examples, but no joy. Any ideas please?
Here's a snippet from the xslt:
<xsl:for-each select="//xs:element">
element: <xsl:value-of select="@name"/>
SubstGroup: <xsl:value-of select="@substitutionGroup"/>

This might work (untested):

<xsl:for-each select="//xs:element">
element: <xsl:value-of select="@name"/>
<xsl:if test="@substitutionGroup">
SubstGroup: <xsl:value-of select="@substitutionGroup"/>
</xsl:if>
</xsl:for-each>

Good luck!
Mats Kindahl
--
IAR Systems in Uppsala, Sweden.

Any opinions expressed are my own and not those of my company.

Spam prevention: Contact me at (e-mail address removed), removing the *NO
SPAM* from the address.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top