XSLT 1.0 - how to check when condition for string - xslt

I am trying to conditional check on the input xml file and place the value.
input xml:
<workorder>
<newwo>1</newwo>
</workorder>
If newwo is 1, then I have to set in my output as "NEW" else "OLD"
Expected output is:
newwo: "NEW"
my xslt is:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
<xsl:template match="/">
<xsl:apply-templates select="NEWWO" />
</xsl:template>
<xsl:template match="/NEWWO">
<xsl:text>{
newwo:"
</xsl:text>
<xsl:choose>
<xsl:when test="NEWWO != '0'">NEW</xsl:when>
<xsl:otherwise>OLD</xsl:otherwise>
</xsl:choose>
<xsl:text>"
}</xsl:text>
</xsl:template>
Please help me. Thanks in advance!

I see a number of reasons you aren't getting output.
The xpaths are case sensitive. NEWWO is not going to match newwo.
You match / and then apply-templates to newwo (case fixed), but newwo doesn't exist at that context. You'll either have to add */ or workorder/ to the apply-templates (like select="*/newwo") or change / to /* or /workorder in the match.
You match /newwo (case fixed again), but newwo is not the root element. Remove the /.
You do the following test: test="newwo != '0'", but newwo is already the current context. Use . or normalize-space() instead. (If you use normalize-space(), be sure to test against a string. (Quote the 1.))
Here's an updated example.
XML Input
<workorder>
<newwo>1</newwo>
</workorder>
XSLT 1.0
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:template match="/*">
<xsl:apply-templates select="newwo" />
</xsl:template>
<xsl:template match="newwo">
<xsl:text>{
newwo: "</xsl:text>
<xsl:choose>
<xsl:when test=".=1">NEW</xsl:when>
<xsl:otherwise>OLD</xsl:otherwise>
</xsl:choose>
<xsl:text>"
}</xsl:text>
</xsl:template>
</xsl:stylesheet>
Output
{
newwo: "NEW"
}

You try it as below
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0">
<xsl:template match="/">
<xsl:choose>
<xsl:when test="/workorder/newwo = 1">
<xsl:text disable-output-escaping="no"> newwo:New</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text disable-output-escaping="no"> newwo:Old</xsl:text> </xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

Related

XSLT Need to Limit Return of Multiple Instances in XML File to 18 Characters

I currently have the following code to combine multiple instances of Ustrd into one returned value:
<Ustrd>
<xsl:value-of select="a:RmtInf/a:Ustrd"/>
</Ustrd>
This returns:
<Ustrd>Item-1 Item-2 Item-3</Ustrd>
The problem is that I need to limit this to 18 characters, and the substring function does not work with a sequence of items.
Tried:
<Ustrd>
<xsl:value-of select="substring(a:RmtInf/a:Ustrd, 1, 18"/>
</Ustrd>
Expected Result:
<Ustrd>Item-1 Item-2 Item</Ustrd>
Use string-join first e.g. substring(string-join(a:RmtInf/a:Ustrd, ' '), 1, 18). In XPath 3.1 you can also write that as a:RmtInf/a:Ustrd => string-join(' ') => substring(1, 18).
Here's a way this could be done in XSLT 1.0.
<?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"/>
<xsl:template match="/">
<Ustrd>
<xsl:variable name="temp">
<xsl:for-each select="RmtInf/Ustrd">
<xsl:value-of select="."/>
<xsl:if test="position()!=last()">
<xsl:value-of select="' '"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="substring($temp,1,18)"/>
</Ustrd>
</xsl:template>
</xsl:stylesheet>
(Only need to add your namespace.)
See it working here: https://xsltfiddle.liberty-development.net/pPgzCL4

Otherwise-statement in XSLT-choose is not applied/working

I have an XML-document with a type-node whose value is either "1" or "2":
<MyDoc>
<foo>
<bar>
<type>2</type>
</bar>
</foo>
</MyDoc>
I want to set a variable typeBool depending on the value of the type-node, if it is "1" it should be set to false, if it's "2" to true.
With the XSLT-choose-Element it should be possible to test for the current value and set typeBool according to the outcome.
I'm trying to do this with the following construct in XSLT 2.0, but I'm puzzled that the "otherwise"-path is not applied and I get an error that typeBool is not created:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:variable name="type" select="/MyDoc/foo/bar/type/text()"/>
<xsl:choose>
<xsl:when test="$type = '2'">
<xsl:variable name="typeBool">true</xsl:variable>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="typeBool">false</xsl:variable>
</xsl:otherwise>
</xsl:choose>
<h1><b><xsl:value-of select="$typeBool"/></b></h1>
</xsl:transform>
This is the transformation error I get:
error during xslt transformation:
Source location: line 0, col 0 Description:
No variable with name typeBool exists
As you currently present your problem, an xsl:choose is not needed and it unnecessarily complicates your XSLT code. Your actual problem might be more intricate though.
You can write a template that matches the element you are interested in (for instance, type elements) and then simply select the value of a comparison that will evaluate to either true or false.
XSLT Stylesheet
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="type">
<h1>
<b>
<xsl:value-of select=". = '2'"/>
</b>
</h1>
</xsl:template>
<xsl:template match="text()"/>
</xsl:transform>
HTML Output
<h1><b>true</b></h1>
Try it online here.
With choose-element
The choose-clause has to be defined inside of the variable-declaration:
<xsl:variable name="type">
<xsl:value-of select="/MyDoc/foo/bar/type/text()"/>
</xsl:variable>
<xsl:variable name="typeBool">
<xsl:choose>
<xsl:when test="$type = '2'">true</xsl:when>
<xsl:otherwise>false</xsl:otherwise>
</xsl:choose>
</xsl:variable>
The conditional also looks cleaner this way.
With XSLT 2.0
#MichaelKay pointed out that in XSLT 2.0 a xpath-conditional can be used, which is even simpler:
<xsl:variable name="type">
<xsl:value-of select="/MyDoc/foo/bar/type/text()"/>
</xsl:variable>
<h1>
<b>
<xsl:value-of select="select="if($type=2) then 'true' else 'false'"/>
</b>
</h1>

How too make if else check using boolen in XSLT (transunion)

I have xml like this
<fileSummary>
<fileHitIndicator>regularHit</fileHitIndicator>
<ssnMatchIndicator>noMatch</ssnMatchIndicator>
<consumerStatementIndicator>true</consumerStatementIndicator>
<market>32</market>
<submarket>QU</submarket>
<creditDataStatus>
<suppressed>false</suppressed>
<doNotPromote>
<indicator>false</indicator>
</doNotPromote>
<freeze>
<indicator>false</indicator>
</freeze>
<minor>false</minor>
<disputed>false</disputed>
</creditDataStatus>
<inFileSinceDate estimatedCentury="false" estimatedDay="false" estimatedMonth="false" estimatedYear="false">2004-02-02</inFileSinceDate>
</fileSummary>
I want to make check if indicator value is true then show some text otherwise hide it.
<freeze>
<indicator>false</indicator>
</freeze>
I am new to XSLT, please let me know.
If the description of what you want to achieve includes an "otherwise", then you're not in need of an xsl:if, but an xsl:choose.
In the example below it is crucial that the template matches the freeze element, i.e. that the context of the xsl:choose is the freeze element.
EDIT: Added a complete example based on your updated input.
Stylesheet
<?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="freeze">
<result>
<xsl:choose>
<xsl:when test="indicator = 'false'">
<xsl:text>Indicator is false!</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>Indicator is not false!</xsl:text>
</xsl:otherwise>
</xsl:choose>
</result>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
Output
<?xml version="1.0" encoding="utf-8"?>
<result>Indicator is false!</result>
The stylesheet below:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="indicator">
<xsl:choose>
<xsl:when test=".='true'">
<xsl:text>some text</xsl:text>
</xsl:when>
<xsl:otherwise/>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
when applied to a your input (slightly modified)
<?xml version="1.0" encoding="UTF-8"?>
<fileSummary>
<fileHitIndicator>regularHit</fileHitIndicator>
<ssnMatchIndicator>noMatch</ssnMatchIndicator>
<consumerStatementIndicator>true</consumerStatementIndicator>
<market>32</market>
<submarket>QU</submarket>
<creditDataStatus>
<suppressed>false</suppressed>
<doNotPromote>
<indicator>true</indicator>
</doNotPromote>
<freeze>
<indicator>false</indicator>
</freeze>
<minor>false</minor>
<disputed>false</disputed>
</creditDataStatus>
<inFileSinceDate estimatedCentury="false" estimatedDay="false" estimatedMonth="false" estimatedYear="false">2004-02-02</inFileSinceDate>
</fileSummary>
produces:
<?xml version="1.0" encoding="utf-8"?><fileSummary>
<fileHitIndicator>regularHit</fileHitIndicator>
<ssnMatchIndicator>noMatch</ssnMatchIndicator>
<consumerStatementIndicator>true</consumerStatementIndicator>
<market>32</market>
<submarket>QU</submarket>
<creditDataStatus>
<suppressed>false</suppressed>
<doNotPromote>
some text
</doNotPromote>
<freeze>
</freeze>
<minor>false</minor>
<disputed>false</disputed>
</creditDataStatus>
<inFileSinceDate estimatedCentury="false" estimatedDay="false" estimatedMonth="false" estimatedYear="false">2004-02-02</inFileSinceDate>
</fileSummary>

Text value of input xml element as final xslt output

I have a scenario where the input(source) xml is having an element which contains a valid well formed xml as string. I am trying to write an xslt that would give me the text value of that desired element which contains the payload xml. In essence, output should only be text of the element that contains it. Here is what I am trying, am I missing something obvious here. I am using xslt 1.0
Thanks.
Input xml:
<BatchOrders xmlns="http://Microsoft.ABCD.OracleDB/STMT">
<BatchOrdersRECORD>
<BatchOrdersRECORD>
<ActualPayload>
<PersonName>
<PersonGivenName>CaptainJack</PersonGivenName>
<PersonMiddleName>Walter</PersonMiddleName>
<PersonSurName>Sparrow</PersonSurName>
<PersonNameSuffixText>Sr.</PersonNameSuffixText>
</PersonName>
</ActualPayload>
</BatchOrdersRECORD>
</BatchOrdersRECORD>
</BatchOrders>
Xslt:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="text()|#*" name="sourcecopy" mode="xml-to-string">
<xsl:value-of select="*"/>
</xsl:template>
<xsl:template name="xml-to-string-called-template">
<xsl:param name ="param1">
<xsl:element name ="DestPayload">
<xsl:text disable-output-escaping ="yes"><![CDATA[</xsl:text>
<xsl:call-template name ="sourcecopy"/>
<xsl:text disable-output-escaping ="yes">]]></xsl:text>
</xsl:element>
</xsl:param>
</xsl:template>
</xsl:stylesheet>
Desired Output:
<PersonName>
<PersonGivenName>CaptainJack</PersonGivenName>
<PersonMiddleName>Walter</PersonMiddleName>
<PersonSurName>Sparrow</PersonSurName>
<PersonNameSuffixText>Sr.</PersonNameSuffixText>
</PersonName>
Do you really need the mode="xml-to-string"?
Change
<xsl:template match="text()|#*" name="sourcecopy" mode="xml-to-string">
<xsl:value-of select="*"/>
</xsl:template>
to
<xsl:template match="text()|#*" name="sourcecopy">
<xsl:value-of select="." disable-output-escaping ="yes"/>
</xsl:template>
Would this template suffice?

XSL associative sorting using a field substring

The transformation I am writing must compose a comma separated string value from a given node set. The resulting string must be sorted according to a random (non-alphabetic) mapping for the first character in the input values.
I came up with this:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tmp="http://tempuri.org"
exclude-result-prefixes="tmp"
>
<xsl:output method="xml" indent="yes"/>
<tmp:sorting-criterion>
<code value="A">5</code>
<code value="B">1</code>
<code value="C">3</code>
</tmp:sorting-criterion>
<xsl:template match="/InputValueParentNode">
<xsl:element name="OutputValues">
<xsl:for-each select="InputValue">
<xsl:sort select="document('')/*/tmp:sorting-criterion/code[#value=substring(.,1,1)]" data-type="number"/>
<xsl:value-of select="normalize-space(.)"/>
<xsl:if test="position() != last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
It doesn't work and looks like the XPath document('')/*/tmp:sorting-criterion/code[#value=substring(.,1,1)] does not evaluate as I expect. I've checked to substitute the substring(.,1,1) for a literal and it evaluates to the proper value.
So, am I missing something that makes the sorting XPath expression not to evaluate as I expect or is it simply impossile to do it this way?
If not possible to create a XPath expression that works, is there a work around to achieve my purpose?
Note: I'm constrained to XSLT-1.0
Sample Input:
<?xml version="1.0" encoding="utf-8"?>
<InputValueParentNode>
<InputValue>A input value</InputValue>
<InputValue>B input value</InputValue>
<InputValue>C input value</InputValue>
</InputValueParentNode>
Expected ouput:
<?xml version="1.0" encoding="utf-8"?>
<OutputValues>B input value,C input value,A input value</OutputValues>
Replace the self::node() abbreviation ., with current() function.
A better predicate would be: starts-with(normalize-space(current()),#value)
Besides changing transformation according to Alejandro´s answer, I found it better to use a XSL variable for th mapping data to avoid declaration of a dummy namespace (tmp) as seen in Dimitre´s answer to another related question.
My final implementation:
<?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="/InputValueParentNode">
<xsl:variable name="sorting-map">
<i code="A" priority="5"/>
<i code="B" priority="1"/>
<i code="C" priority="3"/>
</xsl:variable>
<xsl:variable name="sorting-criterion" select="document('')//xsl:variable[#name='sorting-map']/*"/>
<xsl:element name="OutputValues">
<xsl:for-each select="InputValue">
<xsl:sort select="$sorting-criterion[#code=substring(normalize-space(current()),1,1)]/#priority" data-type="number"/>
<xsl:value-of select="normalize-space(current())"/>
<xsl:if test="position() != last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>