0

I need help with transforming XML file to text file. Firstly the assignment was transforming XML file to text file using xsl template. Now XML file has element ID1 that is a string with "," delimiter. I need to parse that string and create a row for each value parsed from that string. Other attributes should remain the same. Is it doable?

Input file:

<?xml version="1.0" encoding="UTF-8"?>
<LoadData>
  <value>
    <File1>
      <Time>2020/05/26 09:24:22</Time>
      <Documentcs>
        <Documentc id="09004e21802fcc5e">
          <ID1>ID_0002, ID_0001</ID1>
          <ID2>myP_001</ID2>
          <ID3>SOP</ID3>
          <ID4>MAY-26-2020  09:18:11</ID4>
          <ID5>30</ID5>
          <ID6>Days</ID6>
          <ID7>Event</ID7>
          <ID8>JUN-25-2020  09:18:11</ID8>
          <ID9>N</ID9>
          <EOF>!##!</EOF>
        </Documentc>
      </Documentcs>
    </File1>
  </value>
</LoadData>

XSL template:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" encoding="UTF-8" indent="no" />
    <xsl:variable name="newLine">
        <xsl:text>&#10;</xsl:text>
    </xsl:variable>
    <!--main template-->
    <xsl:template match="/LoadData/value/CurriculumItemConnectorFeed">
        <xsl:text>ID1 | ID2 | ID3 | ID4 | ID5 | ID6 | ID7 | ID8 | ID9 | !##!</xsl:text>
        <xsl:value-of select="$newLine" />
<!--        <xsl:value-of select="Time" />
        <xsl:value-of select="$newLine" />
        <xsl:value-of select="count(Documents/Document)" />
        <xsl:value-of select="$newLine" /> -->
        <xsl:apply-templates select="Documentcs/Documentc" mode="feed" />
        <!-- <xsl:text>!##!</xsl:text> -->
    </xsl:template>
    <!--document template-->
    <xsl:template match="Documentcs/Documentc" mode="feed">
        <xsl:for-each select="*">
            <xsl:if test="position() > 1"><xsl:text>|</xsl:text>
            </xsl:if>
            <xsl:value-of select="translate(.,'|','_')" />
        </xsl:for-each>
        <xsl:value-of select="$newLine" />
    </xsl:template>
</xsl:stylesheet>

Current output file:

ID1 | ID2 | ID3 | ID4 | ID5 | ID6 | ID7 | ID8 | ID9 | !##!
ID_0002, ID_0001|myP_001|SOP|MAY-26-2020  09:18:11|30|Days|Event|JUN-25-2020  09:18:11|N|!##!

Desired output file:

ID1 | ID2 | ID3 | ID4 | ID5 | ID6 | ID7 | ID8 | ID9 | !##!
CURR_ID_0001|myP_001|SOP|MAY-26-2020  09:18:11|30|Days|Event|JUN-25-2020  09:18:11|N|!##!
CURR_ID_0002|myP_001|SOP|MAY-26-2020  09:18:11|30|Days|Event|JUN-25-2020  09:18:11|N|!##!
Hrky86
  • 1
  • 2
  • Can you use an XSLT 2.0 processor? If not, which XSLT 1.0 processor will you use? – michael.hor257k May 26 '20 at 11:34
  • javax.xml.transform We use standard java system library. Currently we cannot use an XSLT 2.0 processor. – Hrky86 May 26 '20 at 11:53
  • Closing as a duplicate. Search for "xslt comma-separated" for other solutions. Note, however, if you're using Java then sticking to XSLT 1.0 for this kind of problem is sheer masochism. It's unreasonable to ask for assistance using obsolete technologies when there's no technical obstacle to upgrading. – Michael Kay May 26 '20 at 12:20
  • Check if your processor supports the EXSLT `str:split()` or `str:tokenize()` extension functions. – michael.hor257k May 26 '20 at 14:20
  • It is a legacy that is hard to get rid of. "xslt comma-separated" search doesn't help with my issue. I can separate values with a comma delimiter. I need to create a row with each of that separated value and with ID2-ID9 elements copied from documentc id = "09004e21802fcc5e". @michael.hor257k It supports EXSLT extension functions. I just tested with your example: https://stackoverflow.com/questions/25168475/using-tokenize-within-a-stylesheet-used-in-a-browser I get this as result: some Ha comma Ha delimited Ha string Ha – Hrky86 May 26 '20 at 18:36
  • If you can use `str:split()` then create a variable with all the common values, then create a row for each token and append the variable to the token. If you can't make it work, edit your question and add your attempt. -- P.S. You are wrong about the other thread not helping. You can do the same thing (with just a bit more work) using a recursive named template to tokenize the comma-separate input. – michael.hor257k May 26 '20 at 19:48

0 Answers0