Hello I have an input:

<Records>
  <Record>
    <Row A="UOBSGD2" B="50000"/>
  </Record>
  <Record>
    <Row A="UOBSGD1" B="75050"/>
  </Record>
  <Record>
    <Row A="DBSSGD2" B="55800"/>
  </Record>
  <Record>
    <Row A="DBSSGD1" B="14239"/>
  </Record>
  <Record>
    <Row A="OCBCSGD1" B="55510"/>
  </Record>
  <Record>
    <Row A="OCBCSGD2" B="31714"/>
  </Record>
  <Record>
    <Row A="UOBUSD1" B="36471.1"/>
  </Record>
  <Record>
    <Row A="UOBUSD2" B="41941.765"/>
  </Record>
  <Record>
    <Row A="UOBUSD3" B="48233.02975"/>
  </Record>
  <Record>
    <Row A="DBSUSD1" B="55467.9842125"/>
  </Record>
  <Record>
    <Row A="OCBCUSD1" B="14239"/>
  </Record>
  <Record>
    <Row A="OCBCUSD2" B="16374.85"/>
  </Record>
  <Record>
    <Row A="OCBCEUR1" B="45857"/>
  </Record>
  <Record>
    <Row A="OCBCEUR2" B="19239"/>
  </Record>
  <Record>
    <Row A="DBSEUR1" B="33510"/>
  </Record>
</Records>

I need to change this to Output XML:

<chart>
  <set label='UOBSGD2' value='50000' />
  <set label='UOBSGD1' value='75050' />
  <set label='DBSSGD2' value='55800' />
  <set label='DBSSGD1' value='14239' />
  <set label='OCBCSGD1' value='55510' />
  <set label='OCBCSGD2' value='31714' />
  <set label='UOBUSD1' value='36471.1' />
  <set label='UOBUSD2' value='41941.765' />
  <set label='UOBUSD3' value='48233.03' />
  <set label='DBSUSD1' value='55467.98' />
  <set label='OCBCUSD1' value='14239' />
  <set label='OCBCUSD2' value='16374.85' />
  <set label='OCBCEUR1' value='45857' />
  <set label='OCBCEUR2' value='19239' />
  <set label='DBSEUR1' value='33510' />
</chart>

By using XSLT.. Can someone please help me?
Many Thanks!!!

Dani AI

Generated

Two quick notes based on 's input and the replies from : the transform should create the wrapper element (chart or graph) in the template that matches the input root, and attribute names must be exact (a common typo seen in the thread was alue instead of value). Using either a short for-each or a single template that emits the wrapper and iterates Row elements is the simplest approach.

A compact XSLT 1.0 pattern that produces the requested <chart>/<set> structure (no extra templates required) can use attribute-value-templates; this keeps the XSLT concise and avoids repeated xsl:attribute calls:

<xsl:template match="/">
  <chart>
    <xsl:for-each select="//Row">
      <!-- keep original text of @B -->
      <set label="{@A}" value="{@B}"/>
      <!-- or, to force two decimal places: -->
      <!-- <set label="{@A}" value="{format-number(number(@B),'0.00')}"/> -->
    </xsl:for-each>
  </chart>
</xsl:template>

For the categories/graph case, ensure the template that emits <graph> also writes a <categories> element around the loop that creates <category> entries. To convert a date string like "1/2/2014" into ISO "2014-01-02" in XSLT 1.0, extract pieces with substring-before/after, pad month/day with format-number(number(...),'00'), then concat(year,'-',month,'-',day). If the input is already ISO and the output unexpectedly shows ISO, verify whether a previous template or formatting step changed the value; put the categories wrapper in the same template that emits the graph so child nodes land under it.

Troubleshooting checklist: match the correct input node in the top-level template (match="/"), use xsl:output for indentation/omit-declaration as needed, test incrementally (emit chart first, then add set attributes), and watch for typos in attribute names. 's apply-templates approach is valid; the same effect can be achieved with the compact for-each/AVT pattern above depending on preference.

Recommended Answers

All 8 Replies

This is my XLST

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml"/>
  <xsl:template match="Chart">

    <Chart>
      <xsl:apply-templates select="EmployeeDetails" />
    </Chart>

  </xsl:template>


  <xsl:template match="Chart">

      <xsl:attribute name="A">
        <xsl:value-of select="@label" />
      </xsl:attribute>
      <xsl:attribute name="B">
        <xsl:value-of select="@value" />
      </xsl:attribute>



  </xsl:template>


</xsl:stylesheet>

I'm not sure if this is correct... Most likely it isn't... but I'm new at it... so please help me...

Thanks!

I do not know where the node employee details coming from.
is the best way to solve
each node, and then select the value and or attribute
specify
do not think like a programmed but for node node
respond and evaluate

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" omit-xml-declaration="yes" method="xml"/>
    <xsl:template match="/">
        <chart>
            <xsl:apply-templates select="Records"/>
        </chart>
    </xsl:template>
    <xsl:template match="Records">
        <xsl:apply-templates select="Record"/>
    </xsl:template>
    <xsl:template match="Record">
        <xsl:apply-templates select="Row"/>
    </xsl:template>
    <xsl:template match="Row">
        <set>
            <xsl:attribute name="label">
                <xsl:value-of select="@A"/>
            </xsl:attribute>
            <xsl:attribute name="alue">
                <xsl:value-of select="@B"/>
            </xsl:attribute>
        </set>
    </xsl:template>
</xsl:stylesheet>

Thanks so much!!
I have one more question to ask you...

This is my desired XML:

<graph> 
 <categories>
    <category name='1/1/2014' />
    <category name='1/2/2014' />
    <category name='1/3/2014' />
  </categories>
</graph>

I used this XSLT:

 <graph>
   <xsl:apply-templates select="//row">
    <xsl:sort select="@line" />
   </xsl:apply-templates>
 </graph>

</xsl:template>

  <xsl:template match="//row">
    <xsl:element name="category">

      <xsl:attribute name="name">
        <xsl:value-of select="@Date" />
      </xsl:attribute>

    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

But the result of my new XML is:

<graph>
<category name="2014-01-01"/>
<category name="2014-01-02"/>
<category name="2014-01-03"/>
</graph>

Can you please tell me why am I not getting the:

  <categories>
    <category name='1/1/2014' />
    <category name='1/2/2014' />
    <category name='1/3/2014' />
    <category name='1/4/2014' />
  </categories>  

like i'm suppose to be having please?

It can't work >.<

  <xsl:template match="categories">

  <categories>
      <xsl:element name="category">  

      <xsl:attribute name="name">
        <xsl:value-of select="@Date" />
      </xsl:attribute>

      </xsl:element>
  </categories>

  </xsl:template>

Is it like this?

  <xsl:template match="*[not(self::categories)][category]">

    <xsl:element name="category">
      <xsl:attribute name="name">
        <xsl:value-of select="@Date" ></xsl:value>
      </xsl:attribute>
    </xsl:element>

    <xsl:copy>
      <xsl:apply-templates select="@*|node()[not(self::category)]"></xsl:apply>
      <categories>
        <xsl:apply-templates select="category"></xsl:apply>
      </categories>
    </xsl:copy>

  </xsl:template>  

Please help me......

found information and cat

<xsl:variable name="mon" select="format-number(substring-before(@name,'/'),'00')"/>
<xsl:variable name="day" select="format-number(substring-before(substring-after(@name,'/'),'/'),'00')"/>
<xsl:variable name="year" select="substring-after(substring-after(@name,'/'),'/')"/>

concat with

<xsl:value-of select="concat($year,'-',$mon,'-',$day)"/>





<?xml version="1.0"?>
<Records>
    <Record>
        <Row A="UOBSGD2" B="50000" name='1/1/2014' />
    </Record>
    <Record>
        <Row A="UOBSGD1" B="75050" name='1/2/2014'/>
    </Record>
    <Record>
        <Row A="DBSSGD2" B="55800" name='1/4/2014'/>
    </Record>
    <Record>
        <Row A="DBSSGD1" B="14239" name='1/5/2014'/>
    </Record>
    <Record>
        <Row A="OCBCSGD1" B="55510" name='1/6/2014'/>
    </Record>
    <Record>
        <Row A="OCBCSGD2" B="31714" name='1/16/2014'/>
    </Record>
    <Record>
        <Row A="UOBUSD1" B="36471.1"/>
    </Record>
    <Record>
        <Row A="UOBUSD2" B="41941.765"/>
    </Record>
    <Record>
        <Row A="UOBUSD3" B="48233.02975"/>
    </Record>
    <Record>
        <Row A="DBSUSD1" B="55467.9842125"/>
    </Record>
    <Record>
        <Row A="OCBCUSD1" B="14239"/>
    </Record>
    <Record>
        <Row A="OCBCUSD2" B="16374.85"/>
    </Record>
    <Record>
        <Row A="OCBCEUR1" B="45857"/>
    </Record>
    <Record>
        <Row A="OCBCEUR2" B="19239"/>
    </Record>
    <Record>
        <Row A="DBSEUR1" B="33510"/>
    </Record>
</Records>



<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" omit-xml-declaration="yes" method="xml"/>
    <xsl:template match="/">
        <chart>
            <xsl:apply-templates select="Records"/>
        </chart>
    </xsl:template>
    <xsl:template match="Records">
        <xsl:apply-templates select="Record"/>
    </xsl:template>
    <xsl:template match="Record">
        <xsl:apply-templates select="Row"/>
    </xsl:template>
    <xsl:template match="Row">
        <xsl:variable name="mon" select="format-number(substring-before(@name,'/'),'00')"/>
        <xsl:variable name="day" select="format-number(substring-before(substring-after(@name,'/'),'/'),'00')"/>
        <xsl:variable name="year" select="substring-after(substring-after(@name,'/'),'/')"/>

        <set>
            <xsl:attribute name="label">
                <xsl:value-of select="@A"/>
            </xsl:attribute>
            <xsl:attribute name="value">
                <xsl:value-of select="@B"/>
            </xsl:attribute>
            <xsl:if test="@name">
            <xsl:attribute name="name">

                <xsl:value-of select="concat($year,'-',$mon,'-',$day)"/>
            </xsl:attribute>
            </xsl:if>
        </set>
    </xsl:template>
</xsl:stylesheet>




<chart>
  <set label="UOBSGD2" value="50000" name="2014-01-01"/>
  <set label="UOBSGD1" value="75050" name="2014-01-02"/>
  <set label="DBSSGD2" value="55800" name="2014-01-04"/>
  <set label="DBSSGD1" value="14239" name="2014-01-05"/>
  <set label="OCBCSGD1" value="55510" name="2014-01-06"/>
  <set label="OCBCSGD2" value="31714" name="2014-01-16"/>
  <set label="UOBUSD1" value="36471.1"/>
  <set label="UOBUSD2" value="41941.765"/>
  <set label="UOBUSD3" value="48233.02975"/>
  <set label="DBSUSD1" value="55467.9842125"/>
  <set label="OCBCUSD1" value="14239"/>
  <set label="OCBCUSD2" value="16374.85"/>
  <set label="OCBCEUR1" value="45857"/>
  <set label="OCBCEUR2" value="19239"/>
  <set label="DBSEUR1" value="33510"/>
</chart>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.