XSLT 2.0 | One Input --> Multiple stylesheets --> Multiple outputs - xslt-2.0

Setup : Saxon HE 9.6 | XSLT 2.0
main.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:import href="first.xsl" />
<xsl:import href="second.xsl" />
<xsl:template match="/">
<xsl:result-document href="output/first.xml" method="xml">
<xsl:apply-imports/> // <-- apply first.xsl
</xsl:result-document>
<xsl:result-document href="output/second.xml" method="xml">
<xsl:apply-imports/> // <-- apply second.xsl
</xsl:result-document>
</xsl:template>
</xsl:stylesheet>
first.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="/">
<feedNumberOne>
<xsl:apply-templates select="products/p[akt = '1' and export='tak']"/>
</feedNumberOne>
</xsl:template>
<xsl:template match="p">
<first>
<product id="{./code}" name="{./front_name}"/>
</first>
</xsl:template>
</xsl:stylesheet>
second.xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="/">
<feedNumberTwo>
<xsl:apply-templates select="products/p[akt = '1' and export='tak']"/>
</feedNumberTwo>
</xsl:template>
<xsl:template match="p">
<second>
<product id="{./code}" name="{./front_name}"/>
</second>
</xsl:template>
</xsl:stylesheet>
Q : How to have 1 input, many xslt stylesheets imported with many xml output files in a single transformation
input :
<?xml version="1.0" encoding="UTF-8"?>
<products>
<p>
<code>1030037</code>
<front_name>Zelmer</front_name>
<export>tak</export>
<akt>1</akt>
</p>
<p>
<code>1030811</code>
<front_name>Sony</front_name>
<export>tak</export>
<akt>0</akt>
</p>
<p>
<code>900142</code>
<front_name>Severin</front_name>
<export>tak</export>
<akt>0</akt>
</p>
<p>
<code>1030144</code>
<front_name>Ubisoft</front_name>
<export>tak</export>
<akt>1</akt>
</p>
<p>
<code>1029390</code>
<front_name>Blanco</front_name>
<export>tak</export>
<akt>1</akt>
</p>
<p>
<code>1029750</code>
<front_name>Franke</front_name>
<export>tak</export>
<akt>1</akt>
</p>
<p>
<code>1028728</code>
<front_name>WD</front_name>
<export>tak</export>
<akt>0</akt>
</p>
<p>
<code>1030916</code>
<front_name>Electrolux</front_name>
<export>tak</export>
<akt>1</akt>
</p>
<p>
<code>1031097</code>
<front_name>High Sierra</front_name>
<export>tak</export>
<akt>1</akt>
</p>
<p>
<code>1030125</code>
<front_name>Magnat</front_name>
<export>tak</export>
<akt>1</akt>
</p>
<p>
<code>1031195</code>
<front_name>Curver</front_name>
<export>tak</export>
<akt>0</akt>
</p>
<p>
<code>1031061</code>
<front_name>High Sierra</front_name>
<export>tak</export>
<akt>1</akt>
</p>
<p>
<code>1029787</code>
<front_name>Franke</front_name>
<export>tak</export>
<akt>1</akt>
</p>
<p>
<code>1031450</code>
<front_name>Spokey</front_name>
<export>tak</export>
<akt>1</akt>
</p>
<p>
<code>1029699</code>
<front_name>Franke</front_name>
<export>tak</export>
<akt>1</akt>
</p>
<p>
<code>1029130</code>
<front_name>Samsung</front_name>
<export>tak</export>
<akt>1</akt>
</p>
<p>
<code>1028754</code>
<front_name>Technaxx</front_name>
<export>tak</export>
<akt>0</akt>
</p>
<p>
<code>1028822</code>
<front_name>Netgear</front_name>
<export>tak</export>
<akt>1</akt>
</p>
<p>
<code>1030923</code>
<front_name>Electrolux</front_name>
<export>tak</export>
<akt>0</akt>
</p>
<p>
<code>1031318</code>
<front_name>Black&Decker</front_name>
<export>tak</export>
<akt>1</akt>
</p>
<p>
<code>1031625</code>
<front_name>Russell Hobbs</front_name>
<export>tak</export>
<akt>0</akt>
</p>
<p>
<code>1031664</code>
<front_name>HMS</front_name>
<export>tak</export>
<akt>1</akt>
</p>
<p>
<code>1031627</code>
<front_name>Russell Hobbs</front_name>
<export>tak</export>
<akt>0</akt>
</p>
<p>
<code>1031680</code>
<front_name>Nikwax</front_name>
<export>tak</export>
<akt>1</akt>
</p>
<p>
<code>1031899</code>
<front_name>Wiko Mobile</front_name>
<export>tak</export>
<akt>1</akt>
</p>
<p>
<code>1031758</code>
<front_name>DeLonghi</front_name>
<export>tak</export>
<akt>0</akt>
</p>
<p>
<code>1032377</code>
<front_name>MSI</front_name>
<export>nie</export>
<akt>0</akt>
</p>
<p>
<code>1031779</code>
<front_name>DeLonghi</front_name>
<export>tak</export>
<akt>1</akt>
</p>
<p>
<code>1032865</code>
<front_name>Samsung</front_name>
<export>tak</export>
<akt>1</akt>
</p>
<p>
<code>1031848</code>
<front_name>American Tourister</front_name>
<export>tak</export>
<akt>1</akt>
</p>
<p>
<code>1032617</code>
<front_name>AMD</front_name>
<export>nie</export>
<akt>1</akt>
</p>
<p>
<code>1031923</code>
<front_name>Dyson</front_name>
<export>tak</export>
<akt>0</akt>
</p>
<p>
<code>1032998</code>
<front_name>Pioneer</front_name>
<export>tak</export>
<akt>1</akt>
</p>
<p>
<code>1032002</code>
<front_name>Vivanco</front_name>
<export>tak</export>
<akt>0</akt>
</p>
<p>
<code>1032104</code>
<front_name>Sony</front_name>
<export>tak</export>
<akt>1</akt>
</p>
<p>
<code>1032358</code>
<front_name>Adidas</front_name>
<export>tak</export>
<akt>1</akt>
</p>
<p>
<code>1032741</code>
<front_name>Samsonite</front_name>
<export>tak</export>
<akt>0</akt>
</p>
<p>
<code>1032928</code>
<front_name>SanDisk</front_name>
<export>tak</export>
<akt>0</akt>
</p>
<p>
<code>1030230</code>
<front_name>Printe</front_name>
<export>tak</export>
<akt>1</akt>
</p>
<p>
<code>1032287</code>
<front_name>Netis</front_name>
<export>tak</export>
<akt>0</akt>
</p>
<p>
<code>1031984</code>
<front_name>Krusell</front_name>
<export>tak</export>
<akt>0</akt>
</p>
<p>
<code>1029857</code>
<front_name>Franke</front_name>
<export>tak</export>
<akt>1</akt>
</p>
<p>
<code>1030681</code>
<front_name>Printe</front_name>
<export>tak</export>
<akt>0</akt>
</p>
<p>
<code>1032886</code>
<front_name>Alcatel</front_name>
<export>tak</export>
<akt>1</akt>
</p>
<p>
<code>1032688</code>
<front_name>HP</front_name>
<export>tak</export>
<akt>0</akt>
</p>
<p>
<code>1028743</code>
<front_name>Acer</front_name>
<export>tak</export>
<akt>0</akt>
</p>
</products>

As #MartinHonnen says, the classic solution is to use modes.
XSLT 3.0 introduces the default-mode attribute on xsl:stylesheet which makes this a bit easier.
EDIT #Piotr Dajlido - SOLUTION BELOW
Note the usage of mode attribute in the first.xsl
main.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:import href="first.xsl" />
<xsl:import href="second.xsl" />
<xsl:output indent="yes"/>
<xsl:template match="/">
<xsl:result-document href="output/first.xml" method="xml">
<xsl:call-template name="first">
</xsl:call-template>
</xsl:result-document>
<xsl:result-document href="output/second.xml" method="xml">
<xsl:call-template name="second"/>
</xsl:result-document>
</xsl:template>
</xsl:stylesheet>
first.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="/" name="first">
<feedNumberOne>
<xsl:apply-templates select="products/p[akt = '1' and export='tak']" mode="first"/>
</feedNumberOne>
</xsl:template>
<xsl:template match="p" mode="first">
<first>
<product id="{./code}" name="{./front_name}"/>
</first>
</xsl:template>
</xsl:stylesheet>

Related

trying to iterate 3d array | undefined method `each' for nil:NilClass

what i'm making wrong?
The first 7 lines from the Array i can iterate but when i'm trying to open the another dimension than i receive an error.
My Code
require 'rest-client'
class ApiController < ApplicationController
def listings
url = 'https://api.coinmarketcap.com/v2/ticker/'
response = RestClient.get(url)
#jsonData = JSON.parse(response)
end
end
api_controller works
<h1>LISTING </h1>
<% #jsonData["data"].each do |coin| %>
<%coin.each do |id| %>
Name: <%= id["name"] %> <br />
Symbol: <% id["symbol"] %><br />
Website_slug: <% id["website_slug"] %><br />
Rank: <% id["rank"] %><br />
Circulating_supply: <% id["circulating_supply"] %><br />
total_supply: <% id["total_supply"] %><br />
max_supply: <% id["max_supply"] %><br />
till here it works
begin here it not works
<%id["quotes"].each do |quotes| %>
<%quotes.each do |usd| %>
Price: <% usd["price"] %><br />
Volume24h: <%= usd["volume_24h"] %><br />
Market_cap: <%= usd["market_cap"] %><br />
Change_1he: <%= usd["pricpercent_change_1he"] %><br />
Change_24h: <%= usd["percent_change_24h"] %><br />
Change_7d: <%= usd["percent_change_7d"] %><br />
<%end%>
<%end%>
<%end%>
<%end%>
{"1"=>{"id"=>1, "name"=>"Bitcoin", "symbol"=>"BTC", "website_slug"=>"bitcoin", "rank"=>1, "circulating_supply"=>17298850.0, "total_supply"=>17298850.0, "max_supply"=>21000000.0, "quotes"=>{"USD"=>{"price"=>6592.56514166, "volume_24h"=>3990509859.95985, "market_cap"=>114043795501.0, "percent_change_1h"=>-0.31, "percent_change_24h"=>-0.36, "percent_change_7d"=>-0.61}}, "last_updated"=>1538393012}, "1027"=>{"id"=>1027, "name"=>"Ethereum", "symbol"=>"ETH", "website_slug"=>"ethereum", "rank"=>2, "circulating_supply"=>102298658.0, "total_supply"=>102298658.0, "max_supply"=>nil, "quotes"=>{"USD"=>{"price"=>229.147840966, "volume_24h"=>1701049487.13546, "market_cap"=>23441516679.0, "percent_change_1h"=>-0.8, "percent_change_24h"=>-2.56, "percent_change_7d"=>-2.44}}, "last_updated"=>1538393021}, "52"=>{"id"=>52, "name"=>"XRP", "symbol"=>"XRP", "website_slug"=>"ripple", "rank"=>3, "circulating_supply"=>39870907279.0, "total_supply"=>99991836919.0, "max_supply"=>100000000000.0, "quotes"=>{"USD"=>{"price"=>0.5790784377, "volume_24h"=>1229031882.79177, "market_cap"=>23088382697.0, "percent_change_1h"=>-1.6, "percent_change_24h"=>-4.58, "percent_change_7d"=>7.96}}, "last_updated"=>1538393043}, "1831"=>{"id"=>1831, "name"=>"Bitcoin Cash", "symbol"=>"BCH", "website_slug"=>"bitcoin-cash", "rank"=>4, "circulating_supply"=>17378475.0, "total_supply"=>17378475.0, "max_supply"=>21000000.0, "quotes"=>{"USD"=>{"price"=>532.361000338, "volume_24h"=>480935803.85542, "market_cap"=>9251622335.0, "percent_change_1h"=>-0.78, "percent_change_24h"=>-1.57, "percent_change_7d"=>13.15}}, "last_updated"=>1538393013}, "1765"=>{"id"=>1765, "name"=>"EOS", "symbol"=>"EOS", "website_slug"=>"eos", "rank"=>5, "circulating_supply"=>906245118.0, "total_supply"=>1006245120.0, "max_supply"=>nil, "quotes"=>{"USD"=>{"price"=>5.6494577239, "volume_24h"=>822687391.374753, "market_cap"=>5119793479.0, "percent_change_1h"=>-0.53, "percent_change_24h"=>-3.01, "percent_change_7d"=>-1.66}}, "last_updated"=>1538393011}, "512"=>{"id"=>512, "name"=>"Stellar", "symbol"=>"XLM", "website_slug"=>"stellar", "rank"=>6, "circulating_supply"=>18789958255.0, "total_supply"=>104323820467.0, "max_supply"=>nil, "quotes"=>{"USD"=>{"price"=>0.261922298, "volume_24h"=>67633971.9905846, "market_cap"=>4921509046.0, "percent_change_1h"=>0.14, "percent_change_24h"=>1.03, "percent_change_7d"=>-0.49}}, "last_updated"=>1538392997}, "2"=>{"id"=>2, "name"=>"Litecoin", "symbol"=>"LTC", "website_slug"=>"litecoin", "rank"=>7, "circulating_supply"=>58532552.0, "total_supply"=>58532552.0, "max_supply"=>84000000.0, "quotes"=>{"USD"=>{"price"=>60.7806370254, "volume_24h"=>461333051.026648, "market_cap"=>3557645805.0, "percent_change_1h"=>-0.07, "percent_change_24h"=>-1.17, "percent_change_7d"=>4.09}},
You could try this code and feedback please :)
<h1>LISTING </h1>
<% #jsonData.fetch('data', {}).each do |id, coin| %>
Name: <%= coin['name'] %> <br />
Symbol: <%= coin['symbol'] %> <br />
Website_slug: <%= coin['website_slug'] %> <br />
Rank: <%= coin['rank'] %> <br />
Circulating_supply: <%= coin['circulating_supply'] %> <br />
total_supply: <%= coin['total_supply'] %> <br />
max_supply: <%= coin['max_supply'] %> <br />
Price: <%= coin.dig('quotes', 'USD', 'price') %> <br />
Volume24h: <%= coin.dig('quotes', 'USD', 'volume_24h') %> <br />
Market_cap: <%= coin.dig('quotes', 'USD', 'market_cap') %> <br />
Change_1he: <%= coin.dig('quotes', 'USD', 'pricpercent_change_1he') %> <br />
Change_24h: <%= coin.dig('quotes', 'USD', 'percent_change_24h') %> <br />
Change_7d: <%= coin.dig('quotes', 'USD', 'percent_change_7d') %> <br />
<%end%>
<%end%>
This is a sample of your JSON:
{
"data":{
"1":{
"id":1,
"name":"Bitcoin",
"symbol":"BTC",
"website_slug":"bitcoin",
"rank":1,
"circulating_supply":17298850.0,
"total_supply":17298850.0,
"max_supply":21000000.0,
"quotes":{
"USD":{
"price":6592.56514166,
"volume_24h":3990509859.95985,
"market_cap":114043795501.0,
"percent_change_1h":-0.31,
"percent_change_24h":-0.36,
"percent_change_7d":-0.61
}
},
"last_updated":1538393012
},
"1027":{
"id":1027,
"name":"Ethereum",
"symbol":"ETH",
"website_slug":"ethereum",
"rank":2,
"circulating_supply":102298658.0,
"total_supply":102298658.0,
"max_supply":null,
"quotes":{
"USD":{
"price":229.147840966,
"volume_24h":1701049487.13546,
"market_cap":23441516679.0,
"percent_change_1h":-0.8,
"percent_change_24h":-2.56,
"percent_change_7d":-2.44
}
},
"last_updated":1538393021
},
"52":{
"id":52,
"name":"XRP",
"symbol":"XRP",
"website_slug":"ripple",
"rank":3,
"circulating_supply":39870907279.0,
"total_supply":99991836919.0,
"max_supply":100000000000.0,
"quotes":{
"USD":{
"price":0.5790784377,
"volume_24h":1229031882.79177,
"market_cap":23088382697.0,
"percent_change_1h":-1.6,
"percent_change_24h":-4.58,
"percent_change_7d":7.96
}
},
"last_updated":1538393043
},
"1831":{
"id":1831,
"name":"Bitcoin Cash",
"symbol":"BCH",
"website_slug":"bitcoin-cash",
"rank":4,
"circulating_supply":17378475.0,
"total_supply":17378475.0,
"max_supply":21000000.0,
"quotes":{
"USD":{
"price":532.361000338,
"volume_24h":480935803.85542,
"market_cap":9251622335.0,
"percent_change_1h":-0.78,
"percent_change_24h":-1.57,
"percent_change_7d":13.15
}
},
"last_updated":1538393013
},
"1765":{
"id":1765,
"name":"EOS",
"symbol":"EOS",
"website_slug":"eos",
"rank":5,
"circulating_supply":906245118.0,
"total_supply":1006245120.0,
"max_supply":null,
"quotes":{
"USD":{
"price":5.6494577239,
"volume_24h":822687391.374753,
"market_cap":5119793479.0,
"percent_change_1h":-0.53,
"percent_change_24h":-3.01,
"percent_change_7d":-1.66
}
},
"last_updated":1538393011
},
"512":{
"id":512,
"name":"Stellar",
"symbol":"XLM",
"website_slug":"stellar",
"rank":6,
"circulating_supply":18789958255.0,
"total_supply":104323820467.0,
"max_supply":null,
"quotes":{
"USD":{
"price":0.261922298,
"volume_24h":67633971.9905846,
"market_cap":4921509046.0,
"percent_change_1h":0.14,
"percent_change_24h":1.03,
"percent_change_7d":-0.49
}
},
"last_updated":1538392997
},
"2":{
"id":2,
"name":"Litecoin",
"symbol":"LTC",
"website_slug":"litecoin",
"rank":7,
"circulating_supply":58532552.0,
"total_supply":58532552.0,
"max_supply":84000000.0,
"quotes":{
"USD":{
"price":60.7806370254,
"volume_24h":461333051.026648,
"market_cap":3557645805.0,
"percent_change_1h":-0.07,
"percent_change_24h":-1.17,
"percent_change_7d":4.09
}
}
}
}
}
Following this json you can see that there are no arrays [] it just a nested hashes {} so no need for all of your loops. For ex. to get the price you need to do something like: #jsonData['data']['1']['quotes']['USD']['price']
This path ['data']['1']['quotes']['USD']['price'] is all static, which means it will be the same for every coin. the only dynamic/changing part is ['1']. Sometimes it will be ['1'], ['1027'], ['52'], etc.
So in my code you can see that I access all of the data staticly but only for the IDs I had to loop over it but this loop will be over key => value because as mentioned before we are dealing with hashes not arrays.
So when I did <% #jsonData.fetch('data', {}).each do |id, coin| %>:
id has the value of '1'.
coin has the structure of the coin (you can staticlly retrieve any piece of data like coin.dig('quotes', 'USD', 'price') for ex.
What you where doing:
<% #jsonData.['data'].each do |coin| %>:
coin will be ['1', 'structure of the coin here']
when you do coin['name'] this will return nil but won't fail. At this point you thought it's working but it is not.

XSLT adding tag in specific pattern

I have a issue with adding a "P" tag before next pattern matching .
Source i am getting is:
<root>
<Element>
<P>Value1</P>
<P>
<Level1>
<Level2 type="i">Name1</Level2>, Title1
</Level1>, Text1
<Level1>
<Level2 type="i">Name2</Level2>
</Level1>, Text2.</P>
<P>
<Level1>
<Level2 type="i">Name3</Level2>, Title2
</Level1>, Text3.
</P>
</Element>
Desired out i want is
<root>
<Element>
<P>Value1</P>
<P>
<Level1>
<Level2 type="i">Name1</Level2>, Title1
</Level1>, Text1
</P>
<P>
<Level1>
<Level2 type="i">Name2</Level2>
</Level1>, Text2.</P>
<P>
<Level1>
<Level2 type="i">Name3</Level2>, Title2
</Level1>, Text3.
</P>
</Element>
</root>
The following template does not give me desired result, Please help
<xsl:template match="Element">
<xsl:copy>
<xsl:for-each-group select="P" group-starting-with="Level1/Level2">
<P>
<xsl:sequence select="current-group()"/>
</P>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
I think you need to use the for-each-group on a template matching P and then you get
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="P">
<xsl:for-each-group select="node()" group-starting-with="Level1[Level2]">
<P>
<xsl:apply-templates select="current-group()"/>
</P>
</xsl:for-each-group>
</xsl:template>
</xsl:transform>
which at http://xsltransform.net/gVhD8QT gives the result
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Element>
<P>Value1</P>
<P>
<Level1>
<Level2 type="i">Name1</Level2>, Title1
</Level1>, Text1
</P>
<P>
<Level1>
<Level2 type="i">Name2</Level2>
</Level1>, Text2.</P>
<P>
<Level1>
<Level2 type="i">Name3</Level2>, Title2
</Level1>, Text3.
</P>
</Element>
</root>

XSLT 2.0 - Merging adjacent OL elements

I'm trying to merge adjacent <ol>|<ul> elements that appear inside <li> elements without success.
Therefore turning:
<ol>
<li>Text node <p>Child node</p>
<ol>
<li>1</li>
</ol>
<ol>
<li>2</li>
<li>3</li>
<li>4</li>
</ol>
<p>Some text</p>
<ol>
<li>1</li>
</ol>
</li>
</ol>
Into
<ol>
<li>Text node <p>Child node</p>
<ol>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ol>
<p>Some text</p>
<ol>
<li>1</li>
</ol>
</li>
</ol>
Right now, I've come up with this:
<xsl:template match="li[ol]" priority="5">
<xsl:copy copy-namespaces="no">
<ol>
<xsl:for-each-group select="node()" group-adjacent="boolean(self::ol)">
<xsl:choose>
<xsl:when test="current-grouping-key() and current-group()[self::*]">
<xsl:apply-templates select="current-group()/node()"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</ol>
</xsl:copy>
</xsl:template>
Which gives me:
<ol>
<li>
<ol>Text node <p>Child node</p>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<p>Some text</p>
<li>1</li>
</ol>
</li>
</ol>
So I'm struggling to not move the parent <li>'s text into the merged <ol>, how do I fix this?
Using
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="li[ol]">
<xsl:copy>
<xsl:for-each-group select="node()"
group-adjacent="boolean(self::ol | self::text()[not(normalize-space())])">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<ol>
<xsl:copy-of select="current-group()/node()"/>
</ol>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
you can transform
<ol>
<li>Text node <p>Child node</p>
<ol>
<li>1</li>
</ol>
<ol>
<li>2</li>
<li>3</li>
<li>4</li>
</ol>
<p>Some text</p>
<ol>
<li>1</li>
</ol>
</li>
</ol>
into
<ol>
<li>Text node <p>Child node</p>
<ol>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ol>
<p>Some text</p>
<ol>
<li>1</li>
</ol>
</li>
</ol>

xslt: How do I wrap different tags with new tags

I'm not sure this is even doable with xslt, but here goes.
I need to break the xml from articles into different parts.
I need to get all xml until the first div with class name full-width and wrap that in a div with class name narrow-wrap.
The full-width div needs to be wrapped with a div with class name wide-wrap.
Then a new div with class narrow-wrap starts and all xml until the next div with class full-width goes into that. And so on.
This is a simplified version of the xml. Tags can have nested content, as well as attributes.
<p>lorem</p>
<div>ipsum</div>
<div class="full-width">
<img src="image.jpg" />
</div>
<p>lorem</p>
<p>ipsum</p>
<div class="full-width">
<img src="image.jpg" />
</div>
<div class="narrow-width">
<img src="image.jpg" />
</div>
<p>lorem</p>
<div class="full-width">
<img src="image.jpg" />
</div>
<div class="full-width">
<img src="image.jpg" />
</div>
<table>ipsum</table>
<p>lorem</p>
<p>ipsum</p>
<div class="narrow-width">
<img src="image.jpg" />
</div>
This is how I need it to look like after transformation:
<div class="narrow-wrap">
<p>lorem</p>
<div>ipsum</div>
</div>
<div class="wide-wrap">
<div class="full-width">
<img src="image.jpg" />
</div>
</div>
<div class="narrow-wrap">
<p>lorem</p>
<p>ipsum</p>
</div>
<div class="wide-wrap">
<div class="full-width">
<img src="image.jpg" />
</div>
</div>
<div class="wide-wrap">
<div class="narrow-width">
<img src="image.jpg" />
</div>
</div>
<div class="narrow-wrap">
<p>lorem</p>
</div>
<div class="wide-wrap">
<div class="full-width">
<img src="image.jpg" />
</div>
</div>
<div class="wide-wrap">
<div class="full-width">
<img src="image.jpg" />
</div>
</div>
<div class="narrow-wrap">
<table>ipsum</table>
<p>lorem</p>
<p>ipsum</p>
<div class="narrow-width">
<img src="image.jpg" />
</div>
</div>
The xsl for the full-width part would be something like this:
<xsl:template match="div[contains(concat(' ', #class, ' '), ' full-width ')]">
<div>
<xsl:attribute name="class">
wide-wrap
</xsl:attribute>
<xsl:copy-of select="."></xsl:copy-of>
</div>
</xsl:template>
The part of wrapping all the other content in blocks of divs with class narrow-wrap is beyond me though.
Is this possible, and if so, how?
See the group-adjacent example in https://www.w3.org/TR/xslt20/#grouping-examples and try <xsl:for-each-group select="*" group-adjacent="not (#class = 'full-width')">, then, as in the example check the key and wrap the current-group () respectively each item in the group.
Using that approach you get
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="body">
<xsl:copy>
<xsl:for-each-group select="*" group-adjacent="not (#class = 'full-width')">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<div class="narrow-wrap">
<xsl:apply-templates select="current-group()"/>
</div>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="div[contains(concat(' ', #class, ' '), ' full-width ')]">
<div class="wide-wrap">
<xsl:copy-of select="."></xsl:copy-of>
</div>
</xsl:template>
</xsl:transform>
online at http://xsltransform.net/6rewNxQ.

how to get data from my forms

i'm trying to get the data of my forms on my post metod using
public ActionResult EntradaPedidos(FormCollection formulario) {
Pedidos miPedido = new Pedidos();
UpdateModel(miPedido);
miPedido.division = Request.Form["division"];
what i'm doing wrong? i link my model in the view, and try using different forms.
http://imageshack.us/photo/my-images/13/sinttuloamq.png/
http://imageshack.us/photo/my-images/585/sinttuloecp.png/
this is my view
<% using (Html.BeginForm("EntradaPedidos","home",FormMethod.Post )){ %>
<% =Html.ValidationSummary("") %>
<p>
<label>Division: </label>
<%= Html.DropDownList("division", (SelectList)ViewData["divisiones"]) %>
<!--<%= Html.TextBox("clave1", Model.clave1) %>-->
</p>
<p>
<label>Número del pedido: </label>
<label id="numPedido"><% =Html.Encode(Model.numPedido) %></label>
<!--<% =Html.TextBox ("numeroPedido") %>-->
</p>
<p>
<label> Fecha: </label>
<% =Html.TextBox ("FechaInicio") %>
</p>
<p>
<label>Tipo de pedido: </label>
<% =Html.RadioButton ("tipoPedido", "1") %><label class="inline" for="TipoPedido">1</label>
</p>
<p>
<label>Transacción de pedido: </label>
<% =Html.RadioButton("transPedido", "D2") %> <label class="inline" for="TransPedido">D2</label>
</p>
<p>
<label>Codigo del cliente :</label>
<% =Html.TextBox ("codigoCliente") %>
<label id="lbNombreCliente">Nombre del Cliente: </label>
</p>
<p>
<label>Bodega: </label>
<% =Html.RadioButton("bodega", "CD") %><label class="inline" for="Bodega">CD</label>
</p>
<p>
<label>Lista de Precios: </label>
<label id="lbListaPrecios"> </label>
<%= Html.DropDownList("ddListaPrecios") %>
<!-- <%= Html.CheckBox("cbCambioLista") %> <label class="inline" for="cbCambioLista">Desea cambiar lista de precios?</label> -->
</p>
<p>
<label id="lbCondPago">Condiciones de Pago: </label>
</p>
<p>
<label>Vendedor: </label>
<%= Html.DropDownList("ddListaVendedores") %>
</p>
<p>
<label>Concepto Contable: </label>
<label id="lbConContable"> 03 </label>
</p>
<p>
<label>Ciudad: </label>
<% =Html.DropDownList ("ddCiudad") %>
<label>Punto de entrega: </label>
<%= Html.DropDownList("ddPuntosEntrega") %>
</p>
<p>
<input type="submit" value="Siguiente"/>
</p>
<p>
<%= Html.ActionLink("Menu opciones" , "Menu") %>
</p>
<%} %>
Pass your model to the View:
var pedidos = new Pedidos { division = "teste" };
return View(pedidos);
then tell the view what is its model:
<%# Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcProject.Models.Pedidos>" %>
and expect it on your action method
public ActionResult EntradaPedidos(Pedidos miPedido) {
var division = miPedido.division

Resources