get value of variable to another template - xslt

There are references I found in the internet about the passing of variable to other template. I tried to follow all the references but, I can't get the value that I need to populate. I have this xml file:
<Item>
<Test>
<ID>123345677</ID>
</Test>
<DisplayID>99884534</DisplayID>
</Item>
I need to populate MsgId element if the DisplayID is not null, else get value from the ID. My 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="ID">
<xsl:variable name="IDV" select="substring(.,0,35)"/>
<xsl:apply-templates select="DisplayID">
<xsl:with-param name="IDP" select="$IDV"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="DisplayID">
<xsl:param name="IDP"/>
<xsl:element name="MsgId">
<xsl:choose>
<xsl:when test=".!='' or ./*">
<xsl:value-of select="substring(.,0,35)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring($IDP,0,35)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:template>
The condition if DisplayID is not null is working, however, if I remove the value of DisplayID, there's no value getting from the ID. I don't know if I doing it correctly.
Your feedback is highly appreciated.

Please try this,
Demo for references : http://xsltransform.net/ejivdHb/16
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="http://locomotive/bypass/docx" >
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Item">
<xsl:element name="MsgId">
<xsl:choose>
<xsl:when test="DisplayID !='' ">
<xsl:value-of select="substring(DisplayID , 0 ,35)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring(Test/ID,0,35)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

Since this is tagged XSLT 2.0, the match="Item" template from #TechBreak can be replaced by
<xsl:template match="Item">
<MsgId>
<xsl:value-of select="substring(
if (DisplayId != '')
then DisplayID
else Test/ID, 1 ,35)"/>
</MsgId>
</xsl:template>
(Note character counting starts from 1)

Related

XSLT mapping to remove double quotes which has PIPE delimited symbol inside

Experts, i need to write XSLT 1.0 code to eliminate the Pipe delimited symbol inside double quotes and also need to remove those double quotes..
Input:
<?xml version="1.0" encoding="utf-8"?>
<ns:MT_FILE>
<LN>
<LD>EXTRACT|"28|53"|1308026.7500|1176</LD>
</LN>
<LN>
<LD>DETAIL|1176|"LOS LE|OS PARRILLA"|Y|R||||<LD>
</LN>
</ns:MT_FILE>
** Desired Output:**
<?xml version="1.0" encoding="utf-8"?>
<ns:MT_FILE>
<LN>
<LD>EXTRACT|2853|1308026.7500|1176</LD>
</LN>
<LN>
<LD>DETAIL|1176|LOS LE OS PARRILLA|Y|R||||<LD>
</LN>
</ns:MT_FILE>
** XSLT I used is below:**
<?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="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*/text()">
<xsl:value-of select="translate(., '\"', '')"/>
</xsl:template>
</xsl:stylesheet>
This XSLT removing all the double quotes from my input field, please assist here..
If it can be assumed that quotes will always come in pairs, you could do:
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:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:call-template name="process">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:template>
<xsl:template name="process">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, '"')">
<xsl:value-of select="substring-before($text, '"')"/>
<xsl:value-of select="translate(substring-before(substring-after($text, '"'), '"'), '|', '')"/>
<xsl:call-template name="process">
<xsl:with-param name="text" select="substring-after(substring-after($text, '"'), '"')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
As you tagged as EXSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:regexp="http://exslt.org/regular-expressions"
exclude-result-prefixes="regexp">
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="LD/text()">
<xsl:value-of select="regexp:replace(., '(")([^|]+)\|([^"]+)(")', 'g', '$2$3')"/>
</xsl:template>
</xsl:stylesheet>

Conditionally modifying text in multiple identically named children

I'm trying to conditionally modify the content of some XML. Am element has multiple identically named children, which I want to modify based on the text content. For example, I have the below XML:
<first>
<second>
<third>alice</third>
<third>bob</third>
<third>charlie</third>
</second>
</first>
Which I'd like to transform into:
<first>
<second>
<third>xavier</third>
<third>yvonne</third>
<third>charlie</third>
</second>
</first>
I had thought the below xsl would work, but it doesn't (I suspect for a couple of reasons). What am I doing wrong?
<?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" encoding="UTF-8" omit-xml-declaration="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/first/second/third/">
<xsl:choose>
<xsl:when test="contains(text(), 'alice')">
<xsl:text>xavier</xsl:text>
</xsl:when>
<xsl:when test="contains(text(), 'bob')">
<xsl:text>Yvonne</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="text()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Two things you're doing wrong:
You have a slash at the end of this XPath /first/second/third/. Syntactically, it's not legal to have a slash at the end of an XPath, and you don't need one here.
You have a template that's supposed to match third elements (basically providing a substitute for what they once were), but you're not copying the elements. You're just replacing them with text, which means you'd have a result like:
<first>
<second>xavieryvonnecharlie</second>
</first>
In order to get your attempt to work, modifying the template to this should be sufficient:
<xsl:template match="/first/second/third">
<xsl:copy>
<xsl:choose>
<xsl:when test="contains(text(), 'alice')">
<xsl:text>xavier</xsl:text>
</xsl:when>
<xsl:when test="contains(text(), 'bob')">
<xsl:text>Yvonne</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="text()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
However can do this much more cleanly by having templates to match the text nodes you want to replace:
<?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" encoding="UTF-8" omit-xml-declaration="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="third/text()[. = 'alice']">xavier</xsl:template>
<xsl:template match="third/text()[. = 'bob']">yvonne</xsl:template>
</xsl:stylesheet>
When run on your sample input, the result is:
<first>
<second>
<third>xavier</third>
<third>yvonne</third>
<third>charlie</third>
</second>
</first>

correct xsl template matching syntax

I'm learning XSL and hope to get some help. I want to extract part of the following datasets.xml and output them as tab delimited texts:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<listDatasetsResponse xmlns="http://www.algorithmics.com/schema">
<status>OK</status>
<datasets size="31">
<dataset>
<id>stress_20150910_20150910_160259</id>
<basedOn>Mimm_20150910_20150910_030922</basedOn>
<active>false</active>
<sandbox>true</sandbox>
<ownedBy>admin</ownedBy>
<createdOn>2015-09-10T16:04:24.199-04:00</createdOn>
<createdBy>rtcesupp</createdBy>
<evaluated>true</evaluated>
<stopped>false</stopped>
</dataset>
<dataset>
<id>imm_20150910_20150910_140315</id>
<basedOn>Mimm_20150910_20150910_030922</basedOn>
<active>true</active>
<sandbox>true</sandbox>
<ownedBy>admin</ownedBy>
<createdOn>2015-09-10T14:04:42.696-04:00</createdOn>
<createdBy>rtcesupp</createdBy>
<evaluated>true</evaluated>
<stopped>false</stopped>
</dataset>
</datasets>
</listDatasetsResponse>
here is the XSL I used:
$ vi dataset.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="iso-8859-1"/>
<xsl:strip-space elements="*" />
<xsl:template match="/listDatasetsResponse/datasets/dataset">
<xsl:value-of select="id"/><xsl:text> </xsl:text>
<xsl:choose>
<xsl:when test="active='true'">
<xsl:text>ACTIVE</xsl:text>
</xsl:when>
<xsl:when test="stopped='true'">
<xsl:text>,STOPPED</xsl:text>
</xsl:when>
<xsl:when test="evaluated='true'">
<xsl:text>,EVALUATED</xsl:text>
</xsl:when>
</xsl:choose>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
I expected the result of:
stress_20150910_20150910_160259 EVALUATED
imm_20150910_20150910_140315 ACTIVE,EVALUATED
but what I got is:
OKstress_20150910_20150910_160259Mimm_20150910_20150910_030922falsetrueadmin2015-09-10T16:04:24.199-04:00rtcesupptruefalseimm_20150910_20150910_140315Mimm_20150910_20150910_030922truetrueadmin2015-09-10T14:04:42.696-04:00rtcesupptruefalse
It seemed like the XSL stylesheet was ignored. Could some one point me to correct XSL template matching syntax?
The fundamental problem in your XSL is that none of the XPath expression being used match the element in the source XML. Notice your XML has default namespace declared at the root element :
xmlns="http://www.algorithmics.com/schema"
Descendant elements without explicit prefix and without local default namespace inherits ancestor default namespace implicitly. To match element in namespace, simply declare a prefix that point to the namespace-uri and use that prefix in your XPath expressions, for example :
<xsl:stylesheet .....
xmlns:d="http://www.algorithmics.com/schema">
.....
<xsl:template match="/d:listDatasetsResponse/d:datasets/d:dataset">
<xsl:value-of select="d:id"/><xsl:text> </xsl:text>
.....
</xsl:template>
</xsl:stylesheet>
How about ...
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="http://www.algorithmics.com/schema"
version="1.0" >
<xsl:output method="text" encoding="utf-8"/>
<xsl:strip-space elements="*" />
<xsl:template match="#*|node()" />
<xsl:template match="/">
<xsl:apply-templates select="a:listDatasetsResponse/a:datasets/a:dataset" />
</xsl:template>
<xsl:template match="a:dataset">
<xsl:value-of select="a:id"/>
<xsl:text> </xsl:text>
<xsl:apply-templates select="a:active|a:stopped|a:evaluated" />
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="a:active[.='true']">
<xsl:if test="preceding-sibling::a:active[.='true']|
preceding-sibling::a:stopped[.='true']|
preceding-sibling::a:evaluated[.='true']">,</xsl:if>
<xsl:text>ACTIVE</xsl:text>
</xsl:template>
<xsl:template match="a:stopped[.='true']">
<xsl:if test="preceding-sibling::a:active[.='true']|
preceding-sibling::a:stopped[.='true']|
preceding-sibling::a:evaluated[.='true']">,</xsl:if>
<xsl:text>STOPPED</xsl:text>
</xsl:template>
<xsl:template match="a:evaluated[.='true']">
<xsl:if test="preceding-sibling::a:active[.='true']|
preceding-sibling::a:stopped[.='true']|
preceding-sibling::a:evaluated[.='true']">,</xsl:if>
<xsl:text>EVALUATED</xsl:text>
</xsl:template>
</xsl:stylesheet>
... or this version ...
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="http://www.algorithmics.com/schema"
version="1.0" >
<xsl:output method="text" encoding="utf-8"/>
<xsl:strip-space elements="*" />
<xsl:template match="#*|node()" />
<xsl:template match="/">
<xsl:apply-templates select="a:listDatasetsResponse/a:datasets/a:dataset" />
</xsl:template>
<xsl:template match="a:dataset">
<xsl:value-of select="a:id"/>
<xsl:text> </xsl:text>
<xsl:apply-templates select="a:active|a:stopped|a:evaluated" />
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="a:active[.='true'] | a:stopped[.='true'] | a:evaluated[.='true']">
<xsl:if test="preceding-sibling::a:active[.='true']|
preceding-sibling::a:stopped[.='true']|
preceding-sibling::a:evaluated[.='true']">,</xsl:if>
<xsl:value-of select="translate( local-name(), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')" />
</xsl:template>
</xsl:stylesheet>

Error while assigning XML attribute value in XSLT

My input XML is as below:
<MessageOutput Status="2">
<Source>External</Source>
<Error>Server not reachable</Error>
<LineNo>0</LineNo>
</MessageOutput>
My requirement is to write XSLT and check if <Error> tag has value "Server not reachable" and if yes then change the Status attribute value to "3".
I wrote below code but getting error:
"XSLT Error: Open quote is expected for attribute "{1}" associated
with an element type "Status"."
Please assist.
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="ISO-8859-1"/>
<xsl:variable name="Des" select="'Server not reachable'"/>
<xsl:variable name="Err" select="MessageOutput/Error"/>
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="#*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="/MessageOutput">
<xsl:choose>
<xsl:when test="$Des=$Err">
<MessageOutput Status="3">
<xsl:apply-templates/>
</MessageOutput>
</xsl:when>
<xsl:otherwise>
<MessageOutput Status=<xsl:value-of select="#Status"/>>
<xsl:apply-templates/>
</MessageOutput>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Try this:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="ISO-8859-1"/>
<xsl:variable name="Des" select="'Server not reachable'"/>
<xsl:variable name="Err" select="MessageOutput/Error"/>
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="/MessageOutput">
<xsl:choose>
<xsl:when test="$Des=$Err">
<MessageOutput Status="3">
<xsl:apply-templates/>
</MessageOutput>
</xsl:when>
<xsl:otherwise>
<MessageResult Status="{#Status}">
<xsl:apply-templates/>
</MessageResult>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Or shortly:
<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:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="MessageOutput[Error='Server not reachable']/#Status">
<xsl:attribute name="Status">3</xsl:attribute>
</xsl:template>
</xsl:stylesheet>

XSL transformation failed : Expected QName

when I am trying to transform an XMl using below stylesheet, the transformation is failing with error Expected QName. What exactly is missing in the styelesheet, pasted below?
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dp="http://www.datapower.com/extensions" extension-element-prefixes="dp" exclude-result-prefixes="dp">
<xsl:output method="xml" indent="yes" version="1.0"/>
<xsl:variable name="origo-svc-augmented" select="'Y'"/>
<xsl:variable name="origo-svc-ns" select="'http://www.origoservices.com'"/>
<xsl:template match="node()">
<xsl:variable name="namespace" select="namespace-uri(.)"/>
<xsl:choose>
<!--xsl:when test="$namespace = ''
or ($origo-svc-augmented='Y' and $namespace=$origo-svc-ns)"-->
<xsl:when test="$origo-svc-augmented = 'N'">
<xsl:element name="{local-name()}">
<xsl:copy-of select="namespace::*[not(namespace-uri()=$origo-svc-ns)]"/>
<xsl:apply-templates select="#*|node()|comment()|processing-instruction()|text()"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{local-name()}" namespace="{$namespace}">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="#*|node()|comment()|processing-instruction()|text()"/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
UPDATE: After applying the suggestion from the answer provided, the stylesheet is working fine, updated stylesheet is as below
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dp="http://www.datapower.com/extensions" extension-element-prefixes="dp" exclude-result-prefixes="dp">
<xsl:output method="xml" indent="yes" version="1.0"/>
<xsl:variable name="origo-svc-augmented" select="'Y'"/>
<xsl:variable name="origo-svc-ns" select="'http://www.origoservices.com'"/>
<xsl:template match="*">
<xsl:variable name="namespace" select="namespace-uri(.)"/>
<xsl:choose>
<!--xsl:when test="$namespace = ''
or ($origo-svc-augmented='Y' and $namespace=$origo-svc-ns)"-->
<xsl:when test="$origo-svc-augmented = 'Y'">
<xsl:element name="{local-name()}">
<xsl:copy-of select="namespace::*[not(namespace-uri()=$origo-svc-ns)]"/>
<xsl:apply-templates select="#*|node()|comment()|processing-instruction()|text()"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{local-name()}" namespace="{$namespace}">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="#*|node()|comment()|processing-instruction()|text()"/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="#*|comment()|processing-instruction()|text()">
<xsl:copy>
<xsl:apply-templates select="#*|node()|comment()|processing-instruction()|text()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I haven't tried it, but I think it may be because you're matching on node() (i.e. element, text node, processing instruction comment), but then trying to create an element with the name of that possibly non-element node. QName refers to a valid XML name, and I presume a text node, for example, wouldn't have one (when you call local-name()). You could change your template match to be *, and add another template for the other types.