number format for .csv using xslt1.0 - xslt

I have tag in xml which value sometimes -ve number and sometimes +ve number and I want to show the -ve(e.g -10) number as (10) I mean with bracket using xslt1.0.
Could you please help me?
I am using below template but it is not working.
<xsl:call-template name="fmtNumber">
<xsl:with-param name="amt" select="retailer:Amount" />
</xsl:call-template>
<xsl:template name="fmtNumber">
<xsl:param name="amt" />
<xsl:value-of
select="format-number($amt, ' $###,##0 ;($###,##0)')" />
</xsl:template>
it is showing the result in csv file as $(10) but, I want (10), I mean without dollar sumbol.

do:
<xsl:value-of select="format-number($amt, '###,##0;(###,##0)')"/>

Please run the following stylesheet (with any XML source) on your processor and post the resulting code.
<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:variable name="num" select="-123" />
<xsl:template match="/">
<result>
<version><xsl:value-of select="system-property('xsl:version')"/></version>
<vendor><xsl:value-of select="system-property('xsl:vendor')"/></vendor>
<test><xsl:value-of select="format-number($num, '#,##0;(#,##0)')"/></test>
</result>
</xsl:template>
</xsl:stylesheet>

Related

I'm unable to get the desired values in my XSLT

This is the input :
<Data>
<A_ID>123456789</A_ID>
<A_Code>ojhgf</A_Code>
<A_Rec>
<inner1>2345</inner1>
<inner2>14April</inner2>
<inner3>15November</inner3>
</A_Rec>
</Data>
This is my XSLT:
<xsl:variable name="AID" select="A_ID" />
<xsl:variable name="ACode" select="A_Code" />
<xsl:for-each xmlns:sch="http://schemas.w3.com/" select="//A_Rec">
<sch:COMPANY>
<xsl:value-of select="$AID" />
</sch:COMPANY>
<sch:COMPANY_CODE>
<xsl:value-of select="$ACode" />
</sch:COMPANY_CODE>
</xsl:for-each>
I am trying to get the value "123456789" in the below mentioned line. $AID should hold 123456789, i am getting the desired value outside the for-each loop though.
But I’m not getting AID and ACode values inside the for-each loop for Company and company code. What do I do?
You should be using "Data/A_ID". Assuming that your context node is not "Data" as I see nothing else wrong.
Once created, XSLT variables cannot change their value.
There's a very good SO answer here that may help you redesign your XSLT.
Edit: Lingamurthy CS has correctly identified an issue with your XPath, but take a look at the SO page I've linked as it still might be useful.
It seems you want to output A_ID and A_Code in a different node with a different namespace.
Try this instead:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:template match="/">
<root xmlns:sch="http://schemas.w3.com/">
<xsl:apply-templates select="Data/A_ID|Data/A_Code"/>
</root>
</xsl:template>
<xsl:template match="Data/A_ID">
<xsl:element name="sch:COMPANY" namespace="http://schemas.w3.com/">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="Data/A_Code">
<xsl:element name="sch:COMPANY_CODE" namespace="http://schemas.w3.com/">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

XSLT correct approach in writing XPath expression used in multiple condition checking and displaying value

Considering this XML,
XML:
<?xml version="1.0" encoding="UTF-8"?>
<items>
<book>
<title>doublebell</title>
<count>available</count>
</book>
<phone>
<brand>nokia</brand>
<model></model>
</phone>
</items>
Mapping Criteria while writing XSLT:
show the newbook/newtitle only if a value is present in input.
show the newbook/newcount only if a value is present in input.
show the newphone/newbrand only if a value is present in input.
show the newphone/newmodel only if a value is present in input.
XSLT:
<?xml version="1.0" encoding="ISO-8859-1"?>
<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:variable name="book" select="items/book" />
<xsl:variable name="phone" select="items/phone" />
<xsl:template match="/">
<items>
<newbook>
<xsl:if test="$book/title!=''">
<newtitle>
<xsl:value-of select="$book/title" />
</newtitle>
</xsl:if>
<xsl:if test="$book/count!=''">
<newcount>
<xsl:value-of select="$book/count" />
</newcount>
</xsl:if>
</newbook>
<xsl:if test="$phone/brand!='' or $phone/model!=''"> <!-- not sure if this condition is required for the above mapping criteria -->
<newphone>
<xsl:if test="$phone/brand!=''">
<newbrand>
<xsl:value-of select="$phone/brand" />
</newbrand>
</xsl:if>
<xsl:if test="$phone/model!=''">
<newmodel>
<xsl:value-of select="$phone/model" />
</newmodel>
</xsl:if>
</newphone>
</xsl:if>
</items>
</xsl:template>
</xsl:stylesheet>
This is my concern:- In my actual XSLT, I have almost 70 conditions like
this, and everytime the XPath search is made twice [or thrice.. ] for
each condition [ for eg: <xsl:if test="$phone/brand!=''"> and <xsl:value-of select="$phone/brand" /> and outer if condition].
Is this much performance overhead? I don't feel it when I ran my application.
I like to hear from experienced people if this is correct way of writing the XSLT. Do I need to save the path in a variable and reuse it as done for $book
and $phone ? In such a case there will be 70+variables just to hold this.
You can approach this quite differently using templates. If you define a template that matches any element whose content is empty and does nothing:
<xsl:template match="*[. = '']" />
or possibly use normalize-space() if you want to consider elements to be empty if they contain only whitespace
<xsl:template match="*[not(normalize-space())]" />
Now with this in place add templates for the elements you are interested in
<xsl:template match="book">
<newbook><xsl:apply-templates /></newbook>
</xsl:template>
<xsl:template match="title">
<newtitle><xsl:apply-templates /></newtitle>
</xsl:template>
and so on. Now the book template will create a newbook element and go on to process its children. When it gets to the title it will have two different templates to choose from and will pick the "most specific" match. If the title is empty then the *[. = ''] template will win and nothing will be output, only if the title is non-empty will it create a newtitle element.
This way you let the template matcher do most of the work for you, you don't need any explicit conditional checks using xsl:if.
<?xml version="1.0" encoding="ISO-8859-1"?>
<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="/">
<items><xsl:apply-templates select="items/*" /></items>
</xsl:template>
<!-- ignore empty elements -->
<xsl:template match="*[not(normalize-space())]" />
<xsl:template match="book">
<newbook><xsl:apply-templates /></newbook>
</xsl:template>
<xsl:template match="title">
<newtitle><xsl:apply-templates /></newtitle>
</xsl:template>
<!-- and so on with similar templates for the other elements -->
</xsl:stylesheet>
Building on Ian's answer, you can also make a generic template that will create the "new" elements for you without having to specify each one individually. That would look like the below:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<items><xsl:apply-templates select="items/*" /></items>
</xsl:template>
<xsl:template match="*[not(normalize-space())]" />
<xsl:template match="*">
<xsl:element name="{concat('new',name())}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
That last template just rebuilds the element by concatenating the word "new" to the front of it.

XSLT issue with reading in non-xml data

I am newbie to XSLT.
I have a requirement to read a URL and convert some of its values into XML.
I wrote a XSLT that has to take URL as the input value and create a XML file from some of the content of the URL value.
When I debugged the XSLT in XMLSPY, I noticed that the URL value is not being picked up by inputValue variable in the below code. I am not sure if my approach to input the URL and the template match are wrong.
Any help is appreciated.
Thanks in advance.
Input to XSLT:
http://host:port/abc/xyz1/6xyz6?qq=123&pp=3
Here the XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:nnc="Nnc" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:param name="inVal" select="xs:string(http://host:port/abc/xyz1/6xyz6?qq=123&pp=3)"/>
<xsl:template match="/">
<xsl:variable name="inputValue" select="$inVal"/>
<xsl:if test="string-length($inputValue)=0">
<xsl:message terminate="yes">
inputValue is blank
</xsl:message>
</xsl:if>
<xsl:variable name="value" as="xs:string" select="substring-after($inputValue, 'abc/' )"/>
<xsl:variable name="tokenizedValues" select="tokenize($value,'/')"/>
<xsl:for-each select="$tokenizedValues">
<xsl:if test="position() = 1">
<id>
<xsl:value-of select="."/>
</id>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The desired XML output:
<?xml version="1.0" encoding="UTF-8"?>
<id>6xyz6</id>
<qq>123</qq>
<pp>123</pp>
Well if you want to pull in a text file then with XSLT 2.0 and later you can do that but not by simply using a URL, you need to call the unparsed-text function e.g.
<xsl:variable name="inputData" as="xs:string" select="unparsed-text('http://example.com/foo')"/>
See http://www.w3.org/TR/xslt20/#unparsed-text, depending on the encoding of your text document you need to add a second parameter when calling the function.

XSLT: Replace string with Abbreviations

I would like to know how to replace the string with the abbreviations.
My XML looks like below
<concept reltype="CONTAINS" name="Left Ventricular Major Axis Diastolic Dimension, 4-chamber view" type="NUM">
<code meaning="Left Ventricular Major Axis Diastolic Dimension, 4-chamber view" value="18074-5" schema="LN" />
<measurement value="5.7585187646">
<code value="cm" schema="UCUM" />
</measurement>
<content>
<concept reltype="HAS ACQ CONTEXT" name="Image Mode" type="CODE">
<code meaning="Image Mode" value="G-0373" schema="SRT" />
<code meaning="2D mode" value="G-03A2" schema="SRT" />
</concept>
</content>
</concept>
and I am selecting some value from the xml like,
<xsl:value-of select="concept/measurement/code/#value"/>
Now what I want is, I have to replace cm with centimeter. I have so many words like this. I would like to have a xml for abbreviations and replace from them.
I saw one similar example here.
Using a Map in XSL for expanding abbreviations
But it replaces node text, but I have text as attribute. Also, it would be better for me If I can find and replace when I select text using xsl:valueof select instead of having a separate xsl:template. Please help. I am new to xslt.
I have created XSLT v "1.1". For abbreviations I have created XML file as you have mentioned:
Abbreviation.xml:
<Abbreviations>
<Abbreviation>
<Short>cm</Short>
<Full>centimeter</Full>
</Abbreviation>
<Abbreviation>
<Short>m</Short>
<Full>meter</Full>
</Abbreviation>
</Abbreviations>
XSLT:
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml" />
<xsl:param name="AbbreviationDoc" select="document('Abbreviation.xml')"/>
<xsl:template match="/">
<xsl:call-template name="Convert">
<xsl:with-param name="present" select="concept/measurement/code/#value"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="Convert">
<xsl:param name="present"/>
<xsl:choose>
<xsl:when test="$AbbreviationDoc/Abbreviations/Abbreviation[Short = $present]">
<xsl:value-of select="$AbbreviationDoc/Abbreviations/Abbreviation[Short = $present]/Full"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$present"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
INPUT:
as you have given <xsl:value-of select="concept/measurement/code/#value"/>
OUTPUT:
centimeter
You just need to enhance this Abbreviation.xml to keep short and full value of abbreviation and call 'Convert' template with passing current value to get desired output.
Here a little shorter version:
- with abbreviations in xslt file
- make use of apply-templates with mode to make usage shorter.
But with xslt 1.0 node-set extension is required.
<?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:output method="xml" indent="yes"/>
<xsl:variable name="abbreviations_txt">
<abbreviation abbrev="cm" >centimeter</abbreviation>
<abbreviation abbrev="m" >meter</abbreviation>
</xsl:variable>
<xsl:variable name="abbreviations" select="exsl:node-set($abbreviations_txt)" />
<xsl:template match="/">
<xsl:apply-templates select="concept/measurement/code/#value" mode="abbrev_to_text"/>
</xsl:template>
<xsl:template match="* | #*" mode="abbrev_to_text">
<xsl:variable name="abbrev" select="." />
<xsl:variable name="long_text" select="$abbreviations//abbreviation[#abbrev = $abbrev]/text()" />
<xsl:value-of select="$long_text"/>
<xsl:if test="not ($long_text)">
<xsl:value-of select="$abbrev"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

can we use dynamic variable name in the select statement in xslt?

I wanted to use a dynamic variable name in the select statement in xslt.
<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="/">
<xsl:variable name="input" select="input/message" />
<xsl:variable name="Name" select="'MyName'" />
<xsl:variable name="Address" select="MyAddress" />
<xsl:variable name="output" select="concat('$','$input')" /> <!-- This is not working -->
<output>
<xsl:value-of select="$output" />
</output>
</xsl:template>
The possible values for the variable "input" is 'Name' or 'Address'.
The select statement of the output variable should have a dynamic variable name based on the value of input variable. I don't want to use xsl:choose. I wanted to select the value dynamically.
Please provide me a solution.
Thanks,
dhinu
XSLT 1.0 and XSLT 2.0 don't have dynamic evaluation.
Solution for your problem:
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my">
<xsl:output method="text"/>
<my:values>
<name>MyName</name>
<address>MyAdress</address>
</my:values>
<xsl:template match="/">
<xsl:variable name="vSelector"
select="input/message"/>
<xsl:value-of select=
"document('')/*/my:values/*[name()=$vSelector]"/>
</xsl:template>
</xsl:stylesheet>
when applied on the following XML document:
<input>
<message>address</message>
</input>
produces the wanted, correct result:
MyAdress
when the same transformation is applied on this XML document:
<input>
<message>name</message>
</input>
again the wanted, correct result is produced:
MyName
Finally: If you do not wish to use the document() function, but would go for using the xxx:node-set() extension function, then this solution (looking very similar) is what you want, where you may consult your XSLTprocessor documentation for the exact namespace of the extension:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common" >
<xsl:output method="text"/>
<xsl:variable name="vValues">
<name>MyName</name>
<address>MyAdress</address>
</xsl:variable>
<xsl:template match="/">
<xsl:variable name="vSelector"
select="input/message"/>
<xsl:value-of select=
"ext:node-set($vValues)/*[name()=$vSelector]"/>
</xsl:template>
</xsl:stylesheet>
Beside #Dimitre's good answer, for this particular case (output string value) you could also use:
<xsl:variable name="output"
select="concat(substring($Name, 1 div ($input = 'Name')),
substring($Address, 1 div ($input = 'Address')))"/>