Tyrone wrote:
I'm doing transformations to some XHTML documents. Basically I need to only
remove a few attributes and elements from them and leave the document largely
intact beyond that.
You want to do an identity transformation:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
This is a recursive copy-through of all nodes from source to result.
Add to it templates that match the elements you want to change. These
templates will match at a higher priority than the template above.
For example, if you want to add a style="font-size: 85%" attribute
to every 'p' element:
<xsl:template match="p">
<xsl:copy>
<xsl:attribute name="style">font-size: 85%</xsl:attribute>
<xsl:apply-templates select="@*[name() != 'style']|node()"/>
</xsl:copy>
</xsl:template>
The apply-templates allows the recursive copy-through to continue
down into the contents of the <p>...</p>. The predicate on the @*
prevents any existing 'style' attribute from overwriting the new
one you just created.
I'm having problems navigating to the html elment and other elements when I
leave the Doctype and namespace at the top of the xhtml doc. I assume I'm
missing something in my xpath syntax
Yes. In your stylesheet (preferably at the top), bind a prefix to
the namespace that the XHTML elements are in. Use this prefix when
referring to element names in XPath expressions.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
<xsl:template match="xhtml:p">
...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Note that when you feed this to an XML parser, the DTD is most likely
being read from the w3.org site. I would use a local DTD if possible.
I've been looking for some kind of XPATH tool that will let me see how a
document tree is represented in XPATH syntax. If anyone knows of such a tool
could you post a link?
Various XML editors have tree visualization modes, but you could use the
stylesheets Jeni Tennison and I developed to produce an ASCII art view
that you might find appealing. Download them from http://skew.org/xml/
(ASCII XML Tree Viewer or Pretty XML Tree Viewer... try both)
Mike
--
Mike J. Brown | http://skew.org/~mike/resume/
Denver, CO, USA | http://skew.org/xml/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list