Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT adding children in an optional structure

Reply
Thread Tools

XSLT adding children in an optional structure

 
 
Herman Slagman
Guest
Posts: n/a
 
      05-28-2009
Suppose I have a structure like:

<person>
<name>Herman</name>
<address>Street</address>
<city>Amsterdam</city>
<country>The Netherlands</country>
</person>

All sub-elements of <person> are optional, but the sequence is important.

If I have a <person> that doesn't have a <city> element and I want to add
it.
It must be after <address> if present, otherwise after <name>, or at least
inside the <person>, but before <country> if present.

How do I do that ?

Thanks,

Herman


 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      05-28-2009
Herman Slagman wrote:
> Suppose I have a structure like:
>
> <person>
> <name>Herman</name>
> <address>Street</address>
> <city>Amsterdam</city>
> <country>The Netherlands</country>
> </person>
>
> All sub-elements of <person> are optional, but the sequence is important.
>
> If I have a <person> that doesn't have a <city> element and I want to
> add it.
> It must be after <address> if present, otherwise after <name>, or at
> least inside the <person>, but before <country> if present.


You would need to spell that out I think:

<xsl:template match="person">
<xsl:copy>
<xsl:copy-of select="name"/>
<xsl:if test="not(name)">
<name>...</name>
</xsl:if>
<xsl:copy-of select="address"/>
<xsl:if test="not(address)">
<address>...</address>
</xsl:if>
<xsl:copy-of select="city"/>
<xsl:if test="not(city)">
<city>...</city>
</xsl:if>
<xsl:copy-of select="country"/>
<xsl:if test="not(country)">
<country>...</country>
</xsl:if>
</xsl:copy>
</xsl:template>



--

Martin Honnen
http://msmvps.com/blogs/martin_honnen/
 
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
XSLT question: How to lookup another tag's children in XSLT yinglcs@gmail.com XML 18 10-03-2006 11:22 PM
Optional elements in a structure declaration? ( URGENT) zoltan C Programming 14 03-24-2006 02:58 PM
XSLT optional attributes Eric Anderson XML 4 10-08-2004 01:13 PM
xslt and optional attributes Edwin G. Castro XML 1 09-24-2004 08:35 PM
XSLT Select nodes without text-node children whose names starts with specifix text Michael Reiche XML 3 02-05-2004 10:40 PM



Advertisments