« Newer Snippets
Older Snippets »
5 total  XML / RSS feed 

Adding Multiple Attributes to an element

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:element name="paragraph">
<xsl:attribute name="priority">medium</xsl:attribute>
<xsl:attribute name="date">2003-09-23</xsl:attribute>
<xsl:attribute name="doc:style" namespace="http://www.example.com/documents">
classic</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

Adding Attributes

<xsl:template match="/">
<xsl:element name="paragraph">
<xsl:attribute name="priority">medium</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>

Adding ID Values to Elements

<!-- addids1.xsl: Add ID values to all elements that don't have them.

The following XSLT stylesheet copies a source document to a result tree, taking advantage of XSLT 1.0's generate-id() function along the way to create unique IDs for every element that doesn't already have an id attribute. (Technically, there's a small chance that one of the created ones will be the same as an existing one, but it's a very small chance.) The first template rule copies all elements, adding the id value if it's not already there, and the second template rule copies all the other node types.
-->



<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:template match="*">
<xsl:copy>
<xsl:if test="not(@id)">
<xsl:attribute name="id">
<xsl:value-of select="generate-id()"/>
</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

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

</xsl:stylesheet>

Stripping Empty Paragraphs

<!--
check for empty p, pre, and h4 elements, the match condition would be "p|pre|h4". If you want it to look for empty para elements in a DocBook document, set the match condition to "para".
-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<!-- Only copy non-empty p elements. -->
<xsl:template match="p">
<xsl:if test="normalize-space(.)">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:if>
</xsl:template>

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

<@/xsl:stylesheet>

XSLT XHTML Template

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

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">

<xsl:output method="xml"
indent="yes"
omit-xml-declaration="yes"
doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" />

<!--XHTML document outline-->
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<style type="text/css" media="screen,projection">
@import '/css/main.css';
</style>

<title>test1</title>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<!-- /XHTML document outline-->

<!--Templates -->
<xsl:template match="">

</xsl:template>


<xsl:template match="">

</xsl:template>



</xsl:stylesheet>
« Newer Snippets
Older Snippets »
5 total  XML / RSS feed