XML/Input: Difference between revisions

From Rosetta Code
< XML
Content added Content deleted
(added J)
Line 44: Line 44:
}
}
</actionscript>
</actionscript>

=={{header|J}}==
J's system includes XML processing libraries -- this task is best addressed with the XSLT library:

load 'xml/xslt'
XSL xslt XML
April
Bob
Chad
Dave
Emily

with the appropriate definitions of <tt>XSL</tt> and <tt>XML</tt>:
XML =: noun define
<Students>
<Student Name="April" />
<Student Name="Bob" />
<Student Name="Chad" />
<Student Name="Dave" />
<Student Name="Emily" />
</Students>
)
XSL =: noun define
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:h="http://www.w3.org/1999/xhtml">
<xsl:output method="text" encoding="UTF-8" media-type="text/plain" />
<xsl:template match="/">
<xsl:apply-templates select="/Students/Student" />
</xsl:template>
<xsl:template match="/Students/Student">
<xsl:value-of select="@Name" />
<!-- Platform-independent line breaks (instead of &#10; or something) -->
<xsl:text><![CDATA[
]]></xsl:text>
</xsl:template>
</xsl:stylesheet>
)


=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==

Revision as of 19:49, 23 January 2009

Task
XML/Input
You are encouraged to solve this task according to the task description, using any language you may know.

Given the below XML fragment, extract the list of names using whatever means desired. If the only viable method is to use XPath, refer the reader to the task XML_and_XPath.

<Students>
  <Student Name="April" />
  <Student Name="Bob" />
  <Student Name="Chad" />
  <Student Name="Dave" />
  <Student Name="Emily" />
</Students>

Expected Output

April
Bob
Chad
Dave
Emily

ActionScript

<actionscript> package {

   import flash.display.Sprite;
   public class XMLReading extends Sprite
   {
       public function XMLReading()
       {
           var xml:XML = <Students>
                           <Student Name="April" />
                           <Student Name="Bob" />
                           <Student Name="Chad" />
                           <Student Name="Dave" />
                           <Student Name="Emily" />
                         </Students>;
           for each(var node:XML in xml..Student)
           {
               trace(node.@Name);
           }
       }
   }

} </actionscript>

J

J's system includes XML processing libraries -- this task is best addressed with the XSLT library:

   load 'xml/xslt'

   XSL xslt XML
April
Bob
Chad
Dave
Emily

with the appropriate definitions of XSL and XML:

XML =: noun define
<Students>
  <Student Name="April" />
  <Student Name="Bob" />
  <Student Name="Chad" />
  <Student Name="Dave" />
  <Student Name="Emily" />
</Students>
)

XSL =: noun define
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:h="http://www.w3.org/1999/xhtml">
<xsl:output method="text" encoding="UTF-8" media-type="text/plain" />
<xsl:template match="/">
	<xsl:apply-templates select="/Students/Student" />
</xsl:template>

<xsl:template match="/Students/Student">
    <xsl:value-of select="@Name" />

    <xsl:text><![CDATA[
 ]]></xsl:text>
 </xsl:template>
 </xsl:stylesheet>
)

Visual Basic .NET

       Dim xml = <Students>
                     <Student Name="April"/>
                     <Student Name="Bob"/>
                     <Student Name="Chad"/>
                     <Student Name="Dave"/>
                     <Student Name="Emily"/>
                 </Students>

       Dim names = (From node In xml...<Student> Select node.@Name).ToArray

       For Each name In names
           Console.WriteLine(name)
       Next

OCaml

# #directory "+site-lib/xml-light" (* or maybe just "+xml-light" *) ;;
# #load "xml-light.cma" ;;

# let x = Xml.parse_string "
  <Students>
    <Student Name=\"April\" />
    <Student Name=\"Bob\" />
    <Student Name=\"Chad\" />
    <Student Name=\"Dave\" />
    <Student Name=\"Emily\" />
  </Students>"
  in
  Xml.iter (function
    (Xml.Element ("Student", [("Name", name)], [])) -> print_endline name
  |  _ -> ()) x
  ;;
April
Bob
Chad
Dave
Emily
- : unit = ()