Home > other >  Passing the last node with data to the output XML (XSLT question)
Passing the last node with data to the output XML (XSLT question)

Time:02-11

I have the following xml:

<peci:Address>
    <peci:Country>seq0</peci:Country>
</peci:Address>
<peci:Address>
    <peci:Country>seq2</peci:Country>
</peci:Address>
<peci:Address>
    <peci:Country></peci:Country>
</peci:Address>

Using the following XSLT line (sheet and template data is extensive so I've decided not to include it):

xsl:value-of select="peci:Address/peci:Country"/>

Gets me the following data:

<cell name="Country">seq0 seq2 </cell>

Whereas I would like:

<cell name="Country">seq2</cell>

[last()] would produce <cell name="Country"/> so that is not what I'm looking for.

It would have to be scalable because there may be more or less than 3 Country nodes, some of which may be blank. I simply want the last instance of them all.

Eg:

<peci:Address>
    <peci:Country>seq0</peci:Country>
</peci:Address>
<peci:Address>
    <peci:Country>seq2</peci:Country>
</peci:Address>
<peci:Address>
    <peci:Country></peci:Country>
</peci:Address>
<peci:Address>
    <peci:Country>seq3</peci:Country>
</peci:Address>

Should be:

<cell name="Country">seq3</cell>

If you could provide some ideas, that would be much appreciated.

Cheers!

CodePudding user response:

Use a predicate (peci:Address/peci:Country[normalize-space()])[last()].

  • Related