Sun River
Topics about Java SE, Servlet/JSP, JDBC, MultiThread, UML, Design Pattern, CSS, JavaScript, Maven, JBoss, Tomcat, ...
posts - 78,comments - 0,trackbacks - 0

Part I. XSLT Programming

 

1)   What is the exact output of the Question1.xsl program when run against web.xml in Figure 1?

2)   What is the exact output of the Question2.xsl program when run against web.xml in Figure 1?

3)   What is the exact output of the Question3.xsl program when run against web.xml in Figure 1?

4)   What is the exact output of the Question4.xsl program when run against web.xml in Figure 1?

5)   What is the exact output of the Question5.xsl program when run against web.xml in Figure 1?

6)   What is the exact output of the Question6.xsl program when run against web.xml in Figure 1? Be careful here.

7)   What is the exact output of the Question7.xsl program when run against web.xml in Figure 1?

8)   What is the exact output of the Question8.xsl program when run against strange.xml in Figure 2?

Figure 1  web.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app

    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"

    "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">

<web-app>

  <servlet>

    <servlet-name>SOAPDemoService</servlet-name>

    <servlet-class>HttpService</servlet-class>

  </servlet>

  <servlet-mapping>

    <servlet-name>SOAPDemoService</servlet-name>

    <url-pattern>/HttpService/*</url-pattern>

  </servlet-mapping>

</web-app>

 

Question1.xsl

 

<xsl:stylesheet

            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

            version="1.0"

>

    <xsl:template match="servlet-mapping">

 

        <xsl:apply-templates select="url-pattern"/>

 

    </xsl:template>

 

    <xsl:template match = "/" >

 

       <xsl:apply-templates select="web-app/servlet-mapping"/>

 

    </xsl:template>

 

</xsl:stylesheet>

 

 

 Question2.xsl

 

<xsl:stylesheet

            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

            version="1.0"

>

    <xsl:template match="text()">

 

    </xsl:template>

 

    <xsl:template match = "url-pattern" >

 

       <xsl:value-of select="../../servlet/servlet-class"/>

 

    </xsl:template>

 

</xsl:stylesheet>

 

Question3.xsl

 

<xsl:stylesheet

            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

            version="1.0"

>

   

    <xsl:template match="/">

 

             <xsl:value-of select="//servlet-name"/>

 

    </xsl:template>

  

</xsl:stylesheet>

 

Question4.xsl

 

<xsl:stylesheet

            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

            version="1.0"

> 

    <xsl:template match="/">

 

             <xsl:apply-templates select="//servlet-name"/>

 

    </xsl:template>

 

    <xsl:template match="node()">

             <xsl:value-of select="."/>

    </xsl:template>

</xsl:stylesheet>

 

Question5.xsl

 

<xsl:stylesheet

            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

            version="1.0"

>

   

    <xsl:template match="web-app">

             <wml>

                <card id = "{servlet/servlet-name}">

                   <xsl:value-of select="servlet-mapping/url-pattern"/>

                </card>

             </wml>          

 

    </xsl:template>

  

</xsl:stylesheet>

 

 

 Question6.xsl

 

<xsl:stylesheet

            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

            version="1.0"

>

   

    <xsl:template match="servlet-class">

            

            <xsl:value-of select="preceding-sibling::*"/>  

 

    </xsl:template>

 

    <xsl:template match="servlet-name">

            

            <xsl:value-of select="following-sibling::*"/>  

 

    </xsl:template>

 

    <xsl:template match="text()">

    </xsl:template>

  

</xsl:stylesheet>

 

 

Question7.xsl

 

<xsl:stylesheet

            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

            version="1.0"

>

   

    <xsl:template match="/">

            

            <xsl:apply-templates select="//servlet-name[../url-pattern='/HttpService/*']"/>  

 

    </xsl:template>

 

    <xsl:template match="servlet-name">

         

            <xsl:value-of select = "."/>

          

    </xsl:template>

  

</xsl:stylesheet>

 

      Question8.xsl

<xsl:stylesheet

                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

                  version="1.0"

>

    <xsl:template match="b">

                  <B>The answer</B>

                 <xsl:apply-templates/>

    </xsl:template>

    <xsl:template match=”a”>

            <A>The answer is here</A>

    </xsl:template>

</xsl:stylesheet>

 

Part II Document Type Definitions and schemas

 

9)      Consider the following DTD file and the associated xml document. The document is found to be invalid according to this DTD. Make the necessary change to the DTD so that the document is parsed as valid.

 

<?xml version="1.0" encoding="utf-8"?>

<!ELEMENT Swaps (FixedFloatSwap) >

<!ELEMENT FixedFloatSwap (Notional, Fixed_Rate, NumYears, NumPayments ) >

<!ELEMENT Notional (#PCDATA) >

<!ELEMENT Fixed_Rate (#PCDATA) >

<!ELEMENT NumYears (#PCDATA) >

<!ELEMENT NumPayments (#PCDATA) >

<!ATTLIST    Swaps id ID #IMPLIED>

 

 

 

 

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE Swaps SYSTEM "FixedFloatSwap.dtd">

<Swaps>

   <FixedFloatSwap>

   

       <Notional>100</Notional>

       <Fixed_Rate>5</Fixed_Rate>

       <NumYears>3</NumYears>

       <NumPayments>6</NumPayments>

   </FixedFloatSwap>

 

 

   <FixedFloatSwap>    

       <Notional>100</Notional>

       <Fixed_Rate>5</Fixed_Rate>

       <NumYears>3</NumYears>

       <NumPayments>6</NumPayments>

   </FixedFloatSwap>

</Swaps>

 

 

 

10)  Write a DTD that can be used to validate the following document. The id field is required.

 

<?xml version="1.0" encoding="UTF-8"?>

 

<Class>

   <Student id = “1”>

      <StudentName>Amy</StudentName>

      <Qpa>3.5</Qpa>

   </Student>

   <Student id = “2”>

      <StudentName>Sue</StudentName>

      <Qpa>4.0</Qpa>

   </Student>

   <Student id = “3”>

      <StudentName>John</StudentName>

      <Qpa>3.5</Qpa>

   </Student>

   <Student id = “4”>

      <StudentName>Jim</StudentName>

      <Qpa>4.0</Qpa>

   </Student>

</Class>

 

 

 

11)  In class we discussed the representation of relations in XML documents. We also discussed why we might find the following DTD to be inadequate. Briefly describe the problems we found with this DTD.

 

<!DOCTYPE db [

                        <!ELEMENT db (r1*, r2*)>

                        <!ELEMENT r1 (a,b,c)>

                        <!ELEMENT r2 (c,d)>

                        <!ELEMENT a (#PCDATA)>

                        <!ELEMENT b (#PCDATA)>

                        <!ELEMENT c (#PCDATA)>

                        <!ELEMENT d (#PCDATA)>

]>

 

 

12) Is the following document valid or invalid? If it’s invalid then explain why.

 

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE department [

<!ELEMENT department (employee)*>

<!ELEMENT employee (name, (email | url))>

<!ATTLIST    employee id CDATA #REQUIRED>

<!ELEMENT name (#PCDATA)>

<!ELEMENT email (#PCDATA)>

<!ELEMENT url EMPTY>

<!ATTLIST   url href CDATA #REQUIRED>

]>

<department>

                        <employee id="J.D">

                                    <name>John Doe</name>

                                    <email>John.Doe@foo.com</email>

                        </employee>

                        <employee id="B.S">

                                    <name>Bob Smith</name>

                                    <email>Bob.Smith@foo.com</email>

                        </employee>

<employee id="A.M">

                                    <name>Alice Miller</name>

                                    <url href="http://www.foo.com/~amiller/"/>

                        </employee>

</department>

 

13)  The following defines MountadamRiesling as having several properties. If a program were to visit the OWL document containing this definition, how would the program learn more about this particular wine? Be as specific as possible.

       

<DryRiesling rdf:ID="MountadamRiesling">

<locatedIn rdf:resource="#SouthAustraliaRegion"/>

<hasMaker rdf:resource="#Mountadam"/>

<hasSugar rdf:resource="#Dry"/>

<hasFlavor rdf:resource="#Delicate"/>

<hasBody rdf:resource="#Medium"/>

</DryRiesling>

 

 

Part III. Java Server Pages and JDBC

 

14)  Consider the Java class MyCoolBean.java and the JSP file SomeFile.jsp. Describe what will appear on a sixth browser visit after exactly 5 visits from 5 different browsers.

 

15)  Consider the Java class MyCoolBean.java and the JSP file SomeFile2,jsp. Describe what will appear on a sixth browser visit after 5 visits from 5 different browsers.

 

16)  Consider the Java file MyCoolSQLBean.java and the JSP file SomeSQLFile.jsp. Suppose that the database CoolStocks has a customer table containing the following data:

                 

lname     fname      id     age

Jones      Tom        1       23

Smith      Sue         2       21

Bell         Alice      3       24

 

            What will the browser look like on the very first visit to SomeSQLFile.jsp.

 

17)  Suppose many users are visiting the site defined by MyCoolSQLBean.java and SomeSQLFile.jsp. These users are beginning to complain that the site is too slow. Why is it too slow? I am looking for one answer but you might provide several. You’ll get credit only if you hit on the one I am looking for.

 

Part IV. Multiple Choice

 

18) The XML Encryption standard does not replace SSL but adds:

 

(a)    client and Server authentication.

(b)   persistent and partial encryption.

(c)    document signatures.

(d)   cryptography.

(e)    TLS

 

19)  Two major uses of XSDL are 

 

(a) Compilation and Validation

(b) Validation and User Authentication

(c) Use Authentication and Code Generation

(d) Validation and Code Generation

(e) Communication and Compilation

 

 

20) Which of the following makes sense?

            (a) Use Infer.exe to create an XSLT program to generate XSDL

            (b) Use Infer.exe to create an XSDL and run JAXB

            (c) Use JAXB to generate JSP’s and XSDL

            (d) Run JAXB to create XSDL and run Infer.exe

            (e) Use RDF to generate XSDL and run Infer.exe

 

Figure 2  strange.xml

<?xml version="1.0"?>

<a>

        <e>

            <b>

               <c>1</c>

               <c>2</c>

           </b>

           <a>

                <c>3</c>

           </a>

        </e>

</a>

 

 

 

// MyCoolBean.java

 

import java.io.*;

 

public class MyCoolBean implements Serializable  {

 

       private int next;

 

       public int getNext() { return next++; }

       public void setNext(int m) { next = m; }

       public MyCoolBean() { next = 0; }

      

}

 

 

<!—SomeFile.jsp à

 

<%@ page import="MyCoolBean" %>

<jsp:useBean id = "myBean" scope = "application" class = "MyCoolBean" />

 

<html>

<body>

<h1>Helo Visitor # </h1>

 

    <jsp:getProperty name = "myBean" property ="next" />

 

</body>

</html>

 

 

<!—SomeFile2.jsp -->

 

<%@ page import="MyCoolBean" %>

<jsp:useBean id = "myBean" scope = "page" class = "MyCoolBean" />

 

<html>

<body>

<h1>Helo Visitor # </h1>

 

    <jsp:getProperty name = "myBean" property ="next" />

 

</body>

</html>

 

 

// MyCoolSQLBean.java

import java.io.*;

import java.sql.*;

 

public class MyCoolSQLBean implements Serializable  {

 

       private String result;

       public String getResult() { return result; }

      

       public MyCoolSQLBean() {

              Connection con = null;

              try {

                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

                   

                    con = DriverManager.getConnection("jdbc:odbc:CoolStocks");

                   

                    Statement statement = con.createStatement();

                   

                    ResultSet rs = statement.executeQuery("SELECT * " +

                                        "FROM customer");

 

                    result = new String();

 

                    while ( rs.next() ) {

 

                       result += "ID " + rs.getString("id");

                       result += "Name " + rs.getString("lname");

                    }

                    rs.close();

                  }

                          

                          catch (SQLException sqle) {

                                    result += sqle.toString();

                          }

                          catch (ClassNotFoundException cnfe) {

                                     result += cnfe.getMessage();

                          }

                          catch (Exception e) {

                                    result += e.getMessage();

                          }

           

          }     

      

}

 

<!-- SomeSQLFile.jsp -->

 

<%@ page import="MyCoolSQLBean" %>

<jsp:useBean id = "myBean" scope = "page" class = "MyCoolSQLBean" />

 

<html>

<body>

<h1>Helo Visitor </h1>

    <jsp:getProperty name = "myBean" property ="result" />

</body>

</html>

 

 

Exam 2

 

Q0. Describe the operation of an XSLT processor and, in particular, the order in which it processes the contents of an XML document. As part of your answer, describe what XPath is, what it is used for and

what is meant by the term context. Give three examples of XPath expressions and explain their meaning.

 

Q1 (a) Identify the problems in the following XML documents; fix the errors, and explain your answer.

 

<?xml version="1.0">

<greeting>

  Hello, Loops!

</greeting>

<forloop>

for(int i=0;i<n;i++)

{

print i;

}

</forLoop>

<whileloop>

            while(k>=min)

                        {

                        k--;

                        }

</whileloop>

<p>

  <b>What is  <i> so good 

  about </b>for & while loops.

  </i>

</p>

<h1 size>Elements are not

  case sensitive !!</H1>

 

(b) Create a valid XML document which has the following DTD (put the DTD inline in your xml document so that it can be validated) 

 

<!ELEMENT XXX (AAA? , BBB+)>

<!ELEMENT AAA (CCC? , (DDD | EEE+)+)>

<!ELEMENT BBB (CCC , DDD)>

<!ELEMENT CCC (#PCDATA)>

<!ELEMENT DDD (#PCDATA)>

<!ELEMENT EEE (#PCDATA)>

<!ENTITY  XYZ  “LettersXYZ”>

 

 (c)   Consider the following DTD (sample.dtd)

 

<!ELEMENT XXX (AAA+ , BBB+, CCC+, DDD+,EEE?)>

<!ELEMENT AAA (#PCDATA)>

<!ELEMENT BBB (#PCDATA)>

<!ELEMENT CCC (#PCDATA)>

<!ELEMENT DDD (#PCDATA)>

<!ELEMENT EEE (#PCDATA)>

<!ATTLIST AAA

      mark ID #REQUIRED>

<!ATTLIST BBB

      id ID #REQUIRED>     

<!ATTLIST CCC

      ref IDREF #REQUIRED>

<!ATTLIST DDD

      ref IDREFS #REQUIRED>     

<!ATTLIST EEE

      eee NMTOKEN #REQUIRED

      ee NMTOKENS # IMPLIED>     

  

A sample XML document which does not conform to the DTD is shown below. Identify the errors, fix them, validate it, and explain your answer.

 

<!DOCTYPE XXX SYSTEM "sample.dtd">

<XXX>

   <AAA mark="a1"/>

   <AAA mark="a2"/>  

   <BBB id="b01" />

   <CCC ref="a3" />

   <DDD ref="a1 b001 a2" />

</XXX>

<EEE eee=”23 33 24” ee=”twenty”/>

 

 

Q2. Consider the following XML document:

<?xml version="1.0"?>

<source>

<book name="psychology"/>

<book name="Geography"/>

<book name="Astronomy"/>

<book name="accoustics"/>

<book name="Trigonometry"/>

</source>

 

Using the construct:

<xsl:sort select="@name"/>

 

Write a stylesheet to output the books names in sorted order in which all the names starting with lowercase should be sorted first .

 

Are the names sorted in the given order?  If not explain the problem, and  fix it so that names are sorted in the given order. Please look at the documentation of the sorting construct at: http://www.w3.org/TR/xslt#sorting

You can test your stylesheet either by including its reference in the xml file or using JavaScript.

 

Q3. For the XML file of Q2, write  Java code based on DOM parser that implements the requirements of Q2, that is to output “title” and “price” of all books which have year element above “1989”.

Q4. What is the input to XSLT?
XML file & XSL file or Dom tree & xsl file

Answer: a hierarchical tree representing an XML document - typically DOM tree. XSLT transformation requires:
"- parser (typically DOM parser): read a document and transform it into tree repsesentation
- XSLT processor: receive “a hierarchical tree representing an XML document” as an input, transform it, and produce another hierarchical tree.

  Question 1 (Architecture)
A manufacturing company is using a legacy application that imports and exports data in comma-delimited flat files. XML will be used as the data exchange format between the manufacturing company and its suppliers and customers.

Which of the following is MOST likely to be required by this architecture?
A.   The element types and attribute names in the XML documents sent by suppliers and customers must match the names of data fields in the manufacturing company's legacy system.
B.   Before sending documents to suppliers, the comma-delimited flat files must be transformed into XML by writing a DOM application.
C.   The XML documents received from suppliers and customers must be validated and transformed into comma-delimited flat files by writing an XSLT transformation.
D.   An XSLT processor will be used to transform comma-delimited flat files data into XML before sending documents to suppliers and customers.

Select two answers.
Explanation: Choice A is incorrect. The element types and attribute names used by suppliers need not be the same as the data fields in the legacy system. The data can be mapped. XML is all about interoperability between heterogeneous applications.

Choice B is correct because the DOM is a very flexible method for constructing XML document trees from scratch. The Document interface contains factory methods for creating nodes and building a DOM tree.

Choice C is correct because the result of an XSLT transformation can be XML or non-XML data.

Choice D is not correct because the input to an XSLT transformation must be a well-formed XML document. Therefore, a comma-delimited flat file cannot be processed with XSLT.

Question 2 (Testing & Tuning)
Consider the following XSLT element and function:

<xsl:key name="chapkey" match="
chapter" use="@author"/>

key("chapkey", 'durand')

Which of the following elements will be returned by the key function?
A.   <chapter chapkey= 'durand'>
B.   <chapkey author= 'durand'>
C.   <chapter author= 'durand'>
D.   <chapter key= 'durand'>

Select one answer.
Explanation: Choice C is the correct answer. The <xsl:key> element is used to declare keys. The "name" attribute specifies the name of the key. The <xsl:key> element specifies the key of any node that matches the pattern specified in the "match" attribute. In this case, the "match" attribute is a pattern that selects <chapter> elements. Therefore, the key is applicable to <chapter> elements.

The "use" attribute is an expression specifying the value of the key. The value of the "use" attribute is an expression that returns the "author" attribute of the <chapter> element.

The key function key ("chapkey", 'durand') returns a node set containing the nodes in the same document as the context node that have a value for the key named "chapkey" equal to the string "durand". The key function will return <chapter> elements that have an "author" attribute with value "durand".

Question 3 (Information Modeling)
A DTD in a catalog.dtd file contains the following declarations:

<!ENTITY % discount 'IGNORE' >
<!ENTITY % regular 'INCLUDE' >
<![%discount;[
<!ELEMENT Item (Description?, PartNumber,
DiscountedPrice, Manufacturer+)>
]]>
<![%regular;[
<!ELEMENT Item (Description, PartNumber,
Price, Manufacturer?)>
]]>

Consider the following document type declaration in an XML document:

<!DOCTYPE Catalog SYSTEM "catalog.dtd" [
<!ENTITY % regular 'IGNORE' >
<!ENTITY % discount 'INCLUDE' >
]

Which of the following is TRUE about the structure of an <Item> element in the XML document?
A.   Inside an <Item> element, the <Manufacturer> element must occur before the <DiscountedPrice> element.
B.   An <Item> element must contain at least one <Price> child element.
C.   The first child element of an <Item> element must be a <Description> element.
D.   An <Item> element must contain at least one <Manufacturer> child element.

Select one answer.
Explanation: Choice D is the correct answer. The declarations in a conditional section are part of the DTD only if the keyword of the conditional section is INCLUDE. The declarations in a conditional section are not logically part of the DTD if the keyword of the conditional section is IGNORE.

In this case, the keywords of the two conditional sections are parameter-entity references. The parameter-entity references must be expanded to determine whether to include or ignore the conditional sections. The parameter entities "discount" and "regular" are declared in both the internal subset and the external subset of the document type declaration. Entity declarations in the internal subset take precedence over those in the external subset.

The internal subset sets the "discount" keyword to INCLUDE. The conditional section with keyword "discount" is used to validate the XML document. The <Item> element must contain the following elements in the order specified:
1.   An optional <Description> element
2.   A required <PartNumber> element
3.   A required <DiscountedPrice> element
4.   One or more <Manufacturer> elements

Question 4 (XML Processing)
Consider the following XML document:

<!DOCTYPE a [
<!ELEMENT a EMPTY>
<!ATTLIST a
b NMTOKEN #REQUIRED
c CDATA 'y'
d CDATA 'z'>
]>
<a b='x' d='1' />

Which of the following statements is TRUE about the Element interface in DOM 2?
A.   getAttributeNode("b") returns the string "x".
B.   getAttribute("c") returns the empty string.
C.   getAttribute("c") returns the string "y".
D.   An attribute "d" with value "z" immediately appears when removeAttribute("d") is called.

Select two answers.
Explanation: Choice A is not correct because getAttributeNode() returns the Attr node with the specified node name or null if there is no such attribute.

Choice B is not correct because getAttribute() returns the Attr value as a string, or the empty string if that attribute does not have a specified or default value.

Choice C is correct because in this case, the default value of c is y.

Choice D is correct. removeAttribute() removes an attribute by name. If the removed attribute is known to have a default value, an attribute immediately appears containing the default value.

Question 5 (XML Rendering)
Which of the following statements is FALSE about XSL Formatting Objects?
A.   The fo:page-sequence formatting object is used to specify how to create a sequence of pages within a document.
B.   The fo:table-and-caption flow object is used for formatting a table together with its caption.
C.   The fo:table-caption formatting object is used to contain block-level formatting objects containing the caption for the table only when using fo:table-and-caption.
D.   The fo:list-item-body formatting object contains the label and the body of an item in a list.

Select one answer.
Explanation: Choice D is the correct answer. The following formatting objects are used to format lists: fo:list-block, fo:list-item, fo:list-item-label, and fo:list-item-body. The fo:list-item formatting object contains the label and the body of the item. The fo:list-item-body formatting object contains only the body of an item in a list. The fo:list-item-label formatting object contains the content of the label of the item.

posted on 2006-11-27 08:00 Sun River 阅读(279) 评论(0)  编辑  收藏 所属分类: XML