XSLT adding children in an optional structure

H

Herman Slagman

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
 
M

Martin Honnen

Herman said:
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>
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top