XSL selective copy [closed] - xslt

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have this XML file:
<bases>
<marker>
<name>Main</nane>
<city>Hartford</city>
<tag>current</tag>
</marker>
<marker>
<name>Secondary</nane>
<city>Boston</city>
<tag>none</tag>
</marker>
<marker>
<name>Tertiary</nane>
<city>Bedford</city>
</marker>
</bases>
I want to copy only those markers where the tag element matches "current". So, the output would be this:
<bases>
<marker>
<name>Main</nane>
<city>Hartford</city>
<tag>current</tag>
</marker>
</bases>
I'm an XSLT novice and I can't figure it out. Help, please.

You could do it simply 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 method="xml" indent="yes"/>
<!-- Remove all marker elements where element tag value is not 'current' -->
<xsl:template match="marker[not(tag='current')]"/>
<!-- Identity template -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
See it working here : https://xsltfiddle.liberty-development.net/aiynev/2

Related

How to group nearby same elements using XSLT version 1 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I need to transform an XML structure to another XML structure by grouping nearby elements using XSLT version 1 and store into a variable for later process.
My current solution:
https://xsltfiddle.liberty-development.net/naZXVF1/3
XML
<?xml version="1.0"?>
<Items>
<Parameter>1</Parameter>
<Parameter>2</Parameter>
<Menu>1</Menu>
<Parameter>3</Parameter>
<Parameter>4</Parameter>
<Menu>2</Menu>
<Menu>3</Menu>
<Parameter>5</Parameter>
<Parameter>6</Parameter>
<Parameter>7</Parameter>
<Parameter>8</Parameter>
</Items>
Expected Result
<Items>
<Parameters>
<Parameter>1</Parameter>
<Parameter>2</Parameter>
</Parameters>
<Menus>
<Menu>1</Menu>
</Menus>
...
</Items>
Either use sibling recursion or use a complicated key to identify the elements belonging together:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:key name="group" match="*[name(preceding-sibling::*[1]) = name()]"
use="generate-id(preceding-sibling::*[name() = name(current())][name(preceding-sibling::*[1]) != name()][1])"/>
<xsl:template match="Items">
<xsl:copy>
<xsl:apply-templates select="*[name(preceding-sibling::*[1]) != name()]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Items/*">
<xsl:element name="{name()}s">
<xsl:copy-of
select=". | key('group', generate-id())"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/naZXVF1/4

XPath expression of xslt [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a question about if statemnt for xslt if statement
https://xsltfiddle.liberty-development.net/bwe3c5/38
Here is a link to mine html structure and xslt code.
I do not understand why if statemnt is called only once however i have several circumstances at which statement can be called.
P.S.
Sorry for the wrong first code. Somehow it has shared the wrong one
Your <html> element is in a default namespace xmlns="http://www.w3.org/1999/xhtml". So you have to declate it in your XSLT file and on all of the elements using it:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml" version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" indent="no"/>
<?startSampleFile ?>
<!-- xq61.xsl: converts xq60.xml into xq62.xml -->
<xsl:template match="/xhtml:html/xhtml:body/xhtml:table">
<xsl:for-each select="xhtml:tr">
<xsl:value-of select="xhtml:td" />
<xsl:if test="xhtml:td[1] != 'skip'">
<xsl:call-template name="first_column" />
</xsl:if>
</xsl:for-each>
</xsl:template>
<?endSampleFile ?>
<xsl:template name="first_column">
I am here
</xsl:template>
<!-- Copy the other nodes -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
P.S.: You changed your code after I first retrieved the XSLT and XML files. This is against the rules of SO. Include the current state of your code into the question.
It's only being called 1 time because the match='/' only matches the root element in the XML.
This template is never run
<xsl:template match="#*|node()">
<xsl:apply-templates select="*"/>
</xsl:template>

Not able to loop through two child nodes simultaneously [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
input xml:
Required output format:
Using for-each we can traverse through all child tags under either "headers" or "objects"; but how to traverse through both tags and select values and combine them as shown above(just one weird example :)). By using xsl transformation. Any kind of help will be greatly appreciated...
Input (please post only text. easier to copy)
<payload>
<headers>
<header>
<name>entry1</name>
</header>
<header>
<name>entry2</name>
</header>
<header>
<name>entry3</name>
</header>
<header>
<name>entry4</name>
</header>
</headers>
<objects>
<row>
<value>1231</value>
</row>
<row>
<value>342</value>
</row>
<row>
<value>98789</value>
</row>
<row>
<value>6576</value>
</row>
</objects>
</payload>
XSLT: (there are many solutions)
<?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"/>
<xsl:template match="/">
<xsl:element name="objects">
<xsl:for-each select="payload/objects/row">
<xsl:variable name="i" select="position()"/>
<entry name="{/payload/headers/header[$i]/name}" value="{value}"></entry>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Result
<objects>
<entry name="entry1" value="1231"></entry>
<entry name="entry2" value="342"></entry>
<entry name="entry3" value="98789"></entry>
<entry name="entry4" value="6576"></entry>
</objects>

How change a Node Position using XSLT

I have a problem with the XML Web response i am getting. The Inner Loop warehouseItems have a node leadTimeCumulative and leadTimeDays coming on the first and Second Position for the FIrst time, for the Rest its coming last. I need to keep these node always in the Last position in side the Loop.
Example
<Envelope>
<Body>
<searchItemResponse>
<status>
<statusCode>Success</statusCode>
</status>
<itemList>
<itemWithWarehouses>
<item>
<originOfData>SME</originOfData>
<itemNumbers>
<shortNumber>115632</shortNumber>
<tssArticleNumber>PT0401450-T46N</tssArticleNumber>
</item>
<warehouseItems>
<leadTimeCumulative>14</leadTimeCumulative>
<leadTimeDays>14</leadTimeDays>
<warehouse>
<code>GA01</code>
</warehouse>-
<stockItem>-
<quantities>
<quantityAvailable>0</quantityAvailable>
<quantityOnHand>0</quantityOnHand>
</quantities>
</stockItem>-
<stockClass>
<group>MTO</group>
</stockClass>
</warehouseItems>-
<warehouseItems>-
<warehouse>
<code>GL01</code>
</warehouse>-
<stockItem>-
<quantities>
<quantityAvailable>0</quantityAvailable>
<quantityOnHand>0</quantityOnHand>
</quantities>
</stockItem>-
<stockClass>
<group>MTO</group>
</stockClass>
<leadTimeCumulative>14</leadTimeCumulative>
<leadTimeDays>14</leadTimeDays>
</warehouseItems>-
<warehouseItems>-
<warehouse>
<code>GS01</code></warehouse>-
<stockItem>-
<quantities>
<quantityAvailable>0</quantityAvailable>
<quantityOnHand>0</quantityOnHand>
</quantities>
</stockItem>-
<stockClass>
<group>MTO</group>
</stockClass>
<leadTimeCumulative>10</leadTimeCumulative>
<leadTimeDays>10</leadTimeDays>
</warehouseItems>-
</itemWithWarehouses>
</itemList>
</searchItemResponse>
</Body>
</Envelope>
I need any XSLT for changing the position of the node leadTimeCumulative and leadTimeDays to the Last like the rest of the Loop Structure. Please help me.
The above part is solved.
I need an improvement along with this.The tssArticleNumber node need to copy to the respective warehouse items with a different node name like "Item No". Please give me another XSLT for this. Thanks
try the following stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- this is called an identity template -->
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<!-- a template to override warehouseItems -->
<xsl:template match="warehouseItems">
<xsl:copy>
<!-- apply templates except leadTimeCumulative and leadTimeDays -->
<xsl:apply-templates select="node()[not(self::leadTimeCumulative) and not(self::leadTimeDays)]|#*"/>
<Item_No><xsl:value-of select="../item//tssArticleNumber"/></Item_No>
<xsl:apply-templates select="leadTimeCumulative"/>
<xsl:apply-templates select="leadTimeDays"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

variable inside a variable in xslt coding [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Is it possible to create a variable inside a variable in xslt??
Is the above thing possible???
Is this what you mean?
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" version="2.0">
<xsl:variable name="a">
<xsl:variable name="b" select="10"/>
<xsl:value-of select="$b"/>
</xsl:variable>
<xsl:template match="/">
<xsl:value-of select="$a"/>
</xsl:template>
</xsl:stylesheet>
The answer is yes, but the inner variable is in scope only within the definition of the outer variable. So if the definition of the outer variable calls for some complex expression that you'd like to store in a temporary (possibly for debugging purposes), then this is a way to do it.
The answer is: yes. This stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:variable name="vOuter">
<xsl:variable name="vInner">
<xsl:value-of select="'Content'"/>
</xsl:variable>
<xsl:value-of select="concat('Some ',$vInner)"/>
</xsl:variable>
<xsl:value-of select="$vOuter"/>
</xsl:template>
</xsl:stylesheet>
Output:
Some Content