Friday, 25 March 2016

XML : xslt transform to combine child nodes into a string

I have some xml that looks something like this:

  <List A>      <Item          name = "Name1"          ID = "ID1"          shoesize = "10">         <AddressList>             <Enum>City1</Enum>             <Enum>City2</Enum>         </AddressList>      </Item>      <Item          name = "Name2"          ID = "ID2"          shoesize = "13">         <AddressList>             <Enum>City3</Enum>         </AddressList>      </Item>  </List A>  <List B>      ...  </List B>    

I need a transform that selects only the Items in List A and produces a single line of pipe-separated text per item, but also flattens out the addresses into a semicolon separated list like this:

  Name1|ID1|City1;City2  Name2|ID2|City3    

This is what I have done so far:

  <?xml version="1.0"?>  <xsl:stylesheet version="1.0"      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    <xsl:template match="Item"><xsl:value-of select="@name"/>|<xsl:value-of select="@ID"/>  <xsl:text>&#xa;</xsl:text>  </xsl:template>    <xsl:strip-space elements="*"/>  <xsl:output omit-xml-declaration="yes" indent="yes" />  </xsl:stylesheet>    

I don't know how to loop over the AddressList and put it into a single field. I'd also like to ignore list B.

I'm sure this is basic stuff but I don't know xslt and everything I've read seems to assume a certain knowledge... plus I can't find a quick beginners guide that gives me enough to work this out.

Thanks in advance for any help.

No comments:

Post a Comment