how to remove specifically matched elements using xslt 1.0 - xslt

html:
<div class="ls-col" id="ls-row-2-col-2">
<div class="ls-col-body" id="ls-gen79493564-ls-col-body">
<div class="ls-row" id="ls-row-2-col-2-row-1">
<div class="ls-fxr" id="ls-gen79493565-ls-fxr">
<div class="ls-area" id="ls-row-2-col-2-row-1-area-1">
<div class="ls-area-body" id="ls-gen79493566-ls-area-body">
<div class="grid-container">
<div class="grid-100">
<div class="intro_content slide_content grid-40">
<div class="current"></div>
<div class="next"></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
expected:
<div class="ls-col">
<div class="ls-row">
<div class="ls-area">
<div class="grid-container">
<div class="grid-100">
<div class="intro_content slide_content grid-40">
<div class="current"></div>
<div class="next"></div>
</div>
</div>
</div>
</div>
</div>
</div>
I want to remove div's which contain following classes
ls-col-body
ls-area-body
ls-row-body
ls-fxr
just remove all the id attributes which starts with ls- not the div just id attribute.
I tried matching this way. but not understanding how to remove it
<xsl:template match="div[contains(#class,'ls-col-body')]">
<xsl:apply-templates select="container"/> <!-- some template -->
</xsl:template>

Try this XSLT:
First template is used to copy nodes and attributes. second one to remove divs with particular values and third to omit id attributes starting with ls-
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="div[#class='ls-col-body' or #class='ls-area-body' or #class='ls-row-body']">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="#id[starts-with(.,'ls-')]"/>
</xsl:stylesheet>

Related

xslt Rename node's attribute value depending on ancestor's attribute value

I have a large number of html files like the following:
<html>
<body>
<div class="a">aaa
<div class="a1">a1a1a1</div>
<div class="a2">a2a2a2</div>
<div class="a2">a3a3a3</div>
</div>
<div class="v u">bbb
<div class="x">xxx</div>
<div class="y">yyy</div>
<div class="z">z1z1z1
<div class="w">www1</div>
<div class="w">www2</div>
<div class="w">www3</div>
</div>
<div class="z">z2z2z2
<div class="w">www4</div>
<div class="w">www5</div>
<div class="w">www6</div>
</div>
</div>
<div class="i">
<div class="j">jjj</div>
<div class="x">
<div class="k">
<div class="w">www7</div>
<div class="w">www8</div>
</div>
</div>
</div>
</body>
</html>
The classes x, y, z, and w can occur any number of times and with any div throughout the html.
There is one and only one div class="v u"
I would like to:
Rename the classes x, y, z, and w to b1, b2, b3, and b4 respectively if and only if any ancestor of that node is div class="v u"
Rename div class="v u" to div class="b"
The result would then be:
<html>
<body>
<div class="a">aaa
<div class="a1">a1a1a1</div>
<div class="a2">a2a2a2</div>
<div class="a2">a3a3a3</div>
</div>
<div class="b">bbb
<div class="b1">xxx</div>
<div class="b2">yyy</div>
<div class="b3">z1z1z1
<div class="b4">www1</div>
<div class="b4">www2</div>
<div class="b4">www3</div>
</div>
<div class="b3">z2z2z2
<div class="b4">www4</div>
<div class="b4">www5</div>
<div class="b4">www6</div>
</div>
</div>
<div class="i">
<div class="j">jjj</div>
<div class="x">
<div class="k">
<div class="w">www7</div>
<div class="w">www8</div>
</div>
</div>
</div>
</body>
</html>
I have tried the following xslt, that doesn't give me the expected result:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="#*|node()" >
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="div[#class='v u']">
<div class="b">
<xsl:copy-of select="node()" />
</div>
</xsl:template>
<xsl:template match="div[#class='v u']/div[#class='x']">
<div class="b1">
<xsl:copy-of select="node()" />
</div>
</xsl:template>
<xsl:template match="div[#class='v u']/div[#class='y']">
<div class="b2">
<xsl:copy-of select="node()" />
</div>
</xsl:template>
<xsl:template match="div[#class='v u']/div[#class='z']">
<div class="b3">
<xsl:copy-of select="node()" />
</div>
</xsl:template>
<xsl:template match="div[#class='v u']/div[#class='z']/div[#class='w']">
<div class="b4">
<xsl:copy-of select="node()" />
</div>
</xsl:template>
</xsl:stylesheet>
I think I understand why it is not giving me the correct result, however I do not seem to find the correct solution.
Instead of all those <xsl:copy-of select="node()" /> you need to use <xsl:apply-templates/> or <xsl:apply-templates select="node()"/> to keep the template based processing alive.

XSLT for dynamic tree

You can see my xml patern sample below. Every manuel has chapter tags, chapter tags may have subchapter and checklist tags. There is no static depth, it may change document to document, so I couldnt write the dynamic recursive xslt code to generate Html. Does anyone knows, how can I achive this.
<manuel name="Test">
<chapter name="00">
<subchapter name="00.01">
<checklist name="00.01.01">
<summary>abc</summary>
</checklist>
</subchapter>
<subchapter name="00.02">
<checklist name="00.02.01">
<summary>def</summary>
</checklist>
<subchapter name="00.02.02">
<checklist name="00.02.02.01">
<summary>xyz</summary>
</checklist>
</subchapter>
</subchapter>
<checklist name="00.03">
<summary>ZZZZ</summary>
</checklist>
</chapter>
</manuel>
For this sample, I suppose this result. I can set the css style, its not important now. The problem is generating the structure.
<div class="cssChapter"> 00</div>
<div class="cssSubChapter"> 00.01 </div>
<div class="cssCheckList"> 00.01.01 </div>
<div class="cssSummary"> abc </div>
<div class="cssSubChapter"> 00.02 </div>
<div class="cssCheckList"> 00.02.01 </div>
<div class="cssSummary"> def </div>
<div class="cssSubChapter"> 00.02.02 </div>
<div class="cssCheckList"> 00.02.02.01 </div>
<div class="cssSummary"> xyz </div>
<div class="cssCheckList"> 00.03 </div>
<div class="cssSummary"> ZZZZ</div>
Use a simple apply-templates:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes"/>
<xsl:template match="manuel">
<xsl:apply-templates select=".//*"/>
</xsl:template>
<xsl:template match="chapter">
<div class="cssChapter">
<xsl:value-of select="#name"/>
</div>
</xsl:template>
<xsl:template match="subchapter">
<div class="cssSubChapter">
<xsl:value-of select="#name"/>
</div>
</xsl:template>
<xsl:template match="checklist">
<div class="cssChecklist">
<xsl:value-of select="#name"/>
</div>
</xsl:template>
<xsl:template match="summary">
<div class="cssSummary">
<xsl:value-of select="."/>
</div>
</xsl:template>
</xsl:transform>
Online at http://xsltransform.net/3NJ3915/1.

xslt adding constant number of siblings

I have a large number of files with a structure like the following a.html:
<html>
<body>
<div class="a">aaa
<div class="b">bbb</div>
<div class="c">ccc1
<div class="d">ddd11
<div class="e">eee11</div>
<div class="f">fff11
<div class="g">ggg111</div>
<div class="g">ggg112</div>
<div class="g">ggg113</div>
<div class="g">ggg114</div>
<div class="g">ggg115</div>
<div class="g">ggg116</div>
</div>
</div>
<div class="d">ddd12
<div class="e">eee12</div>
<div class="f">fff12
<div class="g">ggg121</div>
<div class="g">ggg122</div>
<div class="g">ggg123</div>
<div class="g">ggg124</div>
</div>
</div>
</div>
<div class="c">ccc2
<div class="d">ddd21
<div class="e">eee21</div>
<div class="f">fff21
<div class="g">ggg211</div>
<div class="g">ggg212</div>
<div class="g">ggg213</div>
<div class="g">ggg214</div>
<div class="g">ggg215</div>
</div>
</div>
</div>
...
</div>
</body>
</html>
The number of div class="c" is variable in each file (zero or more)
The number of div class="d" is variable inside each (zero or more)
The number of div class="g" is variable inside each (zero or more)
I would like to have a number of div class="c" equal to the max_c parameter in all files.
I use the following shell script to pass the max_c parameter with a value equal to 3:
#!/bin/bash
xsltproc --param max_c 3 a.xslt a.html
And I use the following a.xslt:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="max_c"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="div[#class='a']">
<xsl:copy>
<xsl:apply-templates select="div[#class='a']" />
<xsl:apply-templates select="div[#class='b']" />
<xsl:apply-templates select="div[#class='c']" />
<xsl:call-template name="AddC">
<xsl:with-param name="count" select="$max_c - count(div[#class='c'])" />
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="AddC">
<xsl:param name="count" />
<xsl:if test="$count > 0">
<div class="c">ccc
</div>
<xsl:call-template name="AddC">
<xsl:with-param name="count" select="$count - 1" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
The output is the following:
<html>
<body>
<div>
<div class="b">bbb</div>
<div class="c">ccc1
<div class="d">ddd11
<div class="e">eee11</div>
<div class="f">fff11
<div class="g">ggg111</div>
<div class="g">ggg112</div>
<div class="g">ggg113</div>
<div class="g">ggg114</div>
<div class="g">ggg115</div>
<div class="g">ggg116</div>
</div>
</div>
<div class="d">ddd12
<div class="e">eee12</div>
<div class="f">fff12
<div class="g">ggg121</div>
<div class="g">ggg122</div>
<div class="g">ggg123</div>
<div class="g">ggg124</div>
</div>
</div>
</div>
<div class="c">ccc2
<div class="d">ddd21
<div class="e">eee21</div>
<div class="f">fff21
<div class="g">ggg211</div>
<div class="g">ggg212</div>
<div class="g">ggg213</div>
<div class="g">ggg214</div>
<div class="g">ggg215</div>
</div>
</div>
</div>
<div class="c">ccc$count</div>
</div>
</body>
</html>
This is almost what I would like, with 2 exceptions:
The first div has lost its class="a" and its value aaa
The added div class="c" should have a value of ccc3, and not ccc$count
What am I doing wrong?
As usual I thank you in advance for your help.
The first div has lost its class="a" and its value aaa
To add the missing attributes and text content change the xslt:copy like the following:
<xsl:copy>
<xsl:apply-templates select="#* | div[#class='b'] | text()" />
<xsl:apply-templates select="div[#class='c']" />
The #*adds all attributes and the text() all text content.
The added div class="c" should have a value of ccc3, and not ccc$count
change the output for div c as:
<div class="c">
ccc<xsl:value-of select="$count"/>
</div>

XSLT For-Each Wrapping every nth item in a div

I have a series of nodes that are direct child nodes to a parent I want to loop over those nodes but have them wrapped in 'groups' of 4... I'm probably not wording this very clearly so this might help;
<span class="child01">#nodename</span>
<span class="child02">#nodename</span>
<span class="child03">#nodename</span>
<span class="child04">#nodename</span>
<span class="child05">#nodename</span>
<span class="child06">#nodename</span>
<span class="child07">#nodename</span>
<span class="child08">#nodename</span>
..
<span class="child32">#nodename</span>
<span class="child33">#nodename</span>
..and so on
Goal
<div class="group">
<span class="child01">#nodename</span>
<span class="child02">#nodename</span>
<span class="child03">#nodename</span>
<span class="child04">#nodename</span>
</div>
<div class="group">
<span class="child05">#nodename</span>
<span class="child06">#nodename</span>
<span class="child07">#nodename</span>
<span class="child08">#nodename</span>
</div>
<div class="group">
..
<span class="child32">#nodename</span>
</div>
<div class="group">
<span class="child33">#nodename</span>
..and so on
I have tried variations on this idea - wrapping the lot in the open and closing group tags and every fourth loop drop in a new close / open pair
<div class="group">
<xsl:for-each select="$currentPage/*">
<span>
<xsl:value-of select="#nodeName" />
</span>
<!--
=============================================================
After very 4th item
=============================================================
-->
<xsl:if test="position() mod 4 = 0">
<xsl:text></div><div class="page"></xsl:text>
</xsl:if>
</xsl:for-each>
</div>
But essentially it seems XSLT won't let me start with a closing unmatched tag
The clkoset solution I ahve found so far is a 'fix' in jquery Wrapping a div around every three divs but I would rather not rely on javascript to format the page.
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pNumCols" select="3"/>
<xsl:template match="/*">
<xsl:apply-templates select="span[position() mod $pNumCols = 1]"/>
</xsl:template>
<xsl:template match="span">
<div>
<xsl:copy-of select=
".|following-sibling::span[not(position() > $pNumCols -1)]"/>
</div>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<t>
<span class="child01">#nodename</span>
<span class="child02">#nodename</span>
<span class="child03">#nodename</span>
<span class="child04">#nodename</span>
<span class="child05">#nodename</span>
<span class="child06">#nodename</span>
<span class="child07">#nodename</span>
<span class="child08">#nodename</span> ..
<span class="child32">#nodename</span>
<span class="child33">#nodename</span>
</t>
produces the wanted result:
<div>
<span class="child01">#nodename</span>
<span class="child02">#nodename</span>
<span class="child03">#nodename</span>
</div>
<div>
<span class="child04">#nodename</span>
<span class="child05">#nodename</span>
<span class="child06">#nodename</span>
</div>
<div>
<span class="child07">#nodename</span>
<span class="child08">#nodename</span>
<span class="child32">#nodename</span>
</div>
<div>
<span class="child33">#nodename</span>
</div>
If like me you need to transform the source elements that are being divided by position, use xsl:for-each instead of xsl:copy:
<xsl:template match="span">
<ol>
<xsl:for-each select=".|following-sibling::span[not(position() > $pNumCols -1)]"/>
<li><xsl:value-of select="./text()"/></li>
</xsl:for-each>
</ol>
</xsl:template>
Faced by the same problem, that is wanting to output
<div class="container">
<div class="row">
<div class="col">...</div>
<div class="col"/>...</div>
</div>
<div class="row">
...
</div>
</div>
from a CXML (Collection XML) file (http://gallery.clipflair.net/collection/activities.cxml - the data behind the PivotViewer display at http://gallery.clipflair.net/activity)
I coined up the following, based on other suggestions here, but using "mode" attribute of "template" and "apply-templates" XSL tags instead which make it cleaner I believe:
<?xml version="1.0" encoding="UTF-8"?>
<?altova_samplexml http://gallery.clipflair.net/collection/activities.cxml?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:cxml="http://schemas.microsoft.com/collection/metadata/2009"
exclude-result-prefixes="cxml"
>
<xsl:output method="html" version="4.0" encoding="UTF-8" indent="yes"/>
<xsl:param name="COLUMNS" select="2"/>
<!-- ########################### -->
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>ClipFlair Activities</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<!-- ########################### -->
<xsl:template match="cxml:Collection">
<div class="container">
<xsl:apply-templates/>
</div>
</xsl:template>
<!-- ########################### -->
<xsl:template match="cxml:Items">
<xsl:apply-templates select="cxml:Item[position() mod $COLUMNS = 1]" mode="row"/>
</xsl:template>
<!-- ########################### -->
<xsl:template match="cxml:Item" mode="row">
<div class="row">
<div>----------</div>
<xsl:apply-templates select=".|following-sibling::cxml:Item[position() < $COLUMNS]" mode="col"/>
</div>
</xsl:template>
<xsl:template match="cxml:Item" mode="col">
<xsl:variable name="URL" select="#Href"/>
<xsl:variable name="FILENAME" select="cxml:Facets/cxml:Facet[#Name='Filename']/cxml:String/#Value"/>
<div class="col">
<xsl:value-of select="$FILENAME"/> --- <xsl:value-of select="$URL"/>
</div>
</xsl:template>
<!-- ########################### -->
<xsl:template match="*">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()|#*">
</xsl:template>
</xsl:stylesheet>
the output from the above when run in Altova XMLSpy tool (note that it uses altova_samplexml processor instruction to find the XML data) is:
2DaysInParis-OpenActivity-CapRev-FR-EN.clipflair --- http://studio.clipflair.net/?activity=2DaysInParis-OpenActivity-CapRev-FR-EN.clipflair
Abu_Dukhan-CapRev-A1-AR.clipflair --- http://studio.clipflair.net/?activity=Abu_Dukhan-CapRev-A1-AR.clipflair
----------
AFarewellToArms-RevCap-C2-EN.clipflair --- http://studio.clipflair.net/?activity=AFarewellToArms-RevCap-C2-EN.clipflair
agComhaireamhCountingRND.clipflair --- http://studio.clipflair.net/?activity=agComhaireamhCountingRND.clipflair
----------
Al-imtihan-CapRev-B1-AR.clipflair --- http://studio.clipflair.net/?activity=Al-imtihan-CapRev-B1-AR.clipflair
AlBar-Cap-B1-B2-IT.clipflair --- http://studio.clipflair.net/?activity=AlBar-Cap-B1-B2-IT.clipflair
...

XSLT replace tag by generated one

I'm quite new to XSLT.
This is the problem I'm trying to solve for hours now:
I auto-generate a table of contents for a xml document which works great so far. However I'd like to replace a placeholder tag in my source xml with that just generated toc code.
So the output should include the whole document with replaced placeholder-toc-tag with the auto-generated toc xml.
This is what I've tried:
Let's say I have my placeholderTag anywhere in the document and want to replace this/those. I thought that I could loop through all nodes by node() and check if the node name equals my placeholder tag:
<xsl:template match="node()">
<xsl:choose>
<xsl:when test="divGen">
<!-- apply other template to generate toc-->
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="node()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
However the if statement won't match like this.
edit:
Ok, here's the source document (TEI coded - TEI namespace removed):
<TEI>
<teiHeader>
<fileDesc>
<titleStmt>
<title>Title</title>
</titleStmt>
<publicationStmt>
<p>Publication information</p>
</publicationStmt>
<sourceDesc>
<p>Information about the source</p>
</sourceDesc>
</fileDesc>
</teiHeader>
<text>
<front>
<titlePage>
<byline>title page details</byline>
</titlePage>
</front>
<body>
<divGen type="toc"/>
<div type="part">
<div type="section">
<head>heading1</head>
</div>
<div type="section">
<head>heading2</head>
</div>
</div>
<div type="part">
<div type="section">
<head>heading3</head>
</div>
<div type="section">
<head>heading4</head>
</div>
<div type="section">
<head>heading5</head>
</div>
</div>
</body>
<back> </back>
</text>
I'd like to auto-generate the toc from the head values and replace the divGen tag by the auto-produced toc code. However please notice that the divGen tag can be anywhere in the document but not outside of body.
Any ideas?
Chris
Good question, +1.
Here is a complete transformation (with mock TOC generation to be replaced by real one) that shows how to do this:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vTOC">
<xsl:apply-templates mode="TOC"/>
<mockTOC>
<xsl:comment>The real TOC generated here</xsl:comment>
</mockTOC>
</xsl:variable>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="divGen[#type='toc']">
<xsl:copy-of select="$vTOC"/>
</xsl:template>
<xsl:template match="text()" mode="TOC"/>
</xsl:stylesheet>
when this transformation is applied on the provided XML document:
<TEI>
<teiHeader>
<fileDesc>
<titleStmt>
<title>Title</title>
</titleStmt>
<publicationStmt>
<p>Publication information</p>
</publicationStmt>
<sourceDesc>
<p>Information about the source</p>
</sourceDesc>
</fileDesc>
</teiHeader>
<text>
<front>
<titlePage>
<byline>title page details</byline>
</titlePage>
</front>
<body>
<divGen type="toc"/>
<div type="part">
<div type="section">
<head>heading1</head>
</div>
<div type="section">
<head>heading2</head>
</div>
</div>
<div type="part">
<div type="section">
<head>heading3</head>
</div>
<div type="section">
<head>heading4</head>
</div>
<div type="section">
<head>heading5</head>
</div>
</div>
</body>
<back>
</back>
</text>
</TEI>
the correct, wanted output is produced, in which any occurences of <divGen type="toc"/> are replaced by the generated TOC:
<TEI>
<teiHeader>
<fileDesc>
<titleStmt>
<title>Title</title>
</titleStmt>
<publicationStmt>
<p>Publication information</p>
</publicationStmt>
<sourceDesc>
<p>Information about the source</p>
</sourceDesc>
</fileDesc>
</teiHeader>
<text>
<front>
<titlePage>
<byline>title page details</byline>
</titlePage>
</front>
<body>
<mockTOC><!--The real TOC generated here--></mockTOC>
<div type="part">
<div type="section">
<head>heading1</head>
</div>
<div type="section">
<head>heading2</head>
</div>
</div>
<div type="part">
<div type="section">
<head>heading3</head>
</div>
<div type="section">
<head>heading4</head>
</div>
<div type="section">
<head>heading5</head>
</div>
</div>
</body>
<back/>
</text>
</TEI>
Explanation: Use of modes to pre-generate the TOC in a variable, then overriding the identity rule for any TOC placeholder.
Im guessing somewhere u have a template like
<xsl:template match="/">
<xsl:apply-templates select="*"/>
</xsl:template>
then u want the template to only match your placeholderTag
then the others will default to your other nodes!
<xsl:template match="placeholderTag">
<!-- applying generate toc thing-->
</xsl:template>
<xsl:template match="node()">
<xsl:copy-of select="node()"/>
</xsl:template>