I have this XML document :Now I want to replace LineNo so that the output will be line no will 1 ,2 . I have tried some thing like this.
<xsl:value-of select="replace( '000010',1)"/>
<Rder>
<Order>
<OrderNo>458</OrderNo>
<LineNo>000010</LineNo>
<SerialNO>96</SerialNO>
<VNo>543</VNo>
</Order>
<Order>
<OrderNo>458</OrderNo>
<LineNo>000020</LineNo>
<SerialNO>32</SerialNO>
<VNo>543</VNo>
</Order>
</Rder>
I want to replace the value of LineNo= 000010 ,000020 by 1,2 in XSLT below one i have tried.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:key name="orders" match="Order" use="OrderNo" />
<xsl:template match="/*">
<SalesOrders>
<xsl:for-each select="Rder/Order[generate-id() = generate-id(key('orders', OrderNo)[1])]">
<Order VNo="{VNo}" OrderNo="{OrderNo}">
<OrderLines>
<xsl:apply-templates select="key('orders', OrderNo)" />
</OrderLines>
</Order>
</xsl:for-each>
</SalesOrders>
</xsl:template>
<xsl:template match="Order">
<OrderLine LineNo="{LineNo}" SerialNO="{SerialNO}"/>
</xsl:template>
</xsl:stylesheet>
Actually I getting those lineno details in same format i have tried couple cases its doesn't giving that expected format.
Any help would be appreciated.
Why don't you do simply:
<xsl:template match="Order">
<OrderLine LineNo="{position()}" SerialNO="{SerialNO}"/>
</xsl:template>
or:
<xsl:template match="Order">
<OrderLine LineNo="{number(LineNo) div 10}" SerialNO="{SerialNO}"/>
</xsl:template>
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?
I have a source list of xml in this format:
<metadata>
<metadatum>
<description>OnEnter</description>
<value>Hello World</id>
</metadatum>
<metadatum>
<description>OnLeave</description>
<value>Goodbye World</id>
</metadatum>
</metadata>
and a target structure like this:
<friendlyText>
<onEnter>[Content Here]</onEnter>
<onLeave>[Content Here]</onLeave>
</friendlyText>
Is it possible to create an XSLT that will map the 'value' field in the metadata hierarchy to the proper target node depending on the source 'description'?
I'm trying to get this done with Altova MapForce; it feels like there should be an interface to allow this, I'm just not finding it.
<?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="metadata">
<friendlyText>
<xsl:apply-templates select="metadatum"/>
</friendlyText>
</xsl:template>
<xsl:template match="metadatum">
<xsl:element name="{description}">
<xsl:value-of select="value"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="utf-8"?>
<friendlyText>
<OnEnter>Hello World</OnEnter>
<OnLeave>Goodbye World</OnLeave>
</friendlyText>
This transformation is a general solution that can work with any "target structure" that is in a separate XML document:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my" exclude-result-prefixes="my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vUpper" select=
"'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:variable name="vLower" select=
"'abcdefghijklmnopqrstuvwxyz'"/>
<my:target>
<friendlyText>
<onEnter>[Content Here]</onEnter>
<onLeave>[Content Here]</onLeave>
</friendlyText>
</my:target>
<xsl:variable name="vTarget" select="document('')/*/my:target/*"/>
<xsl:variable name="vMeta" select="/*/metadatum"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="$vTarget"/>
</xsl:template>
<xsl:template match="friendlyText/*/text()">
<xsl:value-of select=
"$vMeta[translate(description, $vLower, $vUpper)
=
translate(name(current()/..), $vLower, $vUpper)
]/value"/>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document (corrected to be made well-formed):
<metadata>
<metadatum>
<description>OnEnter</description>
<value>Hello World</value>
</metadatum>
<metadatum>
<description>OnLeave</description>
<value>Goodbye World</value>
</metadatum>
</metadata>
produces the wanted, correct result:
<friendlyText xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="my:my">
<onEnter>Hello World</onEnter>
<onLeave>Goodbye World</onLeave>
</friendlyText>
Do note: Only for convenience, the "target structure" is inline here. In a real world case it would be better to keep the "target structure" in a separate file and to load it using the document() function. Only the line:
<xsl:variable name="vTarget" select="document('')/*/my:target/*"/>
will need to be changed to:
<xsl:variable name="vTarget" select="document('someFileUrl')/*"/>
We are using XSL to convert a XML file into a pipe-delimited format.
<?xml version="1.0" encoding="UTF-8"?>
<ns:tradedata xmlns:ns="http://schemas.com/enterprise/util/extractservice/v1">
<tradedata_item>
<ORDER_ID>113632428</ORDER_ID>
<CUSIP>31393FHA7</CUSIP>
<TICKER>FHR</TICKER>
<SEC_NAME>FHR 2527 SG</SEC_NAME>
<ORDER_QTY>169249.6824</ORDER_QTY>
</tradedata_item>
<tradedata_item>
<ORDER_ID>113632434</ORDER_ID>
<CUSIP>31393G2C7</CUSIP>
<TICKER>FHR</TICKER>
<SEC_NAME>FHR 2531 ST</SEC_NAME>
<ORDER_QTY>214673.0105</ORDER_QTY>
</tradedata_item>
<tradedata_item>
<ORDER_ID>113632431</ORDER_ID>
<CUSIP>527069AH1</CUSIP>
<TICKER>LESL</TICKER>
<SEC_NAME>ZZZ_LESLIE S POOLMART INC</SEC_NAME>
<ORDER_QTY>365000.0000</ORDER_QTY>
</tradedata_item>
</ns:tradedata>
We need the first line in the output to be the column headers, and everything else would be data, like this...
ORDER_ID|CUSIP|TICKER|SEC_NAME|ORDER_QTY
1136324289|31393FHA7|FHR|FHR 2527 SG|169249.6824
1136324304|31393G2C7|FHR|FHR 2531 ST|214673.0105
We've got the XSL working to get the data, but we can't get the header to output correctly. We just select the first tradedata_item element, then iterate the element name and separate them using | characters. Here is the full XSL...
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl"
version="1.0" xmlns="http://schemas.com/enterprise/util/extractservice/v1"
xmlns:o="http://schemas.com/enterprise/util/extractservice/v1" >
<!-- xsl:strip-space elements="*"/-->
<xsl:output method="text" indent="no"/>
<xsl:template match="/tradedata/tradedata_item[1]">
<xsl:for-each select="*">
<xsl:value-of select="local-name()"/>|
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="/">
<xsl:for-each select="tradedata/tradedata_item">
<xsl:value-of select="ORDER_ID"/>|<xsl:value-of select="CUSIP"/>|<xsl:value-of select="TICKER"/>|<xsl:value-of select="SEC_NAME"/>|<xsl:value-of select="ORDER_QTY"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The output we're seeing is just data, no header...
113632428|31393FHA7|FHR|FHR 2527 SG|169249.6824
113632430|31393G2C7|FHR|FHR 2531 ST|214673.0105
113632431|527069AH1|LESL|ZZZ_LESLIE S POOLMART INC|365000.0000
113632434|38470RAD3|GRAHAM|ZZZ_GRAHAM PACKAGING CO|595000.0000
Please disregard any namespace inconsistencies; I had to obfuscate the xml and xsl for legal reasons.
Try this :
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" indent="no"/>
<xsl:template match="/">
<xsl:for-each select="tradedata/tradedata_item[1]/*">
<xsl:value-of select="concat(name(), '|')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Output :
ORDER_ID|CUSIP|TICKER|SEC_NAME|ORDER_QTY|
It seems pretty simple to me. Maybe your error lies elsewhere.
I tried your code.. I only changed the first template to match:
<xsl:template match="//tradedata_item[1]">
and it worked for me, i.e. got the header names.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl ofi"
version="1.0" xmlns="http://schemas.com/enterprise/util/extractservice/v1"
xmlns:ofi="http://schemas.oppen.com/enterprise/util/extractservice/v1">
<xsl:output method="text" indent="no"/>
<xsl:template match="tradedata_item[position()='1']">
<xsl:for-each select="self::*">
<xsl:for-each select="child::*[position()!='5']">
<xsl:value-of select="local-name(self::*)"/>|
</xsl:for-each>
<xsl:value-of select="local-name(child::*[position()='5'])"/>
<xsl:text>
</xsl:text>
<xsl:value-of select="ORDER_ID"/>|<xsl:value-of select="CUSIP"/>|<xsl:value-of select="TICKER"/>|<xsl:value-of select="SEC_NAME"/>|<xsl:value-of select="ORDER_QTY"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
<xsl:template match="tradedata_item[position()>1]">
<xsl:for-each select="self::*">
<xsl:value-of select="ORDER_ID"/>|<xsl:value-of select="CUSIP"/>|<xsl:value-of select="TICKER"/>|<xsl:value-of select="SEC_NAME"/>|<xsl:value-of select="ORDER_QTY"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
I have some short form day names like so:
M -> Monday
T -> Tuesday
W -> Wednesday
R -> Thursday
F -> Friday
S -> Saturday
U -> Sunday
How can I convert an xml element like <days>MRF</days> into the long version <long-days>Monday,Thursday,Friday</long-days> using xslt?
Update from comments
Days will not be repeated
This stylesheet
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:d="day"
exclude-result-prefixes="d">
<d:d l="M" n="Monday"/>
<d:d l="T" n="Tuesday"/>
<d:d l="W" n="Wednesday"/>
<d:d l="R" n="Thursday"/>
<d:d l="F" n="Friday"/>
<d:d l="S" n="Saturday"/>
<d:d l="U" n="Sunday"/>
<xsl:variable name="vDays" select="document('')/*/d:d"/>
<xsl:template match="days">
<long-days>
<xsl:apply-templates
select="$vDays[contains(current(),#l)]"/>
</long-days>
</xsl:template>
<xsl:template match="d:d">
<xsl:value-of select="#n"/>
<xsl:if test="position()!=last()">,</xsl:if>
</xsl:template>
</xsl:stylesheet>
With this input:
<days>MRF</days>
Output:
<long-days>Monday,Thursday,Friday</long-days>
Edit: For those who wander, retaining the sequence order:
<xsl:variable name="vCurrent" select="current()"/>
<xsl:apply-templates
select="$vDays[contains($vCurrent,#l)]">
<xsl:sort select="substring-before($vCurrent,#l)"/>
</xsl:apply-templates>
Note: Because days wouldn't be repeated, this is the same as looking up for item existence in sequence with empty string separator.
That should do it... (There might be more elegant solutions though... ;-)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/days">
<long-days>
<xsl:if test="contains(.,'M')">Monday<xsl:if test="string-length(substring-before(.,'M'))=string-length(.)-1">,</xsl:if></xsl:if>
<xsl:if test="contains(.,'T')">Tuesday<xsl:if test="string-length(substring-before(.,'T'))=string-length(.)-1">,</xsl:if></xsl:if>
<xsl:if test="contains(.,'W')">Wednesday<xsl:if test="string-length(substring-before(.,'W'))=string-length(.)-1">,</xsl:if></xsl:if>
<xsl:if test="contains(.,'R')">Thursday<xsl:if test="string-length(substring-before(.,'R'))=string-length(.)-1">,</xsl:if></xsl:if>
<xsl:if test="contains(.,'F')">Friday<xsl:if test="string-length(substring-before(.,'F'))=string-length(.)-1">,</xsl:if></xsl:if>
<xsl:if test="contains(.,'S')">Saturday<xsl:if test="string-length(substring-before(.,'S'))=string-length(.)-1">,</xsl:if></xsl:if>
<xsl:if test="contains(.,'U')">Sunday<xsl:if test="string-length(substring-before(.,'U'))=string-length(.)-1">,</xsl:if></xsl:if>
</long-days>
</xsl:template>
The currently accepted solution always displays the long days names in chronological order and in addition, it doesn't display repeating (with same code) days.
Suppose we have the following XML document:
<days>STMSU</days>
I. This XSLT 1.0 transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my" exclude-result-prefixes="my" >
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<my:days>
<M>Monday</M>
<T>Tuesday</T>
<W>Wednesday</W>
<R>Thursday</R>
<F>Friday</F>
<S>Saturday</S>
<U>Sunday</U>
</my:days>
<xsl:key name="kLongByShort" match="my:days/*"
use="name()"/>
<xsl:variable name="vstylesheet"
select="document('')"/>
<xsl:template match="days">
<long-days>
<xsl:call-template name="expand"/>
</long-days>
</xsl:template>
<xsl:template name="expand">
<xsl:param name="pcodeString" select="."/>
<xsl:if test="$pcodeString">
<xsl:variable name="vchar" select=
"substring($pcodeString,1,1)"/>
<xsl:for-each select="$vstylesheet">
<xsl:value-of select=
"concat(key('kLongByShort',$vchar),
substring(',',1,string-length($pcodeString)-1)
)
"/>
</xsl:for-each>
<xsl:call-template name="expand">
<xsl:with-param name="pcodeString" select=
"substring($pcodeString,2)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
when applied on the above document, produces the wanted, correct result:
<long-days>Saturday,Tuesday,Monday,Saturday,Sunday</long-days>
II. This XSLT 2.0 transformation:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:variable name="vshortCodes" as="xs:integer+"
select="string-to-codepoints('MTWRFSU')"/>
<xsl:variable name="vlongDays" as="xs:string+"
select="'Monday','Tuesday','Wenesday','Thursday',
'Friday','Saturday','Sunday'
"/>
<xsl:template match="days">
<long-days>
<xsl:for-each select="string-to-codepoints(.)">
<xsl:value-of separator="" select=
"for $pos in position() ne last()
return
($vlongDays[index-of($vshortCodes,current())],
','[$pos])
"/>
</xsl:for-each>
</long-days>
</xsl:template>
</xsl:stylesheet>
when applied on the same XML document:
<days>STMSU</days>
produce the wanted, correct result:
<long-days>Saturday,Tuesday,Monday,Saturday,Sunday</long-days>