Attribute Value Templates

We know we can go from attributes to elements, but what about the other way around? Suppose we wanted to create a stylesheet that changed the underlying elements of a Listing into attributes? Given our original definition of a Listing

<!ELEMENT TownDescription (#PCDATA)>
<!ELEMENT Listing (ListingBroker, Type,ListPrice,Addr, YearBuilt?,Bedrooms?,Baths?,
 SpecialFeatures?, Lotsize,Description)>
<!ELEMENT ListingBroker (#PCDATA)>
<!ELEMENT Type (#PCDATA)>
<!ATTLIST Type
PropType CDATA #REQUIRED
PropType (residential|commercial|land|farm|other) "residential">
<!ELEMENT Addr (#PCDATA) >
<!ELEMENT YearBuilt (#PCDATA)>
<!ELEMENT Bedrooms (#PCDATA)>
<!ELEMENT Baths (#PCDATA)>
<!ELEMENT SpecialFeatures (#PCDATA)>
<!ELEMENT Lotsize (#PCDATA)>
<!ATTLIST Lotsize Units (acres|sq.feet) "sq.feet">

Our first inclination would be to write the following:

<xsl:template match="Listing>
    <NewListing
        ListingBroker="<xsl:value-of select='ListingBroker'"
. . .
    />
</xsl:template>

But this code violates the rule that we must output correctly formed XML and this result is malformed. We can, however, get the job done by using an attribute template and replacing the xsl:value-of with {ListingBroker}. For example

<xsl:template match="//Listing">
    <NewListing
        ListingBroker="{ ListingBroker} "
        Type="{ Type} "
    />
</xsl:template>

We can see that {ListingBroker} is just a shortcut for saying xsl:value-of select='ListingBroker'.

Copying Nodes with xsl:copy

Sometimes, we just want an exact copy of the input generated to the output. xsl:copy does just that for us, as shown in Listing 5.27. In fact, the XSLT Specification defines a transform called the identity transform that generates its input as output using xsl:copy.

Code Listing 5.27. Identity.xsl—Generate Input as Output
<?xml version="1.0"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/XSL/Transform/1.0"
    indent-result="no" default-space="strip">

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
</xsl:stylesheet>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.131.110.169