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>
Related
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
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
I have an example document that looks like this
<document>
<memo>
<to>Allen</to>
<p>Hello! My name is <bold>Josh</bold></p>
<p>It's nice to meet you <bold>Allen</bold>. I hope that we get to meet up more often.</p>
<from>Josh</from>
<memo>
</document>
and this is my XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:company="http://my.company">
<xsl:output method="html"/>
<xsl:variable name="link" select="company:generate-link()"/>
<xsl:template match="/document/memo">
<h1>To: <xsl:value-of select="to"/></h1>
<xsl:for-each select="p">
<p><xsl:apply-templates select="node()" mode="paragraph"/></p>
</xsl:for-each>
<xsl:if test="from">
<p>From: <strong><xsl:value-of select="from"/></strong></p>
</xsl:if>
<xsl:copy-of select="$link"/>
</xsl:template>
<xsl:template match="bold" mode="paragraph">
<b><xsl:value-of select="."/></b>
</xsl:template>
<xsl:template match="text()" mode="paragraph">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
and the variable link contains the following example node:
When I do a copy-of out the variable link it prints out the node correctly (but obviously without any text). I want to insert it into the document and replace the text using XSLT. For example, the text could be:
View all of <xsl:value-of select="/document/memo/from"/>'s documents.
So the resulting document would look like:
<h1>To: Allen</h1>
<p>Hello! My name is <b>Josh</b></p>
<p>It's nice to meet you <b>Allen</b>. I hope that we get to meet up more often.</p>
<from>Josh</from>
View all of Josh's documents.
I have been searching the internet on how to do this but I couldn't find anything. If anyone could help I would appreciate it a lot!
Thanks,
David.
You haven't said which XSLT processor that is nor have you shown the code of that extension function to allow us to understand what it returns but based on your comment saying it returns a node you can usually process it further with templates so if you use <xsl:apply-templates select="$link"/> and then write a template
<xsl:template match="a[#href]">
<xsl:copy>
<xsl:copy-of select="#*"/>
View all of <xsl:value-of select="$main-doc/document/memo/from"/>'s documents.
</xsl:copy>
</xsl:template>
where you declare a global variable <xsl:variable name="main-doc" select="/"/> you should be able to transform the node returned from your function.
Here is my example document
<a >
<b flag='foo'>
<c/>
<d/>
</b>
</a>
I am looking to remove the "c" element only when the flag attribute on b is 'bar'.
Ie if flag='foo' the "c" element should not be removed. I do not currently have an xsl debugging tool on my pc, and have been unable to find an online tool that shows xslt error information, and have been running the following test xsl transform on http://xslttest.appspot.com/ :
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes" version="1.0" encoding="ISO-8859-1"/>
<!--Identity template to copy all content by default-->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:choose>
<xsl:when test="/a/b[#flag='bar']">
<xsl:template match="/a/b/c"/>
</xsl:when>
</xsl:choose>
</xsl:stylesheet>
When I run this I get Error: Failed to compile stylesheet. 3 errors detected.
I'm looking for help (1) fixing the problems with the xsl code and (2) anything out there like jsfiddle for xsl that can debug/test fragements of xsl code.
You can't put a choose outside a template, but you don't need to - you can use predicates in match expressions so just declare your no-op template to match the elements you want to remove:
<xsl:template match="b[#flag='bar']/c" />
or more generally, if the parent of the c element might have various names
<xsl:template match="c[../#flag='bar']" />
or
<xsl:template match="*[#flag='bar']/c" />
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