XSLT adding children in an optional structure

Discussion in 'XML' started by Herman Slagman, May 28, 2009.

  1. 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
     
    Herman Slagman, May 28, 2009
    #1
    1. Advertising

  2. 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/
     
    Martin Honnen, May 28, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Reiche
    Replies:
    3
    Views:
    10,869
    Michael Reiche
    Feb 5, 2004
  2. bearclaws
    Replies:
    3
    Views:
    587
    bearclaws
    Mar 10, 2005
  3. Replies:
    18
    Views:
    2,769
    Joseph Kesselman
    Oct 4, 2006
  4. Zachary  Turner
    Replies:
    1
    Views:
    533
    Bjoern Hoehrmann
    May 30, 2007
  5. nirman longjam
    Replies:
    1
    Views:
    269
    Miki Tebeka
    Nov 24, 2011
Loading...

Share This Page