Xml output showing blank - html

I'm new to XML i have made an XML file and an XSL file. After linking the XSL file to the XML file, the output of the XML in the browser goes blank. I could not find the error.
This is my XML:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="cupcake.xsl"?>
<cupcakes>
<item>
<name>Luscious Vanilla</name>
<flavour>Vanilla</flavour>
<colour>Brown</colour>
<energy> 100 cj </energy>
</item>
<item>
<name>Chocolate Hazelnut</name>
<flavour>chocolaty</flavour>
<colour>coffe</colour>
<energy> 100 cj </energy>
<cost> $10 </cost>
</item>
<item>
<name>Risch Red Velvet</name>
<flavour>red velvet</flavour>
<colour>red</colour>
<energy> 100 cj </energy>
<cost> $10 </cost>
</item>
<item>
<name>Classic straberry</name>
<flavour>straberry</flavour>
<colour>pink</colour>
<energy> 100 cj </energy>
<cost> $10 </cost>
</item>
<item>
<name>Lemon Drop</name>
<flavour>lemon</flavour>
<colour>yellow</colour>
<energy> 100 cj </energy>
<cost> $10 </cost>
</item>
</cupcakes>
And this is my XSL:
<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="cupcakes/item">
<div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold"><xsl:value-of select="name"/> - </span>
<xsl:value-of select="price"/>
</div>
<div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
<p>
<xsl:value-of select="cost"/>
</p>
</div>
</xsl:for-each>
</body>
</html>

Related

in xslt match decimal with in text and write after it

I have a problem. I think it is simple but I havent found it.
like below code I want to find and match multiplier factor value like "0.05" with "005" in the note value dynamically and take text "AAA" after "_ 005 _"(without spaces) and write it and others too.
I tried to use together both format number and concatenate to "concat(format-number(***))" but failed because of newby about this.
<cbc:Note>40.00 BT</cbc:Note>
<cbc:Note>17_ 2005.00</cbc:Note>
<cbc:Note>11_005_AAA</cbc:Note>
<cbc:Note>11_002_BBB</cbc:Note>
<cbc:Note>11_003_CCC</cbc:Note>
<cbc:InvoicedQuantity unitCode="CS">1.000</cbc:InvoicedQuantity>
<cbc:LineExtensionAmount currencyID="TRY">200.00</cbc:LineExtensionAmount>
<cac:AllowanceCharge>
<cbc:ChargeIndicator>false</cbc:ChargeIndicator>
<cbc:MultiplierFactorNumeric>0.03</cbc:MultiplierFactorNumeric>
<cbc:Amount currencyID="TRY">6.00</cbc:Amount>
<cbc:BaseAmount currencyID="TRY">200.00</cbc:BaseAmount>
</cac:AllowanceCharge>
<cac:AllowanceCharge>
<cbc:ChargeIndicator>false</cbc:ChargeIndicator>
<cbc:MultiplierFactorNumeric>0.05</cbc:MultiplierFactorNumeric>
<cbc:Amount currencyID="TRY">10.00</cbc:Amount>
<cbc:BaseAmount currencyID="TRY">200.00</cbc:BaseAmount>
</cac:AllowanceCharge>
current xslt block
<xsl:for-each select="./cac:AllowanceCharge/cbc:MultiplierFactorNumeric">
<br/>
<xsl:text> %</xsl:text>
<xsl:value-of select="format-number(. * 100, '###.##0,00', 'european')"/>,
</xsl:for-each>
I appreciate if you help me.
It is very difficult to understand your question.
The following minimal example is mostly based on a guess. It formats the MultiplierFactorNumeric value as a 3-digit whole number and uses it as a key to retrieve the corresponding Note value:
XML
<root>
<Note>40.00 BT</Note>
<Note>17_ 2005.00</Note>
<Note>11_005_AAA</Note>
<Note>11_002_BBB</Note>
<Note>11_003_CCC</Note>
<AllowanceCharge>
<MultiplierFactorNumeric>0.03</MultiplierFactorNumeric>
</AllowanceCharge>
<AllowanceCharge>
<MultiplierFactorNumeric>0.05</MultiplierFactorNumeric>
</AllowanceCharge>
</root>
XSLT 1.0
<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:key name="note" match="Note" use="substring-before(substring-after(., '_'), '_')" />
<xsl:template match="/root">
<result>
<xsl:for-each select="AllowanceCharge">
<item>
<factor>
<xsl:value-of select="MultiplierFactorNumeric"/>
</factor>
<value>
<xsl:value-of select="substring-after(substring-after(key('note', format-number(100*MultiplierFactorNumeric, '000')), '_'), '_')"/>
</value>
</item>
</xsl:for-each>
</result>
</xsl:template>
</xsl:stylesheet>
Result
<?xml version="1.0" encoding="UTF-8"?>
<result>
<item>
<factor>0.03</factor>
<value>CCC</value>
</item>
<item>
<factor>0.05</factor>
<value>AAA</value>
</item>
</result>

MYSQL - LOAD XML multiple repeating (deplicate) tags as one string

Problem to load multiple repeating tags "<image></image>" with data to one <images> table cell.
XML
<posts>
<item>
<id>1</id>
<type>post</type>
<url>www.url.com/1</url>
<date>2016-06-15</date>
<image>some url/1xxx.jpg</image>
<image>some url/1yyy.jpg</image>
<image>some url/1zzz.jpg</image>
</item>
<item>
<id>2</id>
<type>post</type>
<url>www.url.com/2</url>
<date>2016-06-12</date>
<image>some url/2xxx.jpg</image>
<image>some url/2yyy.jpg</image>
<image>some url/2zzz.jpg</image>
<image>some url/2www.jpg</image>
</item>
<item>
<id>3</id>
<type>post</type>
<url>www.url.com/3</url>
<date>2016-06-12</date>
<image>some url/3fff.jpg</image>
</item>
</posts>
Code
Now it loads only last <image> tag from <item>
LOAD XML local infile 'D:\\demo.xml'
REPLACE
INTO TABLE posts CHARACTER SET UTF8
ROWS IDENTIFIED BY '<item>'
(#id, #type, #url, #date, #image)
SET id=#id, type=#type, url=#url, date = str_to_date(#date, '%Y-%m'), images=#image;
How to store all duplicate <image> tags as images VARCHAR or TEXT
Consider transforming your XML with XSLT to normalize the item and images into one-to-many tables. Below uses PHP to run XSLT but most general purpose languages can run XSLT 1.0 scripts including PHP, Perl, Python, Java, C#, VB. Specifically, the transformation will break <image> tags out of <item> keeping corresponding <id> and maintain two sets of tags to upload to two MySQL database tables.
XSLT Script (save as .xsl file)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/posts">
<xsl:copy>
<xsl:apply-templates select="item"/>
<xsl:apply-templates select="item/image"/>
</xsl:copy>
</xsl:template>
<xsl:template match="item">
<xsl:copy>
<xsl:copy-of select="*[local-name()!='image']"/>
</xsl:copy>
</xsl:template>
<xsl:template match="item/image">
<images>
<itemid><xsl:value-of select="ancestor::item/id"/></itemid>
<xsl:copy-of select="."/>
</images>
</xsl:template>
</xsl:stylesheet>
PHP Script
<?php
$cd = dirname(__FILE__);
// LOAD XML AND XSL FILES
$xml = new DOMDocument('1.0', 'UTF-8');
$xml->load('Original.xml');
$xslfile = new DOMDocument('1.0', 'UTF-8');
$xslfile->load('XSLT_Script.xsl');
// TRANSFORM XML with XSLT
$proc = new XSLTProcessor;
$proc->importStyleSheet($xslfile);
$newXml = $proc->transformToXML($xml);
// SAVE TO FILE
file_put_contents('Output.xml', $newXml);
?>
Output (images contain item id)
<?xml version="1.0"?>
<posts>
<item>
<id>1</id>
<type>post</type>
<url>www.url.com/1</url>
<date>2016-06-15</date>
</item>
<item>
<id>2</id>
<type>post</type>
<url>www.url.com/2</url>
<date>2016-06-12</date>
</item>
<item>
<id>3</id>
<type>post</type>
<url>www.url.com/3</url>
<date>2016-06-12</date>
</item>
<images>
<itemid>1</itemid>
<image>some url/1xxx.jpg</image>
</images>
<images>
<itemid>1</itemid>
<image>some url/1yyy.jpg</image>
</images>
<images>
<itemid>1</itemid>
<image>some url/1zzz.jpg</image>
</images>
<images>
<itemid>2</itemid>
<image>some url/2xxx.jpg</image>
</images>
<images>
<itemid>2</itemid>
<image>some url/2yyy.jpg</image>
</images>
<images>
<itemid>2</itemid>
<image>some url/2zzz.jpg</image>
</images>
<images>
<itemid>2</itemid>
<image>some url/2www.jpg</image>
</images>
<images>
<itemid>3</itemid>
<image>some url/3fff.jpg</image>
</images>
</posts>
SQL (two tables to upload)
-- POSTS TABLE
LOAD XML local infile 'C:\\Path\\To\\Output.xml'
REPLACE
INTO TABLE posts CHARACTER SET UTF8
ROWS IDENTIFIED BY '<item>'
(#id, #type, #url, #date)
SET id=#id, type=#type, url=#url, date=str_to_date(#date, '%Y-%m');
-- IMAGES TABLE
LOAD XML local infile 'C:\\Path\\To\\Output.xml'
REPLACE
INTO TABLE images CHARACTER SET UTF8
ROWS IDENTIFIED BY '<images>'
(#itemid, #image)
SET itemid=#itemid, image=#image;

Merging two XML files while transforming them using XSL

I have two XML files which I want to transform into one HTML file using XSL.
I transform them using xsltproc first.xml transform.xsl > output.html command in Linux terminal. Values from first.xml work perfectly and transform into HTML but I cannot force second.xml to work as well. It just didn't appear in file. I know there were questions like this on StackOverflow but I still couldn't figure out what I am doing wrong. It seems like something is wrong with match = "document('effects.xml')/effects" but I don't know what exactly.
first.xml
<elements>
<listOfElements>
<element>
*some data*
</element>
<element>
*some data*
</element>
</listOfElements>
</elements>
second.xml
<effects>
<effect>
<name> NAME1 </name>
<cost> COST1 </cost>
</effect>
<effect>
<name> NAME2 </name>
<cost> COST2 </cost>
</effect>
<effect>
<name> NAME3 </name>
<cost> COST3 </cost>
</effect>
</effect>
transform.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="/">
<xsl:text disable-output-escaping='yes'><!DOCTYPE html></xsl:text>
<html>
<head>
<meta charset="UTF-8"/>
<link rel="stylesheet" type="text/css" href="styl.css"/>
</head>
<body>
<xsl:apply-templates select="elements"/>
<xsl:apply-templates select="effects"/>
</body>
</html>
</xsl:template>
<xsl:template match="elements">
<div>
THIS WORKS
</div>
</xsl:template>
<xsl:template match="document('effects.xml')/effects">
<div>
<xsl:for-each select="effects/effect">
<div>
<p><xsl:value-of select="name"/></p>
</div>
</xsl:for-each>
</div>
</xsl:template>
Use <xsl:apply-templates select="document('effects.xml')/effects"/> and then in the match="effects" and <xsl:for-each select="effect">.

XSLT with RSS feeds

I have an RSS feed that changes regularly (same format, different data) and I need to apply a transform to it to get a list of items from it using tags. The problem is, I never know what position the item with the correct tags will be in. Example: If I was generating a carousel from this for a page that is related to "theory" I'd need to pull the "items" from the list of items in the RSS that have the relevant tags, then append the class "active" to the element with the class "newsitem row item".
I realize I could do this extremely easily with jQuery -- >
$(".newsitem.row.item").first().attr("class","newsitem row item active");
But, if possible, I'd like to save a step and generate the HTML with the active class in place.
<rss xmlns:atom=" ... " version="2.0">
<channel>
<title>Latest News</title>
<link>http://...</link>
<description>Latest News</description>
<atom:link href="..." rel="self"/>
<language>en</language>
<copyright>Copyright (c) 2015</copyright>
<lastBuildDate>Sat, 25 Apr 2015 23:39:40 -0500</lastBuildDate>
<item>
<title>TITLE FOR FIRST ITEM</title>
<link>LINK FOR FIRST ITEM</link>
<description>DESCRIPTION</description>
<pubDate>Sat, 25 Apr 2015 23:39:40 -0500</pubDate>
<guid>GUID</guid>
<category>gallery</category>
<category>events</category>
<item>
<title>TITLE FOR NEXT ITEM</title>
<link>LINK FOR NEXT ITEM</link>
<description>DESCRIPTION ...</description>
<pubDate>Tue, 10 Mar 2015 20:59:12 -0500</pubDate>
<guid>GUID ...</guid>
<category>architecture</category>
<category>class acts</category>
<category>competitions</category>
<category>feature</category>
<category>global college</category>
<category>graduate work</category>
<category>health systems & design</category>
<category>honors</category>
<category>rss</category>
<category>wellness</category>
</item>
...
<item>
<title>TITLE FOR THE NTH ITEM</title>
<link>LINK FOR THE NTH ITEM</link>
<description>DESCRIPTION ...</description>
<pubDate>Tue, 10 Mar 2015 20:52:45 -0500</pubDate>
<guid>GUID ...</guid>
<category>applied creativity</category>
<category>gallery</category>
<category>coa gallery</category>
<category>graduate work</category>
<category>research</category>
<category>rss</category>
<category>technology</category>
<category>theory-philosophy</category>
<category>video</category>
<category>visualization</category>
</item>
</channel>
</rss>
My code is:
<xsl:stylesheet xmlns:xsl="..." version="1.0">
<xsl:template match="/">
<div class="row">
<div class="col-md-12 banner_image vanish">
<div class="carousel newsitems slide" data-carousel-delay="8000" id="banner-latest-85695">
<div class="carousel-inner">
<xsl:apply-templates select="//item"/>
</div>
<a class="carousel-control left" data-slide="next" href="#banner-latest-85695" title="Previous News Item"></a>
<a class="carousel-control right" data-slide="next" href="#banner-latest-85695" title="Next News Item"></a>
</div>
</div>
</div>
</xsl:template>
<xsl:template match="item|text()">
<xsl:if test="category[contains(text(),'theory')]">
<div class="newsitem row item">
<xsl:if test="position() = ?"> <!-- what position test do I use? -->
<xsl:attribute name="class">newsitem row item active</xsl:attribute>
</xsl:if>
<div class="image span">
<img>
<xsl:attribute name="src">
<xsl:value-of select="carousel"/>
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="title"/>
</xsl:attribute>
</img>
</div>
<div class="details span">
<h2>
<a>
<xsl:attribute name="href">
<xsl:value-of select="link"/>
</xsl:attribute>
<xsl:value-of select="title"/>
</a>
</h2>
<div class="publication-date">
<span>posted</span>
<xsl:value-of select="pubDate"/>
</div>
<div class="description">
<xsl:value-of select="description"/>
</div>
</div>
</div>
</xsl:if>
</xsl:template>
This works if the first item in the rss feed contains the tag I am looking for. What am I overlooking here?
** EDITED TO CLARIFY
LINKS REPLACED WITH "..."
Your question is not entirely clear. If you want to select item (or items) by the "tag" it contains, you should do something like:
XSLT 1.0
<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="/">
<div>
<xsl:apply-templates select="rss/channel/item[tag2]"/>
</div>
</xsl:template>
<xsl:template match="item">
<div>
<span>
<xsl:value-of select="value"/>
</span>
</div>
</xsl:template>
</xsl:stylesheet>
Here, we're looking for item/s with a child element named tag2. Applying this to the following test input:
<rss>
<channel>
<item>
<tag1/>
<value>100</value>
</item>
<item>
<tag2/>
<value>200</value>
</item>
<item>
<tag2/>
<value>201</value>
</item>
<item>
<tag3/>
<value>300</value>
</item>
</channel>
</rss>
produces the result:
<?xml version="1.0" encoding="UTF-8"?>
<div>
<div>
<span>200</span>
</div>
<div>
<span>201</span>
</div>
</div>

XmlPad problems with XPath for finding duplicate nodes

[EDIT] It seems that there is a bug in the XML Editor I'm using (XmlPad) that prevents the Xpath query from returning the correct results. I've tested the same query using two online tools (http://www.zrinity.com/xml/xpath/xpath.cfm and http://www.futurelab.ch/xmlkurs/xpath.en.html) and it seems to work. biziclop also commented that the query works correctly in Oxygen.
I've got this structure:
<?xml version="1.0" encoding="utf-8"?>
<root>
<itemlist>
<item>
<code>0001.0.00</code>
<category>709</category>
</item>
<item>
<code>0001.0.00</code>
<category>709</category>
</item>
<item>
<code>0002.0.00</code>
<category>708</category>
</item>
</itemlist>
<itemlist>
<item>
<code>0016.0.00</code>
<category>52</category>
</item>
<item>
<code>0016.0.00</code>
<category>52</category>
</item>
<item>
<code>0016.0.00</code>
<category>51</category>
</item>
<item>
<code>0016.0.00</code>
<category>50</category>
</item>
<item>
<code>0869.0.00</code>
<category>52</category>
</item>
<item>
<code>0869.0.00</code>
<category>51</category>
</item>
<item>
<code>0869.0.00</code>
<category>50</category>
</item>
</itemlist>
</root>
I want find all items where the previous item has the same category.
This Xpath query:
//item[category = preceding-sibling::item[1]/category]
returns the following nodes:
<item>
<code>0001.0.00</code>
<category>709</category>
</item>
<item>
<code>0016.0.00</code>
<category>52</category>
</item>
<item>
<code>0869.0.00</code>
<category>52</category>
</item>
The last item node in the result set is incorrect, because the value of the previous item's category in the input is not 52 so it should not be returned.
Is there an Xpath query that will return the results I want?
This XPath is absolutely correct. I've tested it with Saxon XSLT processor as follows:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:copy-of select="//item[category
= preceding-sibling::item[1]/category]"/>
</xsl:template>
</xsl:stylesheet>
With results:
<item>
<code>0001.0.00</code>
<category>709</category>
</item>
<item>
<code>0016.0.00</code>
<category>52</category>
</item>
You might want also try alternatives:
//item[category = ./preceding-sibling::item[1]/category]
/root/itemlist/item[category = ./preceding-sibling::item[1]/category]