How do I copy an entire element but remove only some of the children?
I want to copy the div#about but I want to remove the table elements from it.
Input HTML:
<html>
<body>
<div class="content-header">
<h1>Title</h1>
</div>
<div id="about">
<h1>About</h1>
<table>...</table>
<p>Bla bla bla</p>
<table>...</table>
<p>The end</p>
</div>
</body>
</html>
XSLT:
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<div class="article">
<h1>
<xsl:value select="//div[#class='content-header']/h1/text()"/>
</h1>
<div>
<xsl:copy-of select="//div[#id='about']"/>
<!-- Here should render the entire div#about without the tables -->
</div>
</div>
</xsl:template>
<xsl:output method="html" indent="yes" omit-xml-declaration="yes"/>
</xsl:transform>
First add the identity template to your XSLT
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
(Or, if you were using XSLT 3.0, you could use <xsl:mode on-no-match="shallow-copy"/> instead)
Then add another template to ignore the table elements
<xsl:template match="div[#id='about']/table" />
And finally, replace your xsl:copy-of with xsl:apply-templates to allow these templates to be matched, thus ensuring the table elements do not get copied.
Try this XSLT
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="div[#id='about']/table" />
<xsl:template match="/">
<div class="article">
<h1>
<xsl:value-of select="//div[#class='content-header']/h1/text()"/>
</h1>
<div>
<xsl:apply-templates select="//div[#id='about']"/>
</div>
</div>
</xsl:template>
</xsl:transform>
Related
I'm trying to write a simple XHTML to Simple Docbook translator (the input XHTML is a limited subset so it should be doable).
I have this:
<?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" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" standalone="no"/>
<!--
<xsl:strip-space elements="*"/>
-->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<!-- skip implicit tags-->
<xsl:template match="/html/body"><xsl:apply-templates/></xsl:template>
<xsl:template match="/html"><xsl:apply-templates/></xsl:template>
<!-- paragraphs to sections converter -->
<xsl:template match="h2">
<xsl:variable name="title" select="generate-id(.)"/>
<section>
<title><xsl:apply-templates select="text()"/></title>
<xsl:for-each select="following-sibling::*[generate-id(preceding-sibling::h2[1]) = $title and not(self::h2)]">
<xsl:apply-templates/>
</xsl:for-each>
</section>
</xsl:template>
<xsl:template match="p">
<para><xsl:apply-templates select="*|text()"/></para>
</xsl:template>
<xsl:template match="p[preceding-sibling::h2]"/>
<xsl:template match="ul">
<itemizedlist><xsl:apply-templates select="li"/></itemizedlist>
</xsl:template>
<xsl:template match="ul[preceding-sibling::h2]"/>
<xsl:template match="ol">
<orderedlist><xsl:apply-templates select="li"/></orderedlist>
</xsl:template>
<xsl:template match="ol[preceding-sibling::h2]"/>
<xsl:template match="li">
<listitem><para><xsl:apply-templates select="*|text()"/></para></listitem>
</xsl:template>
</xsl:stylesheet>
For this input
<html>
<body>
<p>First paragraph</p>
<p>Second paragraph</p>
<h2>First title</h2>
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
<h2>Second title</h2>
<p>First paragraph</p>
<ul>
<li>A list item</li>
<li>Another list item</li>
</ul>
<p>Second paragraph</p>
</body>
</html>
I expect this output
<para>First paragraph</para>
<para>Second paragraph</para>
<section>
<title>First title</title>
<para>First paragraph</para>
<para>Second paragraph</para>
<para>Third paragraph</para>
</section>
<section>
<title>Second title</title>
<para>First paragraph</para>
<itemizedlist>
<listitem>A list item</listitem>
<listitem>Another list item</listitem>
</itemizedlist>
<para>Second paragraph</para>
</section>
But I get
<para>First paragraph</para>
<para>Second paragraph</para>
<section><title>First title</title>First paragraphSecond paragraphThird paragraph</section>
<section><title>Second title</title>First paragraph
<listitem><para>A list item</para></listitem>
<listitem><para>Another list item</para></listitem>
Second paragraph</section>
For some reason, the template for my paragraphs and lists is not being applied. I'm guessing because the templates matching are the empty ones, but I need those to prevent duplicate tags outside section.
How can I make this work? TIA.
Use
<xsl:for-each select="following-sibling::*[generate-id(preceding-sibling::h2[1]) = $title and not(self::h2)]">
<xsl:apply-templates select="."/>
</xsl:for-each>
or simply
<xsl:apply-templates select="following-sibling::*[generate-id(preceding-sibling::h2[1]) = $title and not(self::h2)]"/>
to process those elements you want to wrap into a section. But there will be a collision with your other templates so perhaps using a mode helps for the processing:
<xsl:template match="p" mode="wrapped">
<para><xsl:apply-templates select="*|text()"/></para>
</xsl:template>
<xsl:template match="p[preceding-sibling::h2]"/>
<xsl:template match="ul" mode="wrapped">
<itemizedlist><xsl:apply-templates select="li"/></itemizedlist>
</xsl:template>
<xsl:template match="ul[preceding-sibling::h2]"/>
<xsl:template match="ol" mode="wrapped">
<orderedlist><xsl:apply-templates select="li"/></orderedlist>
</xsl:template>
<xsl:template match="ol[preceding-sibling::h2]"/>
<xsl:template match="li" mode="wrapped">
<listitem><para><xsl:apply-templates select="*|text()"/></para></listitem>
</xsl:template>
How can I apply a different font in an XSL for the XML looking like this
<text> sometext <citation> somecitation </citation> sometext </text>
so the citation content should be in a different font as just text.
so this is the part of my XML
<text>She flipped her bed over and found invisible alligators all over her room. <citation> What's going on here? <citation> she demanded. </text>
I wrote a code <xsl:template match="text">
<p>
<xsl:value-of select="text()"/>
<q>
<xsl:value-of select="citation/text()"/>
</q>
</p>
(q stands for italic in CSS)
What I want to get :
She flipped her bed over and found invisible alligators all over her room.
"What's going on here?" she demanded.
What I get for now : She flipped her bed over and found invisible alligators all over her room.she demanded. "What's going on here?"
how can I proceed to get correct result?
Thank you!
First, the default styling applied by the browser should be quite sufficient - provided you use the correct HTML tags.
Given the following input:
XML
<text>She flipped her bed over and found invisible alligators all over her room. <citation> What's going on here? </citation> she demanded. </text>
the following stylesheet:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="text">
<p>
<xsl:apply-templates/>
</p>
</xsl:template>
<xsl:template match="citation">
<cite>
<xsl:apply-templates/>
</cite>
</xsl:template>
</xsl:stylesheet>
will return:
<html>
<body>
<p>She flipped her bed over and found invisible alligators all over her room. <cite> What's going on here? </cite> she demanded.
</p>
</body>
</html>
which most any browser will render as:
If you don't like or don't trust the browser defaults, you can specify your own style, for example:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="text">
<p>
<xsl:apply-templates/>
</p>
</xsl:template>
<xsl:template match="citation">
<span style="font-style: italic;">
<xsl:apply-templates/>
</span>
</xsl:template>
</xsl:stylesheet>
Or:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<html>
<head>
<style>
q {
font-style: italic;;
}
</style>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="text">
<p>
<xsl:apply-templates/>
</p>
</xsl:template>
<xsl:template match="citation">
<q>
<xsl:apply-templates/>
</q>
</xsl:template>
</xsl:stylesheet>
I want to wrap the headers and paragraph inside the section tags. Section tag ends when the next header arises.
Input:
<body>
<h2>text text</h2>
<p> some text </p>
<p> some text </p>
<h2> text text </h2>
<p> some text </p>
<p> some text </p>
<p> some text </p>
</body>
Output:
<body>
<section>
<h2>text text</h2>
<p> some text </p>
<p> some text </p>
</section>
<section>
<h2> text text </h2>
<p> some text </p>
<p> some text </p>
<p> some text </p>
</section>
</body>
Like mentioned in the comments, this is a grouping question.
If you're using XSLT 2.0, you can use xsl:for-each-group/#group-starting-with...
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/body">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:for-each-group select="*" group-starting-with="h2">
<section>
<xsl:copy-of select="current-group()"/>
</section>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
If you're stuck with XSLT 1.0, you can use an xsl:key based on a generated id of the h2...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="sectElems" match="/body/*[not(self::h2)]"
use="generate-id(preceding-sibling::h2[1])"/>
<xsl:template match="/body">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:apply-templates select="h2"/>
</xsl:copy>
</xsl:template>
<xsl:template match="h2">
<xsl:variable name="id">
<xsl:value-of select="generate-id()"/>
</xsl:variable>
<section>
<xsl:copy-of select=".|key('sectElems',$id)"/>
</section>
</xsl:template>
</xsl:stylesheet>
Both of these stylesheets produce the same output.
I have a project where I need a to convert an xml to html using an XSLT and have a css for it as well. I just can't seem to get the CSS to affect the document, any help would be great!
The CSS is just align center because I can easily see if it's working or not, once I know it's working I can edit it so it's how i want it to be. But basically the XSL works fine but I can't get the CSS to affect the xml
XML:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="BookDescription.xsl" type="text/xsl"?>
<?xml-stylesheet type="text/css" href="book_details.css"?>
<!--ORACLE BOOK-->
<Book>
<bookID>0-07-882122-3</bookID>
<bookTitle>Oracle: A Beginner's Guide</bookTitle>
<bookCategory>database</bookCategory>
<bookDescription>A beginner's guide to the complex and powerful Oracle database management system. Teaches you how to set up, query and manage your database, including principles of database design, how to manage data, generate reports and tune the system for optimal performance.</bookDescription>
<bookPrice>30.00</bookPrice>
<bookAuthor>Michael Abbey</bookAuthor>
<bookImage>images/Oracle.JPG</bookImage>
<bookInfo>Oracle.xml</bookInfo>
</Book>
XSL
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="4.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="Book">
<h3>
<xsl:value-of select="bookTitle"/>
</h3>
<img alt="">
<xsl:attribute name="src"><xsl:value-of select="bookImage"/></xsl:attribute>
</img>
<div>
<xsl:text>ISBN: </xsl:text>
<xsl:value-of select="bookID"/>
</div>
<div>
<xsl:text>Author: </xsl:text>
<xsl:value-of select="bookAuthor"/>
</div>
<div>
<xsl:text>Category: </xsl:text>
<xsl:value-of select="bookCategory"/>
</div>
<div>
<xsl:text>Description: </xsl:text>
<xsl:value-of select="bookDescription"/>
</div>
<div>
<xsl:text>Price: </xsl:text>
<xsl:value-of select="bookPrice"/>
</div>
</xsl:template>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="Book"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
CSS
div{
text-align: center;
}
bookID{
text-align: center;
}
You have to add a link to your CSS file:
<xsl:template match="/">
<html>
<head>
<link rel="stylesheet" href="path/to/your/css/file.css">
</head>
<body>
<xsl:apply-templates select="Book"/>
</body>
</html>
</xsl:template>
Or:
<xsl:template match="/">
<html>
<head>
<style>
/* Your CSS here */
</style>
</head>
<body>
<xsl:apply-templates select="Book"/>
</body>
</html>
</xsl:template>
I'm trying to insert some HTML at a given point. The XML file has a content node, which inside that has actual HTML. For exmaple here is the content section of the XML:
-----------------
<content>
<h2>Header</h2>
<p>some link</p>
<p>some link1</p>
<p>some link2</p>
</content>
-----------------
I need to insert a link after the header but before the first link, inside its own p tag. A little rusty with XSLT, any help is appreciated!
Given this source:
<html>
<head/>
<body>
<content>
<h2>Header</h2>
<p>some link</p>
<p>some link1</p>
<p>some link2</p>
</content>
</body>
</html>
This stylesheet will do what you want to do:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/html/body/content/h2">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
<p>your new link</p>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/content">
<xsl:copy-of select="h2"/>
foo
<xsl:copy-of select="p"/>
</xsl:template>
</xsl:stylesheet>