Passing a parameter does not seem to work - xslt-1.0

I need a parameter to be send to a template to help with processing the right nodes in an external second xml file (this to keep the amount of templates small).
I've found that the following code send the parameter and processes it correctly:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
This is text1
<xsl:apply-templates mode="my-mode">
<xsl:with-param name="testParam" select="'TEST_PARAMETER'"/>
</xsl:apply-templates>
This is text2
</xsl:template>
<xsl:template match="DialStats" mode="my-mode">
<xsl:param name="testParam" />
<br></br>
This is text 3<br></br>
<xsl:value-of select="$testParam" /><br></br>
This is text 4<br></br>
</xsl:template>
</xsl:stylesheet>
When I implement the second file I've found that the following no longer passes the parameter:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
This is text1
<xsl:apply-templates select="document('file://D:/DATA/Marquee/DATA/MarqueeStats.xml')" mode="my-mode">
<xsl:with-param name="testParam" select="'TEST_PARAMETER'"/>
</xsl:apply-templates>
This is text2
</xsl:template>
<xsl:template match="DialStats" mode="my-mode">
<xsl:param name="testParam" />
<br></br>
This is text 3<br></br>
<xsl:value-of select="$testParam" /><br></br>
This is text 4<br></br>
</xsl:template>
It calls the template correctly, as I can see both the lines with 'This is text x". So the parameter is gone. I did find some articles suggesting the match= needs to be set, so I matched that with the xml root:
<DialStats>
<Countries Country="Denmark">
<Products Product="MR">
. . .
Any suggestions on how to resolve this?
BTW this is just the basics I need to get right as a lot more code need to be added.

Related

XSLT 1.0: How to determine the position of a node based on the value

thanks for taking the time!
I am trying to get the position of a node based on its value, so that I can then get corresponding entries. Please let me show you with an example, it will be easier to explain.
Here is the source XML:
<?xml version="1.0" encoding="UTF-8"?>
<sampleSourceXML>
<Table>
<Header>
<Column>Name</Column>
<Column>Surname</Column>
<Column>Title</Column>
</Header>
<Body>
<Row>
<Entry>James</Entry>
<Entry>Bond</Entry>
<Entry>Mr</Entry>
</Row>
<Row>
<Entry>Harry</Entry>
<Entry>Potter</Entry>
<Entry>Mr</Entry>
</Row>
</Body>
</Table>
</sampleSourceXML>
This is the resulting XML that I would like to achieve:
<?xml version="1.0" encoding="UTF-8"?>
<sampleResultXML>
<Person>
<FirstName>James</FirstName>
<LastName>Bond</LastName>
<Title>Mr</Title>
</Person>
<Person>
<FirstName>Harry</FirstName>
<LastName>Potter</LastName>
<Title>Mr</Title>
</Person>
</sampleResultXML>
I tried it with this XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:element name="sampleResultXML">
<xsl:for-each select="sampleSourceXML/Table/Rows/Row">
<xsl:element name="Person">
<xsl:element name="FirstName">
<xsl:value-of select="./Entry[1]/text()"></xsl:value-of>
</xsl:element>
<xsl:element name="LastName">
<xsl:value-of select="./Entry[2]/text()"></xsl:value-of>
</xsl:element>
<xsl:element name="Title">
<xsl:value-of select="./Entry[3]/text()"></xsl:value-of>
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
This works.
But! The source XML can have a different order. For example the Title element can be above the Name element. So the hardcoded position in my XSLT (e.g. Entry[3]) will not work any more.
The header order and the entry order are always corresponding. So if the Column Title is in position 3, so will be the corresponding row.
So I need a way to get the position of, let's say, the Title element dynamically. So I have to go through the Column elements until I reach the one with value "Title". This position I thought I'd save in a variable (e.g. Position_Title) so that I can fill the value later like so:
<xsl:element name="Title">
<xsl:value-of select="./Entry[$Position_Title]/text()"></xsl:value-of>
</xsl:element>
However I haven't been able to find a way to do this...
Do you have any ideas how I can achieve this?
Thanks!
Nick
EDIT: Thanks to Michael for the help! Here is the final XSLT I'm using:
<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="/sampleSourceXML">
<xsl:variable name="cols" select="Table/Header/Column"/>
<sampleResultXML>
<xsl:for-each select="Table/Body/Row">
<Person>
<xsl:for-each select="Entry">
<xsl:variable name="i" select="position()"/>
<xsl:variable name="elementName">
<xsl:choose>
<xsl:when test="$cols[$i]='Name'">
<xsl:value-of select="'FirstName'"/>
</xsl:when>
<xsl:when test="$cols[$i]='Surname'">
<xsl:value-of select="'LastName'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$cols[$i]"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:element name="{$elementName}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</Person>
</xsl:for-each>
</sampleResultXML>
</xsl:template>
</xsl:stylesheet>
So I need a way to get the position of, let's say, the Title element dynamically. So I have to go through the Column elements until I reach the one with value "Title". This position I thought I'd save in a variable (e.g. Position_Title) so that I can fill the value later like so:
I think it could be much simpler if you do it from the opposite direction. Try:
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="/sampleSourceXML">
<xsl:variable name="cols" select="Table/Header/Column" />
<sampleResultXML>
<xsl:for-each select="Table/Body/Row">
<Person>
<xsl:for-each select="Entry">
<xsl:variable name="i" select="position()" />
<xsl:element name="{$cols[$i]}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</Person>
</xsl:for-each>
</sampleResultXML>
</xsl:template>
</xsl:stylesheet>

xslt - create empty file using xslt 1.0

I am trying to create an empty file through xslt.
The input sample is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Businessman>
<siblings>
<sibling>John </sibling>
</siblings>
<child> Pete </child>
<child> Ken </child>
</Businessman>
When the input contains any presence of 'child' tags, it should produce the file AS IS. When the input does not have any 'child' tag, I need an empty file (0 byte file) created.
This is what I tried:
<?xml version="1.0" encoding="UTF-8"?>
<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="#*|node()">
<xsl:choose>
<xsl:when test="/Businessman/child">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
This gives the file unchanged when there is any 'child' tag present. But did not produce any empty file when there is no 'child' tag.
The file I need to test will look like:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Businessman>
<siblings>
<sibling>John </sibling>
</siblings>
</Businessman>
Any help would be great!
Thanks
If you want the processor to go to the trouble of opening the output file, you have to give it something to write to the output file. Try an empty text node. And you only need to make the decision 'copy or not?' once.
One way to make the decision just once and produce empty output if the condition is not met would be to replace your template with:
<xsl:template match="/">
<xsl:choose>
<xsl:when test="/Businessman/child">
<xsl:copy-of select="*"/>
</xsl:when>
<xsl:otherwise>
<xsl:text/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
This works as expected with xsltproc. (If you find yourself getting a file containing an XML declaration and nothing else, try adjusting the parameters on xsl:output.)
But when I have found myself with a similar situation (perform this transform if condition C holds, otherwise ...), I have simply added a template for the document node that would look something like this for your case:
<xsl:choose>
<xsl:when test="/Businessman/child">
<xsl:apply-templates/>
</
<xsl:otherwise>
<xsl:message terminate="yes">No children in this input, dying ...</
</
</
That way I get no output at all rather than zero-length output.
Simple enough - Just don't try to do everything in one template, don't forget to omit the xml declaration and get the xpath right:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="Businessman[child]" priority="9">
<xsl:element name="Businessman">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="Businessman" priority="0" />
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

xslt nested transformation : unable to transnform

I am learning XSLT and trying to transform below xml. But couldn't achieve the task
<sample id="7">
<land1 id="8">
<owner>TOMMY</owner>
<type>INDIVIDUAL</type>
<hint>TOM_INDIVIDUAL</hint>
<date>12.02.2014</date>
<text>land details</text>
<number>1</number>
<cost>WIDERRUFLICH</cost>
</land1>
</sample>
and trying to convert above into
<table name="sample">
<tablename="land1">
<rel name="owner" value="TOMMY"/>
<rel name="type" value="INDIVIDUAL"/>
<rel name="<hint" value="TOM_INDIVIDUAL"/>
<rel name="date" value="12.02.2014"/>
<rel name="details" value="land details"/>
<rel name="number" value="1"/>
<rel name="cost" value="25%"/>
</table>
</table>
I tried below to generate the same, but it's not working.
<?xml version="1.0" encoding="UTF-8"?>
<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:for-each select="*">
<xsl:if test="current().count(*)>0">
<xsl:element name="table">
<xsl:attribute name="name">
<xsl:value-of select="name(.)"/>
</xsl:attribute>
<xsl:apply-templates select="/"
</xsl:element>
</xsl:if>
<xsl:if test="current().count(*)=0">
<xsl:element name="rel">
<xsl:attribute name="name">
<xsl:value-of select="name(.)"/>
</xsl:attribute>
<xsl:attribute name="value">
<xsl:value-of select="current()"/>
</xsl:attribute>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Can someone please let me know where I am doing wrong?
Can someone please let me know where I am doing wrong?
Well, for one thing, current().count(*)>0 is not a valid expression.
And you have <xsl:apply-templates select="/" without closing the tag. Which may be a good thing - because if it worked, it would have created an infinite loop.
I also don't understand the overall logic of your approach. Couldn't you do simply:
<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="*"/>
<xsl:template match="*[*]">
<table name="{name()}">
<xsl:apply-templates/>
</table>
</xsl:template>
<xsl:template match="*">
<rel name="{name()}" value="{.}"/>
</xsl:template>
</xsl:stylesheet>
Added:
every element with an attribute should also be a table. For example,
in the above xml, <land1 id="8"> has children and your logic works
fine. But my inout can also contain <land1 id="8"/>. Now even though
it doesn't have any child elements, still element name has to be
considered as Table
Then use:
<xsl:template match="*[#*]">
instead of:
<xsl:template match="*[*]">

Using XSLT to transform date format from yyyy-mm-dd to dd-mm-yy

I am reading an XML document in which there is a date tag as shown below.
<uabDate>2014-07-23</uabDate>
Now I am reading this XML from XSL and trying to parse it, but I want to translate that date value into a format like 23-07-14.
What I have tried is given below.
Here I am calling the template that will store uabDate in a variable businessDate and then calling the template
<xsl:call-template name="convertDateToDDMMYYYY_template">
<xsl:with-param name="b1"
select="$s2Date/uabDate"/>
</xsl:call-template>
and here is the definition of the named template:
<xsl:template name="convertDateToDDMMYYYY_template">
<xsl:param name="b1"/>
<xsl:variable name="DateVar">
<xsl:value-of select="concat(
substring(./b1,1,4),
'-',
substring(./b1,6,2),
'-',
substring(./b1,9,2))"/>
</xsl:variable>
</xsl:template>
You wrote your template wrong. First of all you are putting the result into a variable, called DateVar, and don't output it.
Secondly you got a parameter called b1 and parameters should be called using $b1.
And third issue is you are formatting the date same as the input. Instead of changing the date format.
So for example if I use this input XML:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<uabDate>2014-07-23</uabDate>
</data>
And this XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.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="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="uabDate">
<xsl:copy>
<xsl:call-template name="convertDateToDDMMYYYY_template">
<xsl:with-param name="b1" select="."/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="convertDateToDDMMYYYY_template">
<xsl:param name="b1"/>
<xsl:value-of select="concat(substring($b1,9,2),'-',substring($b1,6,2),'-',substring($b1,1,4))"/>
</xsl:template>
</xsl:stylesheet>
It creates this output:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<uabDate>23-07-2014</uabDate>
</data>

XSLT- trying to avoid for each looping

I have the source collection as below.
<SourceCollection>
<Row1>
<Col1>Store1</Col1>
<Col2>Med1</Col2>
<Col3>Val1</Col3>
</Row1>
<Row2>
<Col1>Store1</Col1>
<Col2>Med1</Col2>
<Col3>Val2</Col3>
</Row2>
<Row3>
<Col1>Store1</Col1>
<Col2>Med2</Col2>
<Col3>Val2</Col3>
</Row3>
<Row4>
<Col1>Store2</Col1>
<Col2>Med1</Col2>
<Col3>Val4</Col3>
</Row4>
</SourceCollection>
I want the target collection as below with minimal usage of for each.
<TargetCollection>
<Store value=Store1>
<Dim value=Med1>
<DimCode>Val1</DimCode>
<DimCode>Val2</DimCode>
</Dim>
<Dim value=Med2>
<DimCode>Val3</DimCode>
</Dim>
</Store>
<Store value=Store2>
<Dim value=Med1>
<DimCode>Val4</DimCode>
</Dim>
</Store>
</TargetCollection> `
In my initial design I used 3 for-each loops on the same source collection to form the target xml as required. But,the source collection contains millions of rows, which is making my transformation to traverse the whole list (Rows)to the power of 3 times, which is hitting the system performance.
Please let me know any ideas to avoid this.
Generally, using xsl:key can help speed up transforms. Try something like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:key name="Col1" match="Col1" use="."/>
<xsl:key name="Col2byCol1" match="Col2" use="concat(../Col1, '-', .)"/>
<xsl:key name="Col3byCol1andCol2"
match="Col3" use="concat(../Col1, '-', ../Col2, '-', .)"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="SourceCollection">
<TargetCollection>
<xsl:apply-templates
select="*/Col1[generate-id() =
generate-id(key(local-name(), .)[1])]"/>
</TargetCollection>
</xsl:template>
<xsl:template match="Col1">
<xsl:variable name="Column1" select="."/>
<Store value="{.}">
<xsl:apply-templates select="../../*/Col2[generate-id() =
generate-id(key('Col2byCol1', concat($Column1, '-' ,.))[1])]"/>
</Store>
</xsl:template>
<xsl:template match="Col2">
<xsl:variable name="Column1" select="../Col1"/>
<xsl:variable name="Column2" select="."/>
<Dim value="{.}">
<xsl:apply-templates
select="../../*[Col2=$Column2]/Col3[generate-id() =
generate-id(key('Col3byCol1andCol2',
concat($Column1, '-', $Column2, '-',.)))]"/>
</Dim>
</xsl:template>
<xsl:template match="Col3">
<DimCode value="{.}">
<xsl:apply-templates />
</DimCode>
</xsl:template>
</xsl:stylesheet>