Chapter 4-8 XML
What code would you add to an XML instance document to link it to the branding.css style sheet?
<?xml-stylesheet type="text/css" href="students.css" ?>
Provide the processing instruction to link an XML document to an XSLT style sheet named styles.xsl
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
What code would you add to a schema file to force all elements and all attributes to be qualified by default?
<xs:schema> elementFormDefault="qualify" attributFormDefault="qualify" </xs:schema>
Provide the code to apply a template for the location path "catalog/books"
<xsl:apply-template select="catalog/books"/>
You want to call the Date template, setting the value of the dateValue parameter within the template to the value 12/4/2017. What code would you use in your style sheet?
<xsl:apply-templates select="Date"> <xsl:with-param name="dateValue" select="12/4/2017" /> </xsl:apply-templates>
Use a choose construction that displays book titles in an h3 heading if they are fiction and an h2 heading if otherwise.
<xsl:choose> <xsl:when test="title/@category = 'Fiction'"> <h3><xsl:value-of select="title" /></h3> </xsl:when> <xsl:otherwise> <h2><xsl:value-of select="title" /></h2> </xsl:otherwise> </xsl:choose>
Provide the code to create a deep copy of the customer's element
<xsl:copy-of select="customers"/>
What code do you use to create an element named inventory that contains the value 15000?
<xsl:element name="inventory"> 15000 </xsl:element>
What code do you use to create an empty element named inventory that contains a single attribute named amount with the value 15000?
<xsl:element name="inventory"> <xsl:attribute name="amount"> 15000 </xsl:attribute> </xsl:element>
An XML document contains the root element named books, which has a single child element named book. Each book element has two child elements named title and author. Using the XSLT's for-each instruction, sort the book elements in alphabetical order based on the title.
<xsl:for-each select="/books/book"> <xsl:sort select="title" order="ascending"/> <xsl:value-of select="title"/> </xsl:for-each>
Provide code to create a custom function named taxesDue() that returns a decimal value. The function has the myAccount namespace prefix and includes a single parameter named netIncome, which stores the decimal data type. Have the function return the value of netIncome multiplied by 0.17
<xsl:function name="myAccount:taxesDue" as="xs:decimal"> <xsl:param name="netIncome" as="xs:decimal" /><xsl:sequence select="$netIncome * 0.17" /></xsl:function>
The book element has a single attribute named category. The value of category can be either fiction or nonfiction. Write an if construction that displays the book title only if it is a nonfiction book.
<xsl:if test="book/@category = 'Non-fiction'"> <xsl:value-of select="title" /> </xsl:if>
Provide the code to import the sales.xsl style sheet in the active style sheet. Where should this element be placed?
<xsl:import href="sales.xsl"/> as the first child of the stylesheet element
The agent element has a single child element named agentName that stores the name of the real estate agent. What code would you use to create a key named agentKey applied to the agent element with key values based on agentName?
<xsl:key name="agentKey"match="agent"use="agentName"/>
Provide the code to create a parameter named dateValue, setting its default value to "11/18/2017"
<xsl:param name="dateValue" select="'11/18/2017'" />
. In general, how are values passed to a global parameter?
<xsl:param name="name" select="value"
Provide the code to create a template for the property node that belongs to the propertyList mode
<xsl:template match="property"mode="propertyList">styles</xsl:template>
Provide the code to apply a template for either the "catalog/books" or "catalog/authors" node sets.
<xsl:template match="stock"> <xsl:apply-templates select="catalog/books" /> <xsl:apply-templates select="catalog/authors" /> </xsl:template>
Provide the code to display the value of the OrderDate variable.
<xsl:value-of select="$OrderDate" />
Provide the command to display the value of the context node.
<xsl:value-of select=". " />
Provide the command to display the value of the parent of the context node.
<xsl:value-of select=". ." />
Provide two ways of displaying the value of the active node
<xsl:value-of select="." /> <xsl:value-of select="current()" />
Provide the code to display the value of the ISBN attribute of the book element
<xsl:value-of select="@ISBN"/>
Provide the command to display the value of the book element nested within the catalog element
<xsl:value-of select="catalog/book" />
Provide code to display the current date in the format Weekday, Month Day, Year.
<xsl:value-of select="format-date(current-date(), '[FNn], [MNn] [D], [Y]')" />
Create a variable named OrderDate containing the text '10/14/2017'
<xsl:variable name="OrderDate" Select="10/14/2017"/>
Provide code to store the value of the location path //orderQty as a non-negative integer data type from XML Schema in a variable named Orders.
<xsl:variable name="Orders"select="//orderQty" as="xs:nonnegativeInteger"/>
Provide code to split the revisedDoc variable at every occurrence of the tab character. Store the sequence of substrings in a variable named docValues.
<xsl:variable name="docValues"select="tokenize($revisedDoc, '\t')" />
Assuming the orderDate variable contains an xs:dateTime value, provide code to create the expireDate variable containing an xs:dateTime value 1 year and 6 months after orderDate.
<xsl:variable name="expireDate"select="$orderDate + xs:yearMonthDuration('P1Y6M') />
Provide code to call the taxesDue() function from the previous question using 82147.41 as the netIncome value. Store the result in a variable named myTaxes.
<xsl:variable name="myTaxes"select="myAccount:taxesDue(82147.41)">
Tabs are indicated by the \t regular expression character. Provide the code to replace every occurrence of a comma in the text string store in variable sampleDoc with a tab, saving the revised text string in the revisedDoc variable.
<xsl:variable name="revisedDoc"select="replace($sampleDoc, ',', '\t')" />
Create a variable named taxResult that displays the text string "Tax Due" if the myTaxes variable is positive and the value "Refund" otherwise.
<xsl:variable name="taxResult"select="if($myTaxes> 0) then 'Tax Due' else 'Refund'" />
What rule would you add to a CSS style sheet to make the namespace in Question 2 the default namespace for all selectors in the style sheet?
@namespace "http://ns.doc.student"
What rule would you add to a CSS style sheet to declare a namespace with the URI http://ns.doc.student and the namespace prefix student?
@namespace student "http://ns.doc.student";
What is a key? How does a key differ from an ID?
A key is an index that creates a node-set based on the values of an attribute or child element. Keys are declared in the style sheet, not the DTD
By default, does the element sort items numerically or alphabetically?
Alphabetic
Describe the three parts of a step pattern
Axis: defines how to move through the node tree NodeTest:specifies the node for the test patern to match Predicate: Further test the nodes to see if they match a particular pattern.
If an attribute name is unqualified, what namespace is it presumed to belong to?
Default Namespace
Describe the two general ways of grouping data in XSLT 2.0.
Grouping by valuein whichall itemsfrom the population that share the same groupingkey value are grouped together and grouping in sequencein which items are evaluated inpopulation order and the beginning or ending of each group is marked by a specifiedpattern
How does importing a schema file differ from including a schema file, with respect to the namespace of the schema?
Include: You include a schema file when you want to combine schema files from the same namespace Import: The other way to combine schemas is through importing, which is used when the schemas come from different namespaces.
List and define the two types of scope for objects in a schema?
Local Scope:can only be referenced within the object in which it is defined Global: can be referenced throughout the schema document
Name one advantage of each type of scope?
Local: The definition is confined and limited to the scope of an object only in which is declared global: No need to re-write the same code again and again.
How do namespaces prevent the problem of name collisions?
Namespace helps distinguish the element of one XML vocabulary from another.
What is the difference between a parameter and a variable?
Parameters are similar to variables except that their values can be passed to them from outside their scope
What is recursion? What are the three fundamental features of any recursive function?
Recursion is the process by which a function calls itself 1.Base case stopping condition 2.Change of state case not been reached 3.function must call itself and change value each time it call itsself
What is another name for the Flat Catalog design?
Salami
What are XSLT, XPath, and XSL-FO?
XSL-FO: extensible language is used for the layout of paginated documents XSLT: extensible language is used to transform the contents of an XML document into another document format. XPATH: xml location path and is used to locate a node within an XML document.
Provide the function to import the sales.xml file under the XSLT 1.0 processor. What function would you use under XPath 2.0?
XSLT1.0 document("sales.xml") XSLT2.0 doc("sales.xml")
When do you use the mode attribute as part of a template?
between multiple templates that can be applied to the same node-set.
What code do you use to select the first book from the XML document described in Question 1? What do you use to select the last book?
books/book[1] books/book[last()]
What is a built-in template?
built-in templates that specify how the values of different nodes are displayed, by default
Express the following location path as a step pattern in long form: /property/city
child::property/child::city
Provide code to retrieve every file from the current style sheet folder that starts with the text string "data" and ends with the .xml file extension.
collection('.?select=data*.xml')
Provide an expression to combine the lastName and firstName variables in a single text string, separated by the ", " substring.
concat($lastname, ', ', $firstname)
Provide an expression to determine whether the fileName variable contains the substring ".xls"
contains($filename, "xls")
Provide the function to check whether the XML document sales.xml exists and is well-formed.
doc-available("sales.xml")
What is functional programming?
evaluation of functions and expressions, rather than on the sequential execution of commands.
Provide an expression to display the value of the votePerc variable accurate to two decimal places to the right of the decimal point.
format-number($votePerc,'#,##.00')
What XPath expression would you enter to generate an ID based on the agentName element? Assume that agent is the context node.
generate-id(agentName)
What XPath expression would you use to select nodes whose agentID attribute equals "f102"? Assume that the agentID attribute has been defined in the document's DTD
id("f102")
What is a literal result element?
is any element that is not part of the xslt vocabulary but is sent directly as a raw text.
What code would you use to create a node set using agentKey for agents named "Howard"?
key("agentKey", "Howard")
Provide an XPath expression to return the text of the customerID variable in lowercase letters
lower-case($customerID)
How do you reference an object with global scope from an imported schema?
namespace="example"schemaLocation="studentsvb.xsd" />
Provide an expression to remove leading and trailing spaces from the password variable
normalize-space($password)
What is a name collision?
occurs when the same element name from different XML vocabularies is used within a compound document.
Provide the abbreviated form of the following step pattern: child::property/ attribute::rln.
property/@rln
What location path do you use to select the context node only? Provide the extended, not the abbreviated, expression.
self::node()
In a style sheet that includes the namespace rule in Question 2, how would you modify the selector for the lastname element to indicate that it belongs to the namespace with the URI http://ns.doc.student?
stu | Lastname
Provide an expression to extract a five-character substring from the fileURL variable starting with the eighth character
substring(fileURL,8,5)
What is a named template?
template that is not matched to a node set but instead acts like a function to display a calculated value or perform an operation.
.Correct the following expression so that it doesn't result in an error: test="sales < 20000"
test="sales < 20000"
How does the implementation of the XSLT value-of element differ between XSLT 1.0 and XSLT 2.0 when applied to element nodes?
under XSLT 2.0 all of the values of the nodes are returned by thevalue-ofelement;under XSLT 1.0 only the first node value is returned.
Using the node tree diagram in Figure 5-7, if the context node is the author element node, provide the relative path to the sName element node.
./stock/sName
Provide an XPath expression to return a node set of all author elements, regardless of their location in the source document
//author
Using the node tree diagram in Figure 5-7, provide the absolute path to the author element node
/portfolio/author
