I have an XSLT that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="no" encoding="utf-8" media-type="text/plain" />
<xsl:template match="/SOME/NODE">
<xsl:if test="./BLAH[foo]">
<xsl:value-of select="concat(#id, ',' , ./BLAH/bar/#id, ',' , ./blorb/text())"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
The output looks something like this (it is going to be a CSV file):
1,2,3
4,456,22
90,5,some text
365,16,soasdkjasdjkasdf
9,43,more text
What I need is for it to be transformed into:
1,2,3
4,456,22
90,5,some text
365,16,soasdkjasdjkasdf
9,43,more text
The main problems are the blank lines (from nodes that do not match the IF condition) and the indentation. Is there any way to remove the blank lines and trim the indentation while preserving the line breaks after lines that are not blank?
I've tried using <xsl:strip-space elements="*"/>, but then the output looks like this:
1,2,3,4,456,22,90,5,some text,365,16,soasdkjasdjkasdf,9,43,more text
Which doesn't work since I need to have 3 values on each line.
As requested, a (heavily simplified) sample of the input:
<SOME>
<NODE>
<BLAH id="1">
<foo>The Foo</foo>
<bar id="2" />
<blorb> some text </blorb>
</BLAH>
</NODE>
<NODE>
<BLAH id="3">
<bar id="4" />
<blorb>some text that shouldn't be in output because there's no foo here</blorb>
</BLAH>
</NODE>
<NODE>
<BLAH id="5">
<foo>another Foo</foo>
<bar id="6" />
<blorb>some other text</blorb>
</BLAH>
</NODE>
</SOME>
I would suggest you approach it this way:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="utf-8" />
<xsl:template match="/SOME">
<xsl:for-each select="NODE/BLAH[foo]">
<xsl:value-of select="#id"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="bar/#id"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="blorb"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Related
Using XSLT 2 how can I skip and not touch a record if a field contains text, in this case a date? I want to only process all the record that don't have a <SurveyDate> and don't touch record that already have a <SurveyDate>.
I tried using a choose statement with a test of "not(SurveyDate/text())" but this is not working. here is my complete XSL code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:lookup="lookup" xmlns:exsl="http://exslt.org/common" exclude-result-prefixes="lookup exsl">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" encoding="utf-8" media-type="xml/plain" />
<xsl:strip-space elements="*" />
<xsl:template match="node() | #*">
<xsl:copy>
<xsl:apply-templates select="node() | #*" />
</xsl:copy>
</xsl:template>
<xsl:template match="Sub">
<!-- This is the final output -->
<xsl:choose>
<xsl:when test="not(SurveyDate/text())">
<xsl:if test= "count(Request/Phase/Status) = count(Request/Phase/Status[matches(. , 'Sup|Ser|Adm|Can')])">
<Request>
<xsl:copy-of select="Request/Code"/>
<SurveyDate>
<xsl:value-of select="format-dateTime(current-dateTime(), '[Y0001]-[M01]-[D01]T[H1]:[m01]:[s01]')"/>
</SurveyDate>
</Request>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<!-- just for testing remove when done -->
<Test>Do nothing</Test>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
And this is my test XML data.
<?xml version='1.0' encoding='UTF-8'?>
<document>
<businessobjects>
<Sub>
<Code>1.02</Code>
<Status>UsrWorkOrderCancelled</Status>
<Request>
<Code>1.00</Code>
<Description>Test 1</Description>
<SurveyDate>2022-11-02T22:55:55</SurveyDate>
<Phase>
<Code>1.01</Code>
<Status>UsrWorkOrderSupervisorApproved</Status>
</Phase>
<Phase>
<Code>1.02</Code>
<Status>UsrWorkOrderCancelled</Status>
</Phase>
</Request>
</Sub>
<Sub>
<Code>2.01</Code>
<Status>UsrWorkOrderSupervisorApproved</Status>
<Request>
<Code>2.00</Code>
<Description>Test 2</Description>
<SurveyDate></SurveyDate>
<Phase>
<Code>2.01</Code>
<Status>UsrWorkOrderSupervisorApproved</Status>
</Phase>
<Phase>
<Code>2.02</Code>
<Status>UsrWorkOrderCancelled</Status>
</Phase>
</Request>
</Sub>
</businessobjects>
</document>
The result XML I need is this:
<document>
<businessobjects>
<Request>
<Code>2.00</Code>
<SurveyDate>2022-11-03T21:45:13</SurveyDate>
</Request>
</businessobjects>
</document>
My advice: forget using xsl:choose or xsl:if, and instead put the conditional logic into the template's match expression:
<xsl:template match="Sub[not(Request/SurveyDate/text())]">
<!-- handle Sub without SurveyDate -->
<!-- ... -->
</xsl:template>
Leave the case where a Sub does have a SurveyDate for the identity template to handle, if you want to copy it unchanged. If you want to remove it (it's not clear from your test code what you want to do with it), you could add another template to do so:
<xsl:template match="Sub"/>
Note that template would have a lower priority than the one above, because its match expression is simpler, so it would apply only to Sub elements which did have a SurveyDate descendant.
I am looking to shorten my XSLT codebase by seeing if XSLT can increase a text number for each match. The text number exists in both the attribute value "label-period0" and the "xls:value-of" value.
The code works, no errors so this is more a question of how to shorten the code and make use of some sort of iteration on a specific character in a string.
I added 2 similar code structures for "period0" and "period1" to better see what exactly are the needed changes in terms of the digit in the text strings.
Source XML file:
<data>
<periods>
<period0><from>2016-01-01</from><to>2016-12-01</to></period0>
<period1><from>2015-01-01</from><to>2015-12-01</to></period1>
<period2><from>2014-01-01</from><to>2014-12-01</to></period2>
<period3><from>2013-01-01</from><to>2013-12-01</to></period3>
</periods>
<balances>
<balance0><instant>2016-12-31</instant></balance0>
<balance1><instant>2015-12-31</instant></balance1>
<balance2><instant>2014-12-31</instant></balance2>
<balance3><instant>2013-12-31</instant></balance3>
</balances>
</data>
XSL file:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform
version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="xml" indent="yes"/>
<!-- Block all data that has no user defined template -->
<xsl:mode on-no-match="shallow-skip"/>
<xsl:template match="data">
<results>
<periods>
<periods label="period0">
<xsl:value-of
select =
"concat(periods/period0/from, '--', periods/period0/to)"
/>
</periods>
<periods label="period1">
<xsl:value-of
select =
"concat(periods/period1/from, '--', periods/period1/to)"
/>
</periods>
<!-- Etc for period [2 and 3]-->
</periods>
<balances>
<balance label="balance0">
<xsl:value-of select ="balances/balance0/instant"/>
</balance>
<!-- Etc for balance [1,2 and 3] -->
</balances>
</results>
</xsl:template>
</xsl:transform>
Result:
<?xml version="1.0" encoding="UTF-8"?>
<results>
<periods>
<periods label="period0">2016-01-01--2016-12-01</periods>
<periods label="period1">2015-01-01--2015-12-01</periods>
</periods>
<balances>
<balance label="balance0">2016-12-31</balance>
</balances>
</results>
Wanted result:
(with an XSL that steps the digit in the text string, or any other logics in XSL that could cater for manipulating the digit in text string)
<?xml version="1.0" encoding="UTF-8"?>
<results>
<periods>
<periods label="period0">2016-01-01--2016-12-01</periods>
<periods label="period1">2015-01-01--2015-12-01</periods>
<periods label="period2">2014-01-01--2015-12-01</periods>
<periods label="period3">2013-01-01--2015-12-01</periods>
</periods>
<balances>
<balance label="balance0">2016-12-31</balance>
<balance label="balance1">2015-12-31</balance>
<balance label="balance2">2014-12-31</balance>
<balance label="balance3">2013-12-31</balance>
</balances>
</results>
Couldn't you do simply something like:
<xsl:template match="/data">
<results>
<periods>
<xsl:for-each select="periods/*">
<periods label="{name()}">
<xsl:value-of select="from"/>
<xsl:text>--</xsl:text>
<xsl:value-of select="to"/>
</periods>
</xsl:for-each>
</periods>
<balances>
<xsl:for-each select="balances/*">
<balance label="{name()}">
<xsl:value-of select="instant"/>
</balance>
</xsl:for-each>
</balances>
</results>
</xsl:template>
If you want to do your own numbering, you can change:
<periods label="{name()}">
to:
<periods label="period{position() - 1}">
I have the following XML.
<?xml version="1.0" ?>
<Root xmlns="http://1.local/1.xsd">
<Definitions>
<FileTypes>
<FileType ID="1" Name="FileType1"/>
<FileType ID="2" Name="FileType2"/>
<!--... - lots of file types-->
<FileTypes>
</Definitions>
<Files>
<File Name="File1" FileTypeID="1" />
<File Name="File2" FileTypeID="1" />
<File Name="File3" FileTypeID="2" />
<!--... - lots of files-->
<Files>
</Root>
For each file, I need to get its Name attribute and for its FileTypeID lookup corresponding file type name
So example output would be:
File name: File1
File type: FileType1
File name: File2
File type: FileType1
File name: File3
File type: FileType2
This is XSLT I have so far but I'm not sure how to lookup name of file type.
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:l="http://1.local/1.xsd"
exclude-result-prefixes="l"
version="1.0">
<xsl:output method="text" omit-xml-declaration="yes" media-type="text/plain" />
<xsl:template match="/">
<xsl:apply-templates select="l:Root/l:Files" />
</xsl:template>
<xsl:template match="l:Root/l:Files">
Why
<xsl:for-each select="l:File">
File name: <xsl:value-of select="#Name">
File type:
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Use a xsl:key here to look up the FileTypes
<xsl:key name="FileTypes" match="l:FileType" use="#ID" />
Then, to get the relevant FileType name, you would do this
<xsl:value-of select="key('FileTypes', #FileTypeID)/#Name" />
Try this XSLT
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:l="http://1.local/1.xsd"
exclude-result-prefixes="l"
version="1.0">
<xsl:output method="text" omit-xml-declaration="yes" media-type="text/plain" />
<xsl:key name="FileTypes" match="l:FileType" use="#ID" />
<xsl:template match="/">
<xsl:apply-templates select="l:Root/l:Files" />
</xsl:template>
<xsl:template match="l:Files">
<xsl:for-each select="l:File">
File name: <xsl:value-of select="#Name" />
File type: <xsl:value-of select="key('FileTypes', #FileTypeID)/#Name" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
(Note your XML is not well-formed, as you do not have correct closing tags for FileTypes and Files.)
If you want to work with XSLT you need to start with understanding its expression language XPath to navigate XML trees, you can select //l:FileType[#ID = current()/#FileTypeID]/#Name. Or in XSLT, as Tim has already posted, you can use a key to efficiently implement the lookup.
You can try the following XSL.
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:l="http://1.local/1.xsd"
exclude-result-prefixes="l"
version="1.0">
<xsl:output method="text" omit-xml-declaration="yes" media-type="text/plain" />
<xsl:template match="/">
<xsl:apply-templates select="l:Root/l:Files" />
</xsl:template>
<xsl:template match="l:Root/l:Files">
Why
<xsl:for-each select="l:File">
File name: <xsl:value-of select="#Name"/>
File type: <xsl:value-of select="//l:FileTypes/l:FileType[#ID=current()/#FileTypeID]/#Name"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Also, you need to make sure you put the correct namespace in your xsl transformation to actually match the values in you XML, and you were missing a few close tags in your XML.
For completeness I've included the fixed XML that I used for the solution
<Root xmlns="http://1.local/1.xsd">
<Definitions>
<FileTypes>
<FileType ID="1" Name="FileType1"/>
<FileType ID="2" Name="FileType2"/>
<!--... - lots of file types-->
</FileTypes>
</Definitions>
<Files>
<File Name="File1" FileTypeID="1" />
<File Name="File2" FileTypeID="1" />
<File Name="File3" FileTypeID="2" />
<!--... - lots of files-->
</Files>
</Root>
I have source xml looking like this :
<Data>
<ActionPlaces>
<ActionPlace>
<ActionPlaceID>74</ActionPlaceID>
<PlaceName>Theatre Of Classic</PlaceName>
</ActionPlace>
</ActionPlaces>
<Actions>
<CommonAction Id="2075" Name="King">
<Action>
<ActionID>4706</ActionID>
<ActionPlaceID>74</ActionPlaceID>
</Action>
</CommonAction>
</Actions>
</Data>
Which is to transform to this:
<category name="King">
<name>King</name>
<parent name="Theatre Of Classic" />
</category>
I want to use variable :
<xsl:template match="ActionPlaces">
<xsl:variable name="id" select="/ActionPlace/ActionPlaceID"/>
<xsl:template match="CommonAction" >
<category name="<xsl:value-of select="#name"/> >
<name><xsl:value-of select="#name"/></name>
<parent <xsl:if test="/Action/ActionPlaceID = $id">
name=/Action/ActionPlaceID/> <- how to get name of theatre here?
</xsl:template>
Can variable store not only id but name also? And how to get it? What is the most common approach to handle this ?
Here's one option using XSL keys (as #michael-hor257k suggested):
Input
<Root>
<ActionPlaces>
<ActionPlace>
<ActionPlaceID>74</ActionPlaceID>
<PlaceName>Theatre Of Classic</PlaceName>
</ActionPlace>
</ActionPlaces>
<Actions>
<CommonAction Id="2075" Name="King">
<Action>
<ActionID>4706</ActionID>
<ActionPlaceID>74</ActionPlaceID>
</Action>
</CommonAction>
</Actions>
</Root>
Stylesheet
<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"/>
<!-- Collect all <ActionPlace> elements into an XSL key -->
<xsl:key name="ActionPlaceById" match="ActionPlace" use="ActionPlaceID"/>
<xsl:template match="/">
<xsl:apply-templates select="Root/Actions/CommonAction"/>
</xsl:template>
<xsl:template match="CommonAction">
<category name="{#Name}">
<name>
<xsl:value-of select="#Name"/>
</name>
<!--
Using the ActionPlaceById key we created earlier, fetch the <ActionPlace>
element that has an <ActionPlaceID> child that has the same value as the
<ActionPlaceID> descendant of the current <CommonAction> element.
-->
<parent name="{key('ActionPlaceById', Action/ActionPlaceID)/PlaceName}"/>
</category>
</xsl:template>
</xsl:stylesheet>
Output
<?xml version="1.0" encoding="utf-8"?>
<category name="King">
<name>King</name>
<parent name="Theatre Of Classic"/>
</category>
I have the following XML
<?xml version="1.0" encoding="ISO-8859-1" ?>
- <DEVICEMESSAGES>
<VERSION xml="1" checksum="" revision="0" envision="33050000" device="" />
<HEADER id1="0001" id2="0001" content="Nasher[<messageid>]: <!payload>" />
<MESSAGE level="7" parse="1" parsedefvalue="1" tableid="15" id1="24682" id2="24682" eventcategory="1003010000" content="Access to <webpage> was blocked due to its category (<info> by <hostname>)" />
</DEVICEMESSAGES>
I am using the following xslt
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="DEVICEMESSAGES">
<xsl:value-of select="#id2"/>,<xsl:text/>
<xsl:value-of select="#content"/>,<xsl:text/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
when i use MSXML i just get ,, whereas i want to have something like
id2, content
0001 , Nasher[<messageid>]: <!payload>"
The DEVICEMESSAGES element doesn't have attributes at all.
Change:
<xsl:template match="DEVICEMESSAGES">
to:
<xsl:template match="DEVICEMESSAGES/HEADER">