XmlPad problems with XPath for finding duplicate nodes - duplicates

[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]

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;

Xml output showing blank

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>

How to create xml hierarchy from string path

I have an XML file with the structure like this
<Items>
<Item ID="1">
<Folder>Organisms\Pets\Cat</Folder>
</Item>
<Item ID="2">
<Folder>Organisms\Pets\Horse</Folder>
</Item>
<Item ID="3">
<Folder>Organisms\Pets\Fish</Folder>
</Item>
<Item ID="4">
<Folder>Organisms\Pets\Dog</Folder>
</Item>
</Items>
How do I create a hierarchy structure for the folders using xslt ?? So it will look like this:
<Folder>
<Organisms>
<Pets>
<Dog/>
<Cat/>
<Horse/>
<Fish/>
</Pets>
</Organisms>
</Folder>
Thanks for reading :)
-----------------Update-------------------------------------------
<xsl:template match="Item" name="split">
<xsl:param name="pText" select="Folder"/>
<xsl:if test="string-length($pText)">
<ul>
<li>
<xsl:value-of select="substring-before(concat($pText,'\'),'\')"/>
<xsl:call-template name="split">
<xsl:with-param name="pText" select="substring-after($pText, '\')"/>
</xsl:call-template>
</li>
</ul>
</xsl:if>
</xsl:template>
I tried this and some how it works, but only problem is duplication. I don't know how to group the Organism as 1 node and Pets as another node

Windows Phone 8 How to make SpeechRecognizerUI working better

I used SpeechRecognizerUI to read user's voice input. I was expecting to get dollar amount so $200 or $20.32 or $0.43 is an example. However, the response from backend is always like "20 point 32" or "20 dot 32" or "zero point forty five. Is there a better way to use the API samrtly so that I can get "200, 20.32, 0.45"? Thanks!
You could try looking in the alternates, but aside from that, I don't see any way to control text normalization in dictation grammars.
You can load your own grammar to SpeechRecognizer as described here: Adding, loading, and preloading grammars for Windows Phone 8.
In your case will be useful SRGS grammar.
You should create grammar like this (I get this from Microsoft Speech example):
<grammar version="1.0" xml:lang="en-US" mode="voice" root="amount"
xmlns="http://www.w3.org/2001/06/grammar" tag-format="semantics/1.0">
<rule id="amount">
<item repeat="1-3">
<ruleref uri="#number"/>
</item>
<item repeat="0-1">
<ruleref uri="#dot"/>
<item>
<item repeat="0-2">
<ruleref uri="#number"/>
</item>
</rule>
<rule id="dot">
<one-of>
<item> point <tag> out = "." ; </tag> </item>
<item> dot <tag> out = "." ; </tag> </item>
</one-of>
</rule>
<rule id="number">
<one-of>
<item> one <tag> out = 1; </tag> </item>
<item> two <tag> out = 2; </tag> </item>
<item> three <tag> out = 3; </tag> </item>
<item> four <tag> out = 4; </tag> </item>
<item> five <tag> out = 5; </tag> </item>
<item> six <tag> out = 6; </tag> </item>
<item> seven <tag> out = 7; </tag> </item>
<item> eight <tag> out = 8; </tag> </item>
<item> nine <tag> out = 9; </tag> </item>
<item> ten <tag> out = 10; </tag> </item>
</one-of>
</rule>
</grammar>