none
Splitting XML tag into multiple tags by | seperator

    Question

  • I am in the process of attempting to validating some xml data for loading into a system.

    My xml is

    <?xml version="1.0" encoding="utf-8"?>
    <Materials>
      <Work>ABC</Work>
      <Comment>5004|Cylinders 920 Kg|EA|1000|310 Kg</Comment>
    </Materials>

    And I need the Comment field split out into seperate tags by the | (Pipe) symbol

    <?xml version="1.0" encoding="utf-8"?>
    <Materials>
      <Work>ABC</Work>
      <Comment>5004|Cylinders 920 Kg|EA|1000|310 Kg</Comment>
      <a>5004</a>
      <b>Cylinders 920 Kg</b>
      <c>EA</c>
      <d>1000</d>
      <e>310 Kg</e>
    </Materials>

    The Tag names don't need to be anything in particular, but will need to be the same everytime I run it.

    And the amount of pipes in the comments field will always be the same.

    I have been trying to work out the xslt syntax, but so far I haven't been able to quite get it right.

    Regards,

    Michael

    Edit:

    My Current attempt at the xslt is below, this has used some information I have managed to find across the net, but not quite enough to get me the correct tag names

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
     <xsl:template match="Materials">
      <xsl:element name="Materials">
       <xsl:copy-of select="*"/>
       <xsl:apply-templates select="Comment"/>
      </xsl:element>
     </xsl:template>
      <xsl:template match="Comment" name="split">
       <xsl:param name="pText" select="."/>
      <xsl:if test="string-length($pText)">
       <xsl:if test="not($pText=.)">
        <a><xsl:value-of select="substring-before(concat($pText,'|'),'|')"/></a>
       </xsl:if>
       <xsl:call-template name="split">
        <xsl:with-param name="pText" select="substring-after($pText, '|')"/>
       </xsl:call-template>
      </xsl:if>
      </xsl:template>
    </xsl:stylesheet>


    • Edited by Mr.MSanders Friday, October 04, 2013 5:33 AM Added current XSLT
    Friday, October 04, 2013 4:56 AM

Answers

  • <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    
      <xsl:template match="Materials">
        <xsl:element name="Materials">
          <xsl:copy-of select="*"/>
          <xsl:apply-templates select="Comment"/>
        </xsl:element>
      </xsl:template>
    
      <xsl:template match="Comment">
        <xsl:param name="a" select="."/>
        <xsl:param name="b" select="substring-after(., '|')"/>
        <xsl:param name="c" select="substring-after($b, '|')"/>
        <xsl:param name="d" select="substring-after($c, '|')"/>
        <xsl:param name="e" select="substring-after($d, '|')"/>
    
        <a><xsl:value-of select="substring-before($a, '|')"/></a>
        <b><xsl:value-of select="substring-before($b, '|')"/></b>
        <c><xsl:value-of select="substring-before($c, '|')"/></c>
        <d><xsl:value-of select="substring-before($d, '|')"/></d>
        <e><xsl:value-of select="$e"/></e>
      </xsl:template>
    
    </xsl:stylesheet>

    • Marked as answer by Mr.MSanders Sunday, October 06, 2013 10:26 PM
    Saturday, October 05, 2013 12:55 PM

All replies

  • <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    
      <xsl:template match="Materials">
        <xsl:element name="Materials">
          <xsl:copy-of select="*"/>
          <xsl:apply-templates select="Comment"/>
        </xsl:element>
      </xsl:template>
    
      <xsl:template match="Comment">
        <xsl:param name="a" select="."/>
        <xsl:param name="b" select="substring-after(., '|')"/>
        <xsl:param name="c" select="substring-after($b, '|')"/>
        <xsl:param name="d" select="substring-after($c, '|')"/>
        <xsl:param name="e" select="substring-after($d, '|')"/>
    
        <a><xsl:value-of select="substring-before($a, '|')"/></a>
        <b><xsl:value-of select="substring-before($b, '|')"/></b>
        <c><xsl:value-of select="substring-before($c, '|')"/></c>
        <d><xsl:value-of select="substring-before($d, '|')"/></d>
        <e><xsl:value-of select="$e"/></e>
      </xsl:template>
    
    </xsl:stylesheet>

    • Marked as answer by Mr.MSanders Sunday, October 06, 2013 10:26 PM
    Saturday, October 05, 2013 12:55 PM
  • Thank you,

    I knew there would be a simple way of doing it, I just couldn't think of it.

    Sunday, October 06, 2013 10:27 PM