Link to home
Start Free TrialLog in
Avatar of Swn-Y-Mor
Swn-Y-Mor

asked on

Display XML in HTML Table and remove underscores

I have an xml file that I want to display in a HTML table. The XML file contains underscores in place of whitespace. I am trying to display 'Room Area 1' as opposed to 'Room_Area_1'

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MyData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <PercentageComplete>
            <RoomArea>Activities</RoomArea>
            <Percent>51.22</Percent>
      </PercentageComplete>
      <PercentageComplete>
            <RoomArea>Room_Area_1</RoomArea>
            <Percent>62.82</Percent>
      </PercentageComplete>
      <PercentageComplete>
            <RoomArea>Room_Area_2</RoomArea>
            <Percent>82.67</Percent>
      </PercentageComplete>
      <PercentageComplete>
            <RoomArea>Room_Area_3</RoomArea>
            <Percent>74.12</Percent>
      </PercentageComplete>
      <PercentageComplete>
            <RoomArea>Room_Area_4</RoomArea>
            <Percent>70.79</Percent>
      </PercentageComplete>
      <PercentageComplete>
            <RoomArea>Room_Area_5</RoomArea>
            <Percent>77.78</Percent>
      </PercentageComplete>
      <PercentageComplete>
            <RoomArea>Room_Area_6</RoomArea>
            <Percent>68.6</Percent>
      </PercentageComplete>
      <PercentageComplete>
            <RoomArea>Room_Area_7</RoomArea>
            <Percent>87.32</Percent>
      </PercentageComplete>
      <PercentageComplete>
            <RoomArea>Room_Area_8</RoomArea>
            <Percent>49.44</Percent>
      </PercentageComplete>
      <PercentageComplete>
            <RoomArea>Room_Area_9</RoomArea>
            <Percent>68.48</Percent>
      </PercentageComplete>
      <PercentageComplete>
            <RoomArea>Room_Area_10</RoomArea>
            <Percent>41.58</Percent>
      </PercentageComplete>
</MyData>

Can someone please show me the correct html to output this table without displaying the underscores such as 'Room_Area_1' into 'Room Area 1'
Avatar of Swn-Y-Mor
Swn-Y-Mor

ASKER

forgot to mention - 2 column table required with first column heading as 'RoomArea' and second column heading as 'Percent'
ASKER CERTIFIED SOLUTION
Avatar of Rob
Rob
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You don't really indicate which technology you are using

In XSLT you would use translate(., '_', ' ') as in the below example
(the below example is a full working XSLT for getting your XML into a HTML table)

In JavaScript you would use roomarea.replace("_", " ")
given that roomarea is the string value of your element content
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:template match="MyData">
        <html>
            <body>
                <table border="1">
                    <xsl:apply-templates select="PercentageComplete"/>
                </table>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="PercentageComplete">
        <tr>
            <td><xsl:value-of select="translate(RoomArea, '_', ' ')"/></td>
            <td><xsl:value-of select="Percent"/></td>
        </tr>
    </xsl:template>
 </xsl:stylesheet>

Open in new window

The replace Function only replaces the first occurrence unless regular expressions are used
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial