0

I am using XSLT to create CSV file from XML file.

currently my xml file looks like

<VWSRecipeFile>
<PDCScaling User="" Version="1.0" Description="PDCTesting" LastChange="41984.7673783102">
    <Values>
        <Ch1_MLC_Application_GVLPDC_PDC_Body_Sca_GrossWeightTopFlash_Set Item="Upper flash_set" Unit="kg" Type="4" Hex="00000000" Value="0"/>
        <Ch1_MLC_Application_GVLPDC_PDC_Body_Sca_GrossWeightTopFlash_Act Item="Upper flash_act" Unit="kg" Type="4" Hex="00000000" Value="0"/>
        <Ch1_MLC_Application_GVLPDC_PDC_Body_Sca_GrossWeightTopFlash_TolUpp Item="Upper flash_max" Unit="kg" Type="4" Hex="00000000" Value="0"/>            
    </Values>
</PDCScaling>
</VWSRecipeFile>

And my xslt looks like

<?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" indent="no"/>

<xsl:template match="/VWSRecipeFile">

  <xsl:for-each select="PDCScaling/Values/*">
    <xsl:value-of select="concat(@Item,';')" />
  </xsl:for-each>

  <xsl:text>&#xA;</xsl:text>

  <xsl:for-each select="PDCScaling/Values/*">
    <xsl:value-of select="concat(@Unit,';')" />
  </xsl:for-each>

  <xsl:text>&#xA;</xsl:text>

  <xsl:for-each select="PDCScaling/Values/*">
    <xsl:value-of select="concat(@Value,';')" />
  </xsl:for-each>

  <xsl:text>&#xA;</xsl:text>
</xsl:template>
</xsl:stylesheet>

Now there is change in xml file and it looks like

<VWSRecipeFile>
<PDCScaling User="" Version="1.0" Description="PDCTesting" LastChange="41984.7673783102">
    <Values>
        <Ch1_MLC_Application_GVLPDC_PDC_Body_Sca_GrossWeightTopFlash_Set Item="Upper flash_set_kg" Type="4" Hex="00000000" Value="0"/>
        <Ch1_MLC_Application_GVLPDC_PDC_Body_Sca_GrossWeightTopFlash_Act Item="Upper flash_act_kg" Type="4" Hex="00000000" Value="0"/>
        <Ch1_MLC_Application_GVLPDC_PDC_Body_Sca_GrossWeightTopFlash_TolUpp Item="Upper flash_max_kg" Type="4" Hex="00000000" Value="0"/>           
    </Values>
</PDCScaling>
</VWSRecipeFile>

What's changed is, in current file "Item" and "Unit" is merged and is given like "Item_unit". I want to produce same reults as before. For which before creating a CSV i need to break the "Item" into "Item" and "Unit" and then create the CSV. Can anyone tell me efficient way of doing it. Any help is appreciated

akshat
  • 3
  • 4
  • *"is given like "Item_unit"*" In your example, you show "it_em_unit", i.e. the _ character does not only separate between item and unit, but can also appear in item's name. Is that correct? – michael.hor257k Dec 12 '14 at 20:26
  • yes, but " _ " was there in previous "item" value also. In the new scenariao "Item" and "Unit" are clubbed/concatenated using " _ " – akshat Dec 12 '14 at 20:34
  • 1
    Well, it's not a smart change. It introduces an unnecessary complexity and makes more room for error. Anyway, Tom gave you the answer below. – michael.hor257k Dec 12 '14 at 20:45

1 Answers1

0

Take the substring-before-last function from an older answer of mine and use it like this:

<xsl:for-each select="PDCScaling/Values/*">
  <xsl:call-template name="substring-before-last">
    <xsl:param name="string1" select="@Value" />
    <xsl:param name="string2" select="'_'" />
  </xsl:call-template>
  <xsl:text>;</xsl:text>
</xsl:for-each>
Community
  • 1
  • 1
Tomalak
  • 332,285
  • 67
  • 532
  • 628