wrirting XSL to perform some operations on xml data - html

How to write xsl in the body of products.xsl that will get product name and condition with quantity > 10
products.xml:
<?xml version="1.0" encoding="iso-8859-1"?>
<products>
<product>
<name>soaps</name>
<quantity>10</quantity>
<condition>ready</condition>
</product>
<product>
<name>soaps</name>
<quantity>15</quantity>
<condition>ready</condition>
</product>
<product>
<name>soaps</name>
<quantity>20</quantity>
<condition>ready</condition>
</product>
</products>
products.xsl
<?xml version="1.0"?><!-- DWXMLSource="products.xml" -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<HTML>
<HEAD>
<TITLE> products</TITLE>
</HEAD>
<BODY>
products quantity greater than 10 : <BR/>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>

This should do the trick:
<xsl:for-each select="/products/product">
<xsl:if test="quantity > 10">
<xsl:value-of select="name" />: <xsl:value-of select="condition" /> <br/>
</xsl:if>
</xsl:for-each>

This should work: (if provided with a well-formed XML – see comment on question)
<BODY> products quantity greater than 10 : <BR/>
<xsl:apply-templates select="//product[quantity > 10]"/>
</BODY>
Combined with e.g. this template:
<xsl:template match="product">
<P>
<xsl:value-of select="name"/>
<xsl:text>: </xsl:text>
<xsl:value-of select="condition"/>
</P>
</xsl:template>
Just customize per your needs…

This transformation (no <xsl:for-each> and no conditional instructions):
<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="product[quantity > 10]">
<p>
Product: <xsl:value-of select="name"/>
contition: <xsl:value-of select="condition"/>
quantity: <xsl:value-of select="quantity"/>
</p>
</xsl:template>
<xsl:template match="product"/>
</xsl:stylesheet>
when applied on the provided XML document:
<products>
<product>
<name>soaps</name>
<quantity>10</quantity>
<condition>ready</condition>
</product>
<product>
<name>soaps</name>
<quantity>15</quantity>
<condition>ready</condition>
</product>
<product>
<name>soaps</name>
<quantity>20</quantity>
<condition>ready</condition>
</product>
</products>
produces the wanted result:
<p>
Product: soaps
contition: ready
quantity: 15</p>
<p>
Product: soaps
contition: ready
quantity: 20</p>

Related

XSLT How to check if XML Node exists?

I have XML file:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<Data>
<Records>
<Record>
<AddInfo>
<Info>
</Info>
</AddInfo>
</Record>
</Records>
</Data>
and XSL file:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Dane">
<html>
<link rel="stylesheet" type="text/css" href="report.css"></link>
<body>
<h2>Table1</h2>
<table border="1" cellspacing="0">
<tr>
<th>XXX</th>
</tr>
<xsl:for-each select="Records/Record">
<tr>
<td>
<xsl:value-of select="XXX"/>
</td>
</tr>
</xsl:for-each>
</table>
<h2>SecondTable</h2>
<table border="1" cellspacing="0">
<tr>
<th>YYY</th>
<th>ZZZ</th>
</tr>
<xsl:for-each select="Records/Record/AddInfo/Info">
<tr>
<td>
<xsl:value-of select="YYY"/>
</td>
<td>
<xsl:value-of select="ZZZ"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
And I want to make it like this: if node exists, display table with "Info" nodes, and if not, display SOME TEXT.
I've been trying
<xsl:if test="following-sibling::AddInfo">
</xsl:if>
and
<xsl:if test="AddInfo">
</xsl:if>
But it is not working.
I want it like this:
Table1
---------------------
| | | |
(condition: if inside XML will be node, I want to display second table, under Table1)
SecondTable
-------------
| | |
How I can do this?
This outputs Yep if <AddInfo> exists as immediate child of <Record>, and Nope otherwise:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="Data">
<xsl:for-each select="Records/Record">
<xsl:choose>
<xsl:when test="AddInfo">Yep</xsl:when>
<xsl:otherwise>Nope</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Note that you don't need for-each, you should let a second template match each <Record>:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="Data">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Data/Records/Record">
<xsl:choose>
<xsl:when test="AddInfo">Yep</xsl:when>
<xsl:otherwise>Nope</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
You could also avoid choose and use two independent if conditions:
<xsl:template match="Data/Records/Record">
<xsl:if test="AddInfo">Yep</xsl:if>
<xsl:if test="not(AddInfo)">Nope</xsl:if>
</xsl:template>
If you don't want to limit it to immediate children, use .//AddInfo instead.
Consider the following stylesheet:
<?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="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="Data">
<xsl:apply-templates select="Records/Record"/>
</xsl:template>
<xsl:template match="Data/Records/Record">
<table class="one"></table>
<xsl:if test="AddInfo">
<table class="two"></table>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
It outputs
<table class="one"></table>
if there's no <AddInfo> node in <Record>, and
<table class="one"></table>
<table class="two"></table>
otherwise.
You can solve this with neither using if nor choose. XML:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<Data>
<AddInfo>
<Info>This is ignored</Info>
</AddInfo>
<Records>
<Record>
<AddInfo>
<Info>One,</Info>
<Info>Two,</Info>
<Info>Three</Info>
</AddInfo>
</Record>
<Record>
<Info>Ignored as well</Info>
</Record>
<Record>
<Nested>
<AddInfo>
<Info>So is this</Info>
</AddInfo>
</Nested>
</Record>
</Records>
</Data>
XSLT:
<?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="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="Data">
<root>
<xsl:apply-templates select="Records/Record"/>
</root>
</xsl:template>
<xsl:template match="Data/Records/Record">
<xsl:copy>
<table id="one"></table>
<xsl:apply-templates select="AddInfo"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Data/Records/Record/AddInfo">
<table id="two">
<xsl:apply-templates select="Info"/>
</table>
</xsl:template>
<xsl:template match="Data/Records/Record/AddInfo/Info">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
Output:
<root>
<Record>
<table id="one" />
<table id="two">One,Two,Three</table>
</Record>
<Record>
<table id="one" />
</Record>
<Record>
<table id="one" />
</Record>
</root>
To check if node exist in xml this XSLT code works
<xsl:choose>
<xsl:when test="XMLNodeName">
<Cell ss:Index="2" ss:StyleID="s110">
<Data ss:Type="String">
<xsl:value-of select="NodeOne"/>
</Data>
</Cell>
</xsl:when>
<xsl:otherwise>
<Cell`enter code here` ss:Index="2" ss:StyleID="s110">
<Data ss:Type="String">
<xsl:value-of select="NodeTwo"/>
</Data>
</Cell>
</xsl:otherwise>
</xsl:choose>

I have a problems with xpath, i need href link and other <a> attributes

I need get some part of a tag. My XML is like that
<div class="item">
<h2>Vyzivovy poradca</h2>
...
...
<div class="watch"><span>sometihink</span></div>
</div>
And I need href attribute and "data-id" attribute. My template look like
<xsl:variable name="url" select="xhtml:h2/xhtml:a/href"/>
<xsl:variable name="job_id" select="xhtml:div[#class = 'watch']/xhtml:a/data-id"/>
<job>
<xsl:attribute name="id"><xsl:value-of select="$job_id"/></xsl:attribute>
<url name="url"><xsl:value-of select="$url"/></url>
</job>
and template for tag a is:
<xsl:template match="xhtml:a">
<xsl:copy>
<!-- can not copy href, cause it is not absolute url ! -->
<xsl:copy-of select="#align|#title|#rel|#itemprop|#itemtype|#itemscope"/>
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:apply-templates select="*|text()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()"><xsl:value-of select="normalize-space(.)"/></xsl:template>
<xsl:template match="text()[ancestor::xhtml:pre]"><xsl:value-of select="etl:regex-replace(., '(\s|\n)+', '$1', 'g')"/></xsl:template>
but it doesn't work, some ideas?
This input XML:
<div class="item">
<h2>
Vyzivovy poradca
</h2>
...
...
<div class="watch">
<a href="sth"
data-id="292931"
data-active="somethink"
data-inactive="blablalba"
data-class="monitored"
class="watchItem"
title="watching"><span>sometihink</span></a>
</div>
</div>
Given to this XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="//a[descendant::text() = 'sometihink']">
<root>
<href>
<xsl:value-of select="#href"/>
</href>
<data-id>
<xsl:value-of select="#data-id"/>
</data-id>
</root>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
Produces this output XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<href>sth</href>
<data-id>292931</data-id>
</root>
Notes:
I'm assuming that the "sometihink" content is the most unique
characteristic of the a you seek. If it's something else (such as the parent div[#class="watch"]), let me know and we can adjust.
Update per OP's comment below:
This updated XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<root>
<item-href>
<xsl:value-of select="//div[#class='item']/h2/a/#href"/>
</item-href>
<watch-data-id>
<xsl:value-of select="//div[#class='watch']/a/#data-id"/>
</watch-data-id>
</root>
</xsl:template>
</xsl:stylesheet>
Given the above input XML will yield this output XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item-href>url.html</item-href>
<watch-data-id>292931</watch-data-id>
</root>
containing the requested attribute values.

How to call template with name as a variable in XSLT?

I have the following XML document which needs to be parsed with an XSLT to HTML.
<root>
<c>
<c1>
<id>1</id>
<text>US</text>
</c1>
<c1>
<id>2</id>
<text>UK</text>
</c1>
</c>
</root>
The XSLT for converting this to HTML is given below.
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" />
<xsl:template match="root">
<html>
<xsl:for-each select="c/c1">
**<xsl:variable name="vTemplate" select="text"/>
<xsl:apply-templates select="$vTemplate[#name='text'"/>**
</xsl:for-each>
</html>
</xsl:template>
<xsl:template match="xsl:template[#name='text']" name="text">
<select>
<xsl:attribute name="id">
<xsl:value-of select="id"/>
</xsl:attribute>
</select>
</xsl:template>
</xsl:stylesheet>
I need to call a template depends up on the text field. So for the value US, one template will be executed and for UK, another will executed.
How to achieve this with a variable as a template name while calling the template? I just made a try but it gives error. Can someone help me to figure out where i made wrong?
I think it is not possible to choose name of template to be called dynamically. What could be done is xsl:choose utilization (perhaps with combination with mode attribute), like 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" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/root">
<html>
<xsl:for-each select="c/c1">
<xsl:choose>
<xsl:when test="text = 'US'">
<xsl:apply-templates select="text" mode="US"/>
</xsl:when>
<xsl:when test="text = 'UK'">
<xsl:apply-templates select="text" mode="UK"/>
</xsl:when>
<xsl:otherwise>
<xsl:comment>Something's wrong</xsl:comment>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</html>
</xsl:template>
<xsl:template match="text" mode="US">
<xsl:comment>US mode</xsl:comment>
<select>
<xsl:attribute name="id">
<xsl:value-of select="preceding-sibling::id"/>
</xsl:attribute>
</select>
</xsl:template>
<xsl:template match="text" mode="UK">
<xsl:comment>UK mode</xsl:comment>
<select>
<xsl:attribute name="id">
<xsl:value-of select="preceding-sibling::id"/>
</xsl:attribute>
</select>
</xsl:template>
</xsl:stylesheet>
Or you can use match with appropriate predicate and avoid for-each like 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" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/root">
<html>
<xsl:apply-templates select="//c1" />
</html>
</xsl:template>
<xsl:template match="c1[text = 'US']">
<xsl:comment>US mode</xsl:comment>
<select id="{id}" />
</xsl:template>
<xsl:template match="c1[text = 'UK']">
<xsl:comment>UK mode</xsl:comment>
<select id="{id}" />
</xsl:template>
</xsl:stylesheet>
The id attribute of select can be also filled by "Attribute value templates" (xpath in curly brackets) as shown in previous sample.

XSL Repeating Same XML Element/Node, Not Working Correctly

So this is what my XML looks like. It's pretty darn simple, just want to lay out a bunch of links to other XML files with different names/titles for each link of course:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="index.xsl"?>
<playToc>
<play>a_and_c.xml</play>
<play>all_well.xml</play>
<play>as_you.xml</play>
<play>com_err.xml</play>
<play>coriolan.xml</play>
<play>cymbelin.xml</play>
<name>Title 1</name>
<name>Title 2</name>
<name>Title 3</name>
<name>Title 4</name>
<name>Title 5</name>
<name>Title 6</name>
</playToc>
Pretty simple, right? And so here is my XSL:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="playToc">
<html>
<body style="text-align:center;">
<xsl:apply-templates select="play"></xsl:apply-templates>
</body>
</html>
</xsl:template>
<xsl:template match="play">
<xsl:variable name="URL">
<xsl:value-of select="."/>
</xsl:variable>
<xsl:variable name="TITLE">
<xsl:value-of select="../name"/>
</xsl:variable>
<xsl:value-of select="$TITLE"/>
<br />
</xsl:template>
</xsl:stylesheet>
And this is the output:
Title 1
Title 1
Title 1
Title 1
Title 1
Title 1
When I want this to be the output, of course:
Title 1
Title 2
Title 3
Title 4
Title 5
Title 6
Any help would be so great! Thanks a bunch!
Well the XML input is poorly structured but you can work around this by doing
<xsl:template match="play">
<xsl:variable name="pos" select="position()"/>
<a href="{.}">
<xsl:value-of select="../name[position() = $pos]"/>
</a>
<br/>
</xsl:template>
Make sure you keep the <xsl:apply-templates select="play"/> in the other template, otherwise the approach with position() won't work.
<xsl:variable name="TITLE">
<xsl:value-of select="../name"/>
</xsl:variable>
Your problem is here.
The string value of ../name is the string value of the first name child of the parent of the initial context (current) node.
What you actually want is to get the value of the name child that has the same position as the position of the current (play) node.
This complete and short 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:template match="play">
<xsl:variable name="vPos" select="position()"/>
<a href="{.}">
<xsl:value-of select="../name[$vPos]"/>
</a><br />
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
when applied on the provided source XML document:
<playToc>
<play>a_and_c.xml</play>
<play>all_well.xml</play>
<play>as_you.xml</play>
<play>com_err.xml</play>
<play>coriolan.xml</play>
<play>cymbelin.xml</play>
<name>Title 1</name>
<name>Title 2</name>
<name>Title 3</name>
<name>Title 4</name>
<name>Title 5</name>
<name>Title 6</name>
</playToc>
produces the wanted, correct result:
Title 1
<br/>
Title 2
<br/>
Title 3
<br/>
Title 4
<br/>
Title 5
<br/>
Title 6
<br/>
Do note:
There is no need to use xsl:apply-templates at all.
There is just one template.

wrirting XSL to perform some operations on xml data [duplicate]

How to write xsl in the body of products.xsl that will get product name and condition with quantity > 10
products.xml:
<?xml version="1.0" encoding="iso-8859-1"?>
<products>
<product>
<name>soaps</name>
<quantity>10</quantity>
<condition>ready</condition>
</product>
<product>
<name>soaps</name>
<quantity>15</quantity>
<condition>ready</condition>
</product>
<product>
<name>soaps</name>
<quantity>20</quantity>
<condition>ready</condition>
</product>
</products>
products.xsl
<?xml version="1.0"?><!-- DWXMLSource="products.xml" -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<HTML>
<HEAD>
<TITLE> products</TITLE>
</HEAD>
<BODY>
products quantity greater than 10 : <BR/>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
This should do the trick:
<xsl:for-each select="/products/product">
<xsl:if test="quantity > 10">
<xsl:value-of select="name" />: <xsl:value-of select="condition" /> <br/>
</xsl:if>
</xsl:for-each>
This should work: (if provided with a well-formed XML – see comment on question)
<BODY> products quantity greater than 10 : <BR/>
<xsl:apply-templates select="//product[quantity > 10]"/>
</BODY>
Combined with e.g. this template:
<xsl:template match="product">
<P>
<xsl:value-of select="name"/>
<xsl:text>: </xsl:text>
<xsl:value-of select="condition"/>
</P>
</xsl:template>
Just customize per your needs…
This transformation (no <xsl:for-each> and no conditional instructions):
<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="product[quantity > 10]">
<p>
Product: <xsl:value-of select="name"/>
contition: <xsl:value-of select="condition"/>
quantity: <xsl:value-of select="quantity"/>
</p>
</xsl:template>
<xsl:template match="product"/>
</xsl:stylesheet>
when applied on the provided XML document:
<products>
<product>
<name>soaps</name>
<quantity>10</quantity>
<condition>ready</condition>
</product>
<product>
<name>soaps</name>
<quantity>15</quantity>
<condition>ready</condition>
</product>
<product>
<name>soaps</name>
<quantity>20</quantity>
<condition>ready</condition>
</product>
</products>
produces the wanted result:
<p>
Product: soaps
contition: ready
quantity: 15</p>
<p>
Product: soaps
contition: ready
quantity: 20</p>