I am new to XSLT. Please share XSLT for below input and Output XMLs.
Input XML :
<Cars>
<Car>
<Company>Maruthi</Company>
<Model>Alto</Model>
<FeatureName>CC|Mileage</FeatureName>
<FeatureValue>800|20</FeatureValue>
</Car>
<Car>
<Company>Hyundai</Company>
<Model>i10</Model>
<FeatureName>CC|Mileage|Airbag</FeatureName>
<FeatureValue>1000|18|Y</FeatureValue>
</Car>
</Cars>
Output XML :
<Cars>
<Car>
<Company>Maruthi </Company>
<Model>Alto </Model>
<FeatureName><CC>800</CC><Mileage>20</Mileage></FeatureName>
</Car>
<Car>
<Company>Hyundai </Company>
<Model>i10 </Model>
<FeatureName><CC>1000</CC><Mileage>18</Mileage><Airbag>Y</Airbag></FeatureName>
</Car>
</Cars>
Please provide the XSLT to transform input to Output XML.
Thanks in Advance.
This works:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="Cars/Car/FeatureValue"/>
<xsl:template match="Cars/Car/FeatureName">
<xsl:param name="featureNames" select="concat(./text(),'|')"/>
<xsl:param name="featureValues" select="concat(following-sibling::FeatureValue/text(),'|')"/>
<FeatureName>
<xsl:call-template name="features">
<xsl:with-param name="featureNames" select="$featureNames" />
<xsl:with-param name="featureValues" select="$featureValues" />
</xsl:call-template>
</FeatureName>
</xsl:template>
<xsl:template name="features">
<xsl:param name="featureNames"/>
<xsl:param name="featureValues"/>
<xsl:param name="featureName" select="substring-before($featureNames,'|')"/>
<xsl:param name="featureValue" select="substring-before($featureValues,'|')"/>
<xsl:choose>
<xsl:when test="$featureName">
<xsl:element name="{$featureName}"><xsl:value-of select="$featureValue" /></xsl:element>
<xsl:call-template name="features">
<xsl:with-param name="featureNames" select="substring-after($featureNames,'|')" />
<xsl:with-param name="featureValues" select="substring-after($featureValues,'|')" />
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Related
I would like to change an image path.
The code to display the image is:
<xsl:value-of select="image"/>
the image path it takes from the feed is:
https://www.website.com/media/catalog/product/1/7/image.jpg
what i would like is:
https://www.website.com/media/catalog/product/thumbnail/folder/1/7/image.jpg
i tried the following:
<xsl:template name="string-replace-all">
<xsl:param name="text"/>
<xsl:param name="replace"/>
<xsl:param name="with"/>
<xsl:choose>
<xsl:when test="contains($text,$replace)">
<xsl:value-of select="substring-before($text,$replace)"/>
<xsl:value-of select="$with"/>
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="substring-after($text,$replace)"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="with" select="$with"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="image"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="root">
<xsl:for-each select="product[position() = 1]">
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="'https://www.website.com/media/catalog/product/'"/>
<xsl:with-param name="replace" select="'product/'" />
<xsl:with-param name="with" select="'product/thumbnail/folder/'"/>
</xsl:call-template>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
the output is:
https://www.website.com/media/catalog/product/thumbnail/folder/https://www.website.com/media/catalog/product/image.jpg
not getting a real error message, it is just showing twice the http part
Couldn't this be a lot simpler? For example, consider:
XML
<root>
<product>
<image>https://www.website.com/media/catalog/product/1/7/image.jpg</image>
</product>
<product>
<image>https://www.website.com/media/catalog/product/2/56/image.jpg</image>
</product>
<product>
<image>https://www.website.com/media/catalog/product/3/123/image.jpg</image>
</product>
</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:template match="/root">
<images>
<xsl:for-each select="product">
<image>
<xsl:text>https://www.website.com/media/catalog/product/thumbnail/folder/</xsl:text>
<xsl:value-of select="substring-after(image, 'https://www.website.com/media/catalog/product/')" />
</image>
</xsl:for-each>
</images>
</xsl:template>
</xsl:stylesheet>
Result
<?xml version="1.0" encoding="UTF-8"?>
<images>
<image>https://www.website.com/media/catalog/product/thumbnail/folder/1/7/image.jpg</image>
<image>https://www.website.com/media/catalog/product/thumbnail/folder/2/56/image.jpg</image>
<image>https://www.website.com/media/catalog/product/thumbnail/folder/3/123/image.jpg</image>
</images>
I'm a bit new to this language so I have several doubts.
I'm working to process an xml to display some data on pdf form.
But there a few strings that have "|" so I can split the data to display properly.
Here is the example of the input data:
<root>
<reference>
<NroLinRef>12</NroLinRef>
<CodRef>I20</CodRef>
<RazonRef>Data1|Data2|Data3|Data4|Data5|Data6|Data7</RazonRef>
</reference>
</root>
In the output I need something like this so I can display in order in row with cells so data must be clear to read.
<root>
<Reference>
<NroLinRef>12</NroLinRef>
<CodRef>I20</CodRef>
<Data1>Data1</Data1>
<Data2>Data2</Data2>
<Data3>Data3</Data3>
<Data4>Data4</Data4>
<Data5>Data5</Data5>
<Data6>Data6</Data6>
<Data7>Data7</Data7>
</Reference>
</root>
To do this I have been using other code that is from another question but can't find how to get the name to be updated or customized.
And the output I get is actually like this:
<root>
<Reference>
<NroLinRef>12</NroLinRef>
<CodRef>I20</CodRef>
<Data>Data1</Data>
<Data>Data2</Data>
<Data>Data3</Data>
<Data>Data4</Data>
<Data>Data5</Data>
<Data>Data6</Data>
<Data>Data7</Data>
</Reference>
</root>
This is the XSL i'm using
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Referencia/RazonRef" name="tokenize">
<xsl:param name="text" select="."/>
<xsl:param name="separator" select="'|'"/>
<xsl:choose>
<xsl:when test="not(contains($text, $separator))">
<Data>
<xsl:value-of select="normalize-space($text)"/>
</Data>
</xsl:when>
<xsl:otherwise>
<Data>
<xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
</Data>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $separator)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
How can I get the output I want?
The expected result can be achieved by applying the following stylesheet:
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:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="RazonRef" name="tokenize">
<xsl:param name="text" select="."/>
<xsl:param name="separator" select="'|'"/>
<xsl:param name="i" select="1"/>
<xsl:element name="Data{$i}">
<xsl:value-of select="substring-before(concat($text, $separator), $separator)"/>
</xsl:element>
<xsl:if test="contains($text, $separator)">
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $separator)"/>
<xsl:with-param name="i" select="$i + 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
I have XML file with mixed data, some of them have to be just copied but some of them have to be splited and copied based on separators using xslt v 1.0
Input is like this one:
<class id=1>
<teacher id=100>
Some Teacher
</teacher>
<pupils>
101,boy,John;102,girl,Anna;103,boy,Tom
</pupils>
</class>
And I have to transform it to this format:
<class id=1>
<teacher id=100>
Some Teacher
</teacher>
<pupil id=101>
<gender id="boy">
<name>
John
</name>
</gender>
</pupil>
<pupil id=102>
<gender id="girl">
<name>
Anna
</name>
</gender>
</pupil>
<pupil id=103>
<gender id="boy">
<name>
Tom
</name>
</gender>
</pupil>
</class>
Many thanks in advance how to hadle this.
Try it this way?
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:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="pupils">
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="normalize-space(.)"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="';'"/>
<xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" />
<xsl:if test="$token">
<pupil id="{substring-before($token, ',')}">
<gender id="{substring-before(substring-after($token, ','), ',')}">
<name>
<xsl:value-of select="substring-after(substring-after($token, ','), ',')" />
</name>
</gender>
</pupil>
</xsl:if>
<xsl:if test="contains($text, $delimiter)">
<!-- recursive call -->
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
A customer supplied XML contains the delivery address as a comma separated string, I would like to split this string into named nodes using XSLT 1.0.
<?xml version="1.0" encoding="UTF-8"?>
<root>
<text>
Company, Streetaddress 20, 1234 AA, City
</text>
</root>
Output
<?xml version="1.0" encoding="UTF-8"?>
<root>
<text>
<COMPANY>Company</COMPANY>
<ADDRESS>Streetaddress 20</ADDRESS>
<ZIPCODE>1234 AA</ZIPCODE>
<CITY>City</CITY>
</text>
</root>
I tried several recursive templates for XSLT 1.0 which do a fine job splitting but the resulting nodes are identically named.
If possible, how can this be achieved using XSLT 1.0?
Does it have to be a recursive template? How about a straight-forward chain of substring-before and substring-after like this:
<xsl:template match="text">
<xsl:copy>
<COMPANY>
<xsl:value-of select="normalize-space(substring-before(., ','))"/>
</COMPANY>
<xsl:variable name="s1" select="substring-after(., ',')"/>
<ADDRESS>
<xsl:value-of select="normalize-space(substring-before($s1, ','))"/>
</ADDRESS>
<xsl:variable name="s2" select="substring-after($s1, ',')"/>
<ZIPCODE>
<xsl:value-of select="normalize-space(substring-before($s2, ','))"/>
</ZIPCODE>
<CITY>
<xsl:value-of select="normalize-space(substring-after($s2, ','))"/>
</CITY>
</xsl:copy>
</xsl:template>
For the fun of it, here is a generic version using a recursive template.
<xsl:template match="text">
<xsl:copy>
<xsl:call-template name="parse-comma-separated">
<xsl:with-param name="elements" select="'COMPANY,ADDRESS,ZIPCODE,CITY'"/>
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="parse-comma-separated">
<xsl:param name="elements"/>
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($elements, ',')">
<xsl:element name="{normalize-space(substring-before($elements, ','))}">
<xsl:value-of select="normalize-space(substring-before($text, ','))"/>
</xsl:element>
<xsl:call-template name="parse-comma-separated">
<xsl:with-param name="elements" select="substring-after($elements, ',')"/>
<xsl:with-param name="text" select="substring-after($text, ',')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{normalize-space($elements)}">
<xsl:value-of select="normalize-space($text)"/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
As the supplied XML only had the address as single node (Streettaddress 20) I added a second variable to split the address string into streetaddress and housenumber.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" indent="yes" method="xml"/>
<xsl:template match="text">
<root>
<COMPANY>
<xsl:value-of select="normalize-space(substring-before(., ','))"/>
</COMPANY>
<xsl:variable name="s1" select="substring-after(., ',')"/>
<xsl:variable name="address_temp" select="normalize-space(substring-before($s1, ','))"/>
<ADDRESS>
<xsl:value-of select="normalize-space(substring-before($address_temp, ' '))"/>
</ADDRESS>
<HOUSENUMBER>
<xsl:value-of select="normalize-space(substring-after($address_temp, ' '))"/>
</HOUSENUMBER>
<xsl:variable name="s2" select="substring-after($s1, ',')"/>
<ZIPCODE>
<xsl:value-of select="normalize-space(substring-before($s2, ','))"/>
</ZIPCODE>
<CITY>
<xsl:value-of select="normalize-space(substring-after($s2, ','))"/>
</CITY>
</root>
</xsl:template>
</xsl:stylesheet>
Result:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<COMPANY>Company</COMPANY>
<ADDRESS>Streetaddress</ADDRESS>
<HOUSENUMBER>20</HOUSENUMBER>
<ZIPCODE>1234 AA</ZIPCODE>
<CITY>City</CITY>
</root>
Using XSLT 1.0, how can I summarize subnodes under a given node while modifiyng the content with data from another set of nodes in an elegant way? Assume I have this xml:
<Root>
<ExchangeRates>
<ExchangeRate>
<CurrencyCode>USD</CurrencyCode>
<Rate>6.4</Rate>
</ExchangeRate>
<ExchangeRate>
<CurrencyCode>EUR</CurrencyCode>
<Rate>8.44</Rate>
</ExchangeRate>
<ExchangeRate>
<CurrencyCode>SEK</CurrencyCode>
<Rate>1</Rate>
</ExchangeRate>
</ExchangeRates>
<Prices>
<Price>
<Currency>SEK</Currency>
<Amount>10000</Amount>
</Price>
<Price>
<Currency>EUR</Currency>
<Amount>1000</Amount>
</Price>
<Price>
<Currency>USD</Currency>
<Amount>1000</Amount>
</Price>
</Prices>
</Root>
I want the sum of all Amounts converted into SEK with the help of the ExchangeRates. The result should be:
<SumInSEK>24840</SumInSEK>
If I didn't have to convert the amounts I would simply use the xpath sum() function. Is is possible to use that function in this case?
Another possible solution without recursive calls but with exsl extension.
This make use of the key definition form #softwarebear.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:key name="rates" match="//ExchangeRate/Rate" use="parent::*/child::CurrencyCode/text()"/>
<xsl:template match="/" >
<xsl:apply-templates select="//Prices"/>
</xsl:template>
<xsl:template match="Prices">
<SUmInSEK>
<xsl:variable name="price_SEK">
<xsl:apply-templates mode="SEK" />
</xsl:variable>
<xsl:value-of select="sum(exsl:node-set($price_SEK)//price_SEK)"/>
</SUmInSEK>
</xsl:template>
<xsl:template match="Price" mode="SEK">
<price_SEK>
<xsl:value-of
select="number(Amount) * number( key('rates', Currency) )" />
</price_SEK>
</xsl:template>
</xsl:stylesheet>
Wiht the output
<SUmInSEK>24840</SUmInSEK>
I thought this might be simple ... but I don't think you can use sum() on this occasion ... the best I can do is a recursive template.
?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:key name="rates" match="//ExchangeRate/Rate" use="parent::*/child::CurrencyCode/text()"/>
<xsl:template match="//Prices">
<SUmInSEK>
<xsl:call-template name="sum"/>
</SUmInSEK>
</xsl:template>
<xsl:template name="sum">
<xsl:param name="iterator" select="1"/>
<xsl:param name="total" select="0"/>
<xsl:variable name="price" select="child::Price[$iterator]"/>
<xsl:variable name="current">
<xsl:value-of select="number($price/child::Amount) * number( key('rates', $price/child::Currency) )"/>
</xsl:variable>
<xsl:variable name="newtotal">
<xsl:value-of select="$total + $current"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="$price/following-sibling::Price">
<xsl:call-template name="sum">
<xsl:with-param name="iterator" select="$iterator+1"/>
<xsl:with-param name="total" select="$newtotal"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$total + $current"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="* | /">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="text() | #*">
</xsl:template>
<xsl:template match="processing-instruction() | comment()" />
</xsl:stylesheet>