The two sample xmls are given below:
xml1:
<Root>
<Child1/>
<Child2/>
<Child3/>
</Root>
xml2:
<Root>
<Child0>xml2value</Child0>
<Child2/>
<Child3>xml2value</Child3>
<Child4>xml2value</Child4>
</Root>
I have got these two xmls in two variables. Now I want to filter from xml2 those elements which do not exist in xml1, i.e., the resulting variable should look like below:
<Child0>xml2value</Child0>
<Child4>xml2value</Child4>
How can it be done with xslt?
XSLT 2.0:
<xsl:key name="el-by-name" match="Root/*" use="node-name(.)"/>
<xsl:variable name="xml1" select="document('file1.xml')"/>
<xsl:variable name="xml2" select="document('file2.xml')"/>
<xsl:copy-of select="$xml2/Root/*[not(key('el-by-name', node-name(.), $xml1))]"/>
With XSLT 1.0:
<xsl:key name="el-by-name" match="Root/*" use="name()"/>
<xsl:variable name="xml1" select="document('file1.xml')"/>
<xsl:variable name="xml2" select="document('file2.xml')"/>
<xsl:for-each select="$xml2/Root/*">
<xsl:variable name="child" select="."/>
<xsl:for-each select="$xml1">
<xsl:if test="not(key('el-by-name', name($child)))">
<xsl:copy-of select="$child"/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
I have solved the problem with the below code:
<xsl:variable name="output">
<xsl:for-each select="$xml2/Root/*">
<xsl:variable name="cur" select="local-name(.)"/>
<xsl:if test="not($xml1/Root/*[local-name(.)=$cur])">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
Related
I have a problem getting data from a node, when I'm using xml:choose and xml:when. I only get the result NaN or the value from the main xml-file, even if.
Part of the XML-file:
<?xml version="1.0" encoding="UTF-8"?>
<Job xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<Invoice>
<InvoiceLine>
<LineNo>1</LineNo>
<QtyInSecondUnit>56</QtyInSecondUnit>
<Quantity>56</Quantity>
<CustTaric>
<StatNo>34011100</StatNo>
<IssuingCountry>GB</IssuingCountry>
</CustTaric>
</InvoiceLine>
<InvoiceLine>
<LineNo>2</LineNo>
<QtyInSecondUnit>22</QtyInSecondUnit>
<Quantity>0</Quantity>
<CustTaric>
<StatNo>44152020</StatNo>
<IssuingCountry>GB</IssuingCountry>
</CustTaric>
</InvoiceLine>
</Invoice>
</Job>
Part of the XSLT-file:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="Job/Invoice"/>
</xsl:template>
<xsl:template match="Job/Invoice/InvoiceLine">
<xsl:apply-templates select="QtyInSecondUnit"/>
<xsl:apply-templates select="Quantity"/>
<xsl:apply-templates select="CustTaric/StatNo"/>
</xsl:template>
<xsl:template match="QtyInSecondUnit">
<xsl:choose>
<xsl:when test="/Job/Invoice/InvoiceLine/CustTaric/StatNo = '44152020'">
<xsl:value-of select="number(translate(Job/Invoice/InvoiceLine/NetMass,',','.')) div 25"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="Quantity">
<xsl:choose>
<xsl:when test="/Job/Invoice/InvoiceLine/CustTaric/StatNo = '44152020'">
<xsl:value-of select="'0'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="CustTaric/StatNo">
<xsl:value-of select="."/>
</xsl:template>
Hope there is somebody how can tell me (the noob) what I'm doing wrong here?
Here's the line producing NaN
<xsl:value-of select="number(translate(Job/Invoice/InvoiceLine/NetMass,',','.')) div 25"/>
There are two problems (one of which is probably where you have over-simplified your XML)
The xpath expression you are using will be relative to the current node you are positioned on. There is no Job element under the current QtyInSecondUnit element
There is no NetMass element in your XML in your question
Assuming NetMass does exist in your actual XML, and is a child of the parent InvoiceLine the expression you want is this
<xsl:value-of select="number(translate(../NetMass,',','.')) div 25"/>
There is also an issue with your xsl:when (possibly)
<xsl:when test="/Job/Invoice/InvoiceLine/CustTaric/StatNo = '44152020'">
This will test for any CustTaric/StatNo anywhere in the document. Perhaps you only want to test for the one in the current InvoiceLine? If so, do this...
<xsl:when test="../CustTaric/StatNo = '44152020'">
Note, you could rewrite your XSLT to put the logic in template matches, rather than xsl:choose
Try this XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Job/Invoice/InvoiceLine">
<xsl:apply-templates select="QtyInSecondUnit"/>
<xsl:apply-templates select="Quantity"/>
<xsl:apply-templates select="CustTaric/StatNo"/>
</xsl:template>
<xsl:template match="InvoiceLine[CustTaric/StatNo = '44152020']/QtyInSecondUnit">
<xsl:value-of select="number(translate(../NetMass,',','.')) div 25"/>
</xsl:template>
<xsl:template match="QtyInSecondUnit">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="InvoiceLine[CustTaric/StatNo = '44152020']/Quantity">
<xsl:value-of select="'0'"/>
</xsl:template>
<xsl:template match="Quantity">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="CustTaric/StatNo">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
Strictly speaking, the templates that just do <xsl:value-of select="."/> can be removed, as XSLT's built-in templates will do exactly the same thing if there is no matching template in the XSLT.
I need to convert the following content
<QTLS_ITEM>
<ID>123</ID>
<ID1>1345</ID1>
<SERAIL_NUMBER>102697704257</SERAIL_NUMBER>
<PROD_NAME>upgrade</PROD_NAME>
</QTLS_ITEM>
<QTLS_ITEM>
<ID>123</ID>
<ID1>1345</ID1>
<SERAIL_NUMBER>102697704257</SERAIL_NUMBER>
<PROD_NAME>Plugin</PROD_NAME>
</QTLS_ITEM>
<QTLS_ITEM>
<ID>123</ID>
<ID1>1345</ID1>
<SERAIL_NUMBER>102697704257</SERAIL_NUMBER>
<PROD_NAME>License</PROD_NAME>
</QTLS_ITEM>
This is a looping element type.<QTLS_ITEM> is a repeating element. For each element I have to concatenate those two fields and get the value as below.
I want to transform it into a single Element like
<Item_description>
102697704257 upgrade
102697704257 Plugin
102697704257 License
<Item_Description>
Which means I need to concatenate both the SERAIL_NUMBER and PROD_NAME.
Can anyone help on this?
<xsl:template name="string-join">
<xsl:param name="nodes"/>
<xsl:param name="delimiter"/>
<xsl:for-each select="$nodes">
<xsl:value-of select="."/>
<xsl:if test="$nodes[position()!=last()-1]">
<xsl:value-of select="$delimiter"/>
</xsl:if>
</xsl:for-each>
</xsl:template>
I am using this to get the Serial numbers separated by comma. But I want to concatenate and get the result in the above format.
The answer for my case is
<xsl:template name="join">
<xsl:param name="list"/>
<xsl:param name="separator"/>
<xsl:for-each select="db:SERAIL_NUMBER | db:PROD_NAME">($list value)
<xsl:value-of select="."/>
<xsl:if test="position() != last()">
<xsl:value-of select="$separator"/>
</xsl:if>
</xsl:for-each>
</xsl:template>
The input that you show us is not well-formed XML, because it does not have a single root element. Given a well-formed XML input such as:
XML
<root>
<QTLS_ITEM>
<SERAIL_NUMBER>102697704257</SERAIL_NUMBER>
<PROD_NAME>upgrade</PROD_NAME>
</QTLS_ITEM>
<QTLS_ITEM>
<SERAIL_NUMBER>102697704257</SERAIL_NUMBER>
<PROD_NAME>Plugin</PROD_NAME>
</QTLS_ITEM>
<QTLS_ITEM>
<SERAIL_NUMBER>102697704257</SERAIL_NUMBER>
<PROD_NAME>License</PROD_NAME>
</QTLS_ITEM>
</root>
you can do simply:
XSLT 2.0
<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="/root">
<Item_description>
<xsl:for-each select="QTLS_ITEM">
<xsl:value-of select="SERAIL_NUMBER, PROD_NAME" />
<xsl:text>
</xsl:text>
</xsl:for-each>
</Item_description>
</xsl:template>
</xsl:stylesheet>
to get:
Result
<?xml version="1.0" encoding="UTF-8"?>
<Item_description>102697704257 upgrade
102697704257 Plugin
102697704257 License
</Item_description>
This has worked out in my case
<xsl:template name="join">
<xsl:param name="list"/>
<xsl:param name="separator"/>
<xsl:for-each select="db:SERAIL_NUMBER|db:PROD_NAME">($list value)
<xsl:value-of select="."/>
<xsl:if test="position() != last()">
<xsl:value-of select="$separator"/>
</xsl:if>
</xsl:for-each>
</xsl:template>
Silly, simple question. When I output text, it still get the tabs based on my formatted/indented XSL structure. How do I instruct the transformer to ignore the spacing in the stylesheet while still keeping it neatly formatted?
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:apply-templates select="Foo/Bar"></xsl:apply-templates>
</xsl:template>
<xsl:template match="Bar">
<xsl:for-each select="AAA"><xsl:for-each select="BBB"><xsl:value-of select="Label"/>|<xsl:value-of select="Value"/><xsl:text>
</xsl:text></xsl:for-each></xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Produces output line by line with no tabs:
SomeLabel|SomeValue
SomeLabel|SomeValue
SomeLabel|SomeValue
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:apply-templates select="Foo/Bar"></xsl:apply-templates>
</xsl:template>
<xsl:template match="Bar">
<xsl:for-each select="AAA">
<xsl:for-each select="BBB">
<xsl:value-of select="Label"/>|<xsl:value-of select="Value"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Produces output with tabs:
SomeLabel|SomeValue
SomeLabel|SomeValue
SomeLabel|SomeValue
Update:
Adding this does not fix it:
<xsl:output method="text" indent="no"/>
<xsl:strip-space elements="*"></xsl:strip-space>
This is contrived, but you can imagine the XML looks like this:
<Foo>
<Bar>
<AAA>
<BBB>
<Label>SomeLabel1</Label>
<Value>SomeValue1</Value>
</BBB>
<BBB>
<Label>SomeLabel2</Label>
<Value>SomeValue2</Value>
</BBB>
<BBB>
<Label>SomeLabel3</Label>
<Value>SomeValue3</Value>
</BBB>
</AAA>
</Bar>
</Foo>
What you could try is wrapping all your current text nodes in xsl:text. For example, try this
<xsl:for-each select="BBB">
<xsl:value-of select="Label"/>
<xsl:text>|</xsl:text>
<xsl:value-of select="Value"/>
<xsl:text>|</xsl:text>
</xsl:for-each>
Alternatively, you could make use of the concat function.
<xsl:for-each select="BBB">
<xsl:value-of select="concat(Label, '|')"/>
<xsl:value-of select="concat(Value, '|')"/>
</xsl:for-each>
You could even combine the two statements into one if you wanted
<xsl:for-each select="BBB">
<xsl:value-of select="concat(Label, '|', Value, '|')"/>
</xsl:for-each>
EDIT: If you prefer not to enter the separator | so many times, you make use of template matching to output the fileds. First, replace the value-of with apply-templates like so
<xsl:for-each select="BBB">
<xsl:apply-templates select="Label"/>
<xsl:apply-templates select="Value"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
Then you would have one specific template to match Label, where you wouldn't need to output the separator, and another more generic template matching any child of BBB
<xsl:template match="BBB/Label" priority="1">
<xsl:value-of select="." />
</xsl:template>
<xsl:template match="BBB/*">
<xsl:text>|</xsl:text><xsl:value-of select="." />
</xsl:template>
(The priority here is needed to ensure Label is matched by the first template, and not the general one). Of course, you could also not do apply-templates on Label in this case, and just do xsl:value-of for that one.
Furthermore, if the fields were being output in the order they appear in the XML, you could simplify the for-each to just this
<xsl:for-each select="BBB">
<xsl:apply-templates />
<xsl:text>
</xsl:text>
</xsl:for-each>
I have this...
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exslt="http://exslt.org/common">
<xsl:variable name="data">
<root>
<test>1000</test>
<test>2000</test>
<test>3000</test>
</root>
</xsl:variable>
<xsl:template match="/">
<xsl:for-each select="$data/root/test">
<xsl:for-each select="."/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
And I thought that with XSLT 1.1 that the $data variable would be treated as a node-set and that therefore standard XSLT stuff - like for-each - should work.
I don't get an error, but I get no output - it's as though the $data nodeset is completely empty.
I've also tried this
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exslt="http://exslt.org/common">
<xsl:variable name="data">
<root>
<test>1000</test>
<test>2000</test>
<test>3000</test>
</root>
</xsl:variable>
<xsl:template match="/">
<xsl:for-each select="exslt:node-set($data)/root/test">
<xsl:for-each select="."/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
With the same results. (Infact, I've done this before with no problems)
I'm using Saxon.
What am I missing? (I'm not in a position to use XSLT 2.0 by the way)
Thanks
You wrote:
I don't get an error, but I get no
output
The problem is here:
<xsl:for-each select="."/>
Your question:
What am I missing?
Answer: You are missing your template.
<xsl:template match="/">
<xsl:for-each select="exslt:node-set($data)/root/test">
<xsl:for-each select="."/>
</xsl:for-each> </xsl:template>
The error is in the following (empty) instruction:
<xsl:for-each select="."/>
This (probably) has to be:
<xsl:value-of select="."/>
or
<xsl:copy-of select="."/>
or ... ?
I have a pretty flat XML structure that I need to reorder into categorised sections and, for the life of me, I can't figure out how to do it in XSLT (not that I'm by any means an expert.)
Basically, the original XML looks kinda like:
<things>
<thing>
<value>one</value>
<type>a</type>
</thing>
<thing>
<value>two</value>
<type>b</type>
</thing>
<thing>
<value>thee</value>
<type>b</type>
</thing>
<thing>
<value>four</value>
<type>a</type>
</thing>
<thing>
<value>five</value>
<type>d</type>
</thing>
</things>
And I need to output something like:
<data>
<a-things>
<a>one</a>
<a>four</a>
</a-things>
<b-things>
<b>two</b>
<b>three</b>
</b-things>
<d-things>
<d>five</d>
</d-things>
</data>
Note that I can't output <c-things> if there aren't any <c> elements, but I do know ahead of time what the complete list of types is, and it's fairly short so handcoding templates for each type is definitely possible. It feels like I could probably hack something together using <xsl:if> and <xsl:for-each> but it also feels like there must be a more ... 'templatey' way to do it. Can anyone help?
Cheers.
As you are using Saxon, use the native XSLT 2.0 grouping.
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="things">
<data>
<xsl:for-each-group select="thing" group-by="type">
<xsl:element name="{concat(current-grouping-key(),'-things')}">
<xsl:for-each select="current-group()">
<xsl:element name="{current-grouping-key()}">
<xsl:value-of select="value" />
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:for-each-group>
</data>
</xsl:template>
</xsl:stylesheet>
In XSLT 1.0 you can group with keys. This approach is called Muenchian Grouping.
The xsl:key defines an index containing thing elements, grouped by the string value of their type element. Function key() returns all nodes from the key with the specified value.
The outer xsl:for-each selects the thing elements that are the first returned by key() for their value.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:key name="thing" match="thing" use="type" />
<xsl:template match="things">
<data>
<xsl:for-each select="thing[generate-id(.)=generate-id(key('thing',type)[1])]">
<xsl:element name="{concat(type,'-things')}">
<xsl:for-each select="key('thing',type)">
<xsl:element name="{type}">
<xsl:value-of select="value" />
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</data>
</xsl:template>
</xsl:stylesheet>
The generic solution is to use an XSL key:
<xsl:key name="kThingByType" match="thing" use="type" />
<xsl:template match="things">
<xsl:copy>
<xsl:apply-templates select="thing" mode="group">
<xsl:sort select="type" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="thing" mode="group">
<xsl:variable name="wholeGroup" select="key('kThingByType', type)" />
<xsl:if test="generate-id() = generate-id($wholeGroup[1])">
<xsl:element name="{type}-thing">
<xsl:copy-of select="$wholeGroup/value" />
</xsl:element>
</xsl:if>
</xsl:template>
The above yields:
<things>
<a-thing>
<value>one</value>
<value>four</value>
</a-thing>
<b-thing>
<value>two</value>
<value>thee</value>
</b-thing>
<d-thing>
<value>five</value>
</d-thing>
</things>
In XSLT 2, you can do this very elegantly. Say you have a template for formatting each thing before it is wrapped in an <a> element:
<xsl:template match="thing" mode="format-thing">
<xsl:value-of select="value/text()"/>
</xsl:template>
Then you can apply that to each thing of some $type to build the <a-things> elements via a function:
<xsl:function name="my:things-group" as="element()">
<xsl:param name="type" as="xs:string"/>
<xsl:param name="things" as="element(thing)*"/>
<xsl:element name="{ concat($type, '-things') }">
<xsl:for-each select="$things[type/text() eq $type]">
<xsl:element name="{ $type }">
<xsl:apply-templates select="." mode="format-thing"/>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:function>
Then you can call that function for each unique type (a, b, d in your sample input) to build the entire output and you're done:
<xsl:template match="/">
<data>
<xsl:sequence select="
for $type in distinct-values(things/thing/type/text())
return my:things-group($type, /things/thing)
"/>
</data>
</xsl:template>
Of course, asking the question made it obvious...
My solution does use an <xsl:if>, but I can't see how it couldn't now I think about it. My solution looks basically like:
<xsl:if test="/things/thing/type = 'a'">
<a-things>
<xsl:apply-templates select="/things/thing[type='a']" mode="a" />
</a-things>
</if>
<xsl:template match="/things/thing[type='a']" mode="a">
<a><xsl:value-of select="value"/>
</xsl:template>
And repeat for the other types. I've coded it up, and it seems to work just fine.
<a-things>
<xsl:for-each select="thing[type = 'a']">
<a><xsl:value-of select="./value" /></a>
</xsl:for-each>
</a-things>
If you want to get really snazzy, replace the <a-things> and the predicate with parameters and use attribute value templates:
<xsl:param name="type" />
<xsl:element name="{$type}-things">
<xsl:for-each select="thing[type = $type]">
<xsl:element name="{$type}"><xsl:value-of select="./value" /></xsl:element>
</xsl:for-each>
</xsl:element>
And using grouping, you can do it without the if:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="things">
<data>
<xsl:for-each select="thing[not(type=preceding-sibling::thing/type)]">
<xsl:variable name="type"><xsl:value-of select="type" /></xsl:variable>
<xsl:element name="concat($type, '-things')">
<xsl:for-each select="../thing[type=$type]">
<xsl:element name="$type">
<xsl:value-of select="value" />
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</data>
</xsl:template>
</xsl:stylesheet>