I'm using XSLT 3.0 to transform this document JSON to this document XML with the function json-to-xml,
<xsl:variable name="input-as-xml" select="json-to-xml(.)"/>
From saxon I pass this json like document XML:
String XML = "<root>" + JSON + "</root>";
I get an XML when call the function json-to-xml:
<?xml version="1.0" encoding="utf-8"?>
<map
xmlns="http://www.w3.org/2005/xpath-functions">
<string key="_D">urn:oasis:names:specification:ubl:schema:xsd:Invoice-2</string>
<string key="_S">urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2</string>
<string key="_B">urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2</string>
<array key="Invoice">
<map>
<array key="ID">
<map>
<string key="IdentifierContent">123</string>
</map>
</array>
<array key="IssueDate">
<map>
<string key="DateContent">2011-09-22</string>
</map>
</array>
<array key="InvoicePeriod">
<map>
<array key="StartDate">
<map>
<string key="DateContent">2011-08-01</string>
</map>
</array>
<array key="EndDate">
<map>
<string key="DateContent">2011-08-31</string>
</map>
</array>
</map>
</array>
<array key="AccountingSupplierParty">
<map>
<array key="Party">
<map>
<array key="PartyName">
<map>
<array key="Name">
<map>
<string key="TextContent">Custom Cotter Pins</string>
</map>
</array>
</map>
</array>
</map>
</array>
</map>
</array>
<array key="AccountingCustomerParty">
<map>
<array key="Party">
<map>
<array key="PartyName">
<map>
<array key="Name">
<map>
<string key="TextContent">North American Veeblefetzer</string>
</map>
</array>
</map>
</array>
</map>
</array>
</map>
</array>
<array key="LegalMonetaryTotal">
<map>
<array key="PayableAmount">
<map>
<number key="AmountContent">100.00</number>
<string key="AmountCurrencyIdentifier">CAD</string>
</map>
</array>
</map>
</array>
<array key="InvoiceLine">
<map>
<array key="ID">
<map>
<string key="IdentifierContent">1</string>
</map>
</array>
<array key="LineExtensionAmount">
<map>
<number key="AmountContent">100.00</number>
<string key="AmountCurrencyIdentifier">CAD</string>
</map>
</array>
<array key="Item">
<map>
<array key="Description">
<map>
<string key="TextContent">Cotter pin, MIL-SPEC</string>
</map>
</array>
</map>
</array>
</map>
</array>
</map>
</array>
</map>
Is it correct pass this XML as parameter?
<xsl:apply-templates select="$input-as-xml" />
Please, any suggestion in how can I use templates to transform the XML that I want? I just want some suggestions.
The information about prefixes does not seem to be included in the JSON so I have decided to declare them as parameters and I have assumed you know the name/key of the element to start with (e.g. Invoice):
<?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"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
exclude-result-prefixes="xs math fn"
version="3.0">
<xsl:param name="json-input" as="xs:string">{"_D":"urn:oasis:names:specification:ubl:schema:xsd:Invoice-2",
"_S":"urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2",
"_B":"urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2",
"Invoice":[{
"ID":[{"IdentifierContent":"123"}],
"IssueDate":[{"DateContent":"2011-09-22"}],
"InvoicePeriod":[{
"StartDate":[{"DateContent":"2011-08-01"}],
"EndDate":[{"DateContent":"2011-08-31"}]
}],
"AccountingSupplierParty":[{
"Party":[{
"PartyName":[{
"Name":[{"TextContent":"Custom Cotter Pins"}]
}]
}]
}],
"AccountingCustomerParty":[{
"Party":[{
"PartyName":[{
"Name":[{"TextContent":"North American Veeblefetzer"}]
}]
}]
}],
"LegalMonetaryTotal":[{
"PayableAmount":[{"AmountContent":100.00,
"AmountCurrencyIdentifier":"CAD"}]
}],
"InvoiceLine":[{
"ID":[{"IdentifierContent":"1"}],
"LineExtensionAmount":[{"AmountContent":100.00,
"AmountCurrencyIdentifier":"CAD"}],
"Item":[{
"Description":[{"TextContent":"Cotter pin, MIL-SPEC"}]
}]
}]
}]}</xsl:param>
<xsl:param name="prefix2" as="xs:string" select="'cbc'"/>
<xsl:param name="prefix1" as="xs:string" select="'cac'"/>
<xsl:output indent="yes"/>
<xsl:template name="xsl:initial-template">
<xsl:apply-templates select="json-to-xml($json-input)//fn:array[#key = 'Invoice']"/>
</xsl:template>
<xsl:template match="fn:array[#key = 'Invoice']" priority="5">
<xsl:variable name="ns" select="../fn:string[#key = '_D']"/>
<xsl:variable name="ns1" select="../fn:string[#key = '_S']"/>
<xsl:variable name="ns2" select="../fn:string[#key = '_B']"/>
<xsl:element name="{#key}" namespace="{$ns}">
<xsl:namespace name="{$prefix1}" select="$ns1"/>
<xsl:namespace name="{$prefix2}" select="$ns2"/>
<xsl:apply-templates>
<xsl:with-param name="ns1" select="$ns1" tunnel="yes"/>
<xsl:with-param name="ns2" select="$ns2" tunnel="yes"/>
</xsl:apply-templates>
</xsl:element>
</xsl:template>
<xsl:template match="fn:array[#key and *[1][self::fn:map[fn:string]] and not(*[2])]">
<xsl:param name="ns2" tunnel="yes"/>
<xsl:element name="{$prefix2}:{#key}" namespace="{$ns2}">
<xsl:value-of select="fn:map/fn:string"/>
</xsl:element>
</xsl:template>
<xsl:template match="fn:array[#key and fn:map[fn:array]]">
<xsl:param name="ns1" tunnel="yes"/>
<xsl:element name="{$prefix1}:{#key}" namespace="{$ns1}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
JSON to XML Example:
JSON: "
"{\n" +" \"color\": \"red\",\n" +" \"value\": \"#f00\"\n" +"}";
XSLT:
<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="jsonText"></xsl:param>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template name="init">
<xsl:apply-templates select="json-to-xml($jsonText)"/>
</xsl:template>
<xsl:template match="string[#key = 'subjects']" xpath-default namespace="http://www.w3.org/2005/xpath-functions"><xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:sequence select="parse-xml(.)/node()"/></xsl:copy></xsl:template</xsl:stylesheet>`
Related
Here is my source XML which I'm trying to transform based on values of <City>
<?xml version="1.0" encoding="UTF-8"?>
<Workers>
<Worker>
<EmpID>12345</EmpID>
<City>NYC</City>
<Allowance>
<Type>Meal</Type>
<Amount>150</Amount>
</Allowance>
<Allowance>
<Type>Gym</Type>
<Amount>200</Amount>
</Allowance>
</Worker>
<Worker>
<EmpID>56789</EmpID>
<City>SFO</City>
<Base>
<BaseType>General</BaseType>
<BaseAmount>1000</BaseAmount>
</Base>
</Worker>
<Worker>
<EmpID>18978</EmpID>
<City>LAX</City>
<Base>
<BaseType>General</BaseType>
<BaseAmount>3000</BaseAmount>
</Base>
</Worker>
</Workers>
I'm attempting to transform as below. Transformation doesn't need be to applied if the value of <City> is either NYC or SFO
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<SOAP-ENV:Body xmlns:SOAP-ENV="http://www.w3.org/2001/12/soap-envelope">
<m:GetQuotationResponse xmlns:m = "http://www.example.com">
<m:Worker>12345</m:Worker>
<m:Location>NYC</m:Location>
<m:Expense>
<m:ExpenseType>Meal</m:ExpenseType>
<m:Amount>150</m:Amount>
</m:Expense>
<m:Expense>
<m:ExpenseType>Gym</m:ExpenseType>
<m:Amount>200</m:Amount>
</m:Expense>
</m:GetQuotationResponse>
</SOAP-ENV:Body>
<SOAP-ENV:Body xmlns:SOAP-ENV="http://www.w3.org/2001/12/soap-envelope">
<m:GetQuotationResponse xmlns:m = "http://www.example.com">
<m:Worker>56789</m:Worker>
<m:Location>SFO</m:Location>
<m:Expense>
<m:ExpenseType>General</m:ExpenseType>
<m:Amount>1000</m:Amount>
</m:Expense>
</m:GetQuotationResponse>
</SOAP-ENV:Body>
</Root>
This is my attempt
<?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"
xmlns:SOAP-ENV = "http://www.w3.org/2001/12/soap-envelope"
xmlns:m = "http://www.example.com"
exclude-result-prefixes="xs m SOAP-ENV"
version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="Worker[City='NYC']">
<Root>
<SOAP-ENV:Body xmlns:m = "http://www.example.com">
<m:GetQuotationResponse>
<m:Worker><xsl:value-of select="EmpID"/></m:Worker>
<m:Location><xsl:value-of select="City"/></m:Location>
<xsl:for-each select="Allowance">
<m:Expense>
<m:ExpenseType><xsl:value-of select="Type"/></m:ExpenseType>
<m:Amount><xsl:value-of select="Amount"/></m:Amount>
</m:Expense>
</xsl:for-each>
</m:GetQuotationResponse>
</SOAP-ENV:Body>
</Root>
</xsl:template>
<xsl:template match="Worker[City='SFO']">
<Root>
<SOAP-ENV:Body xmlns:m = "http://www.example.com">
<m:GetQuotationResponse>
<m:Worker><xsl:value-of select="EmpID"/></m:Worker>
<m:Location><xsl:value-of select="City"/></m:Location>
<xsl:for-each select="Base">
<m:Expense>
<m:ExpenseType><xsl:value-of select="BaseType"/></m:ExpenseType>
<m:Amount><xsl:value-of select="BaseAmount"/></m:Amount>
</m:Expense>
</xsl:for-each>
</m:GetQuotationResponse>
</SOAP-ENV:Body>
</Root>
</xsl:template>
</xsl:stylesheet>
I have two issues(or more)
Couldn't get <Root> as the Parent node
Templates which are not matching values of <City> also returned. I wanted to match only <xsl:template match="Worker[City='NYC']"> or <xsl:template match="Worker[City='SFO']">
Current Output
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<SOAP-ENV:Body xmlns:SOAP-ENV="http://www.w3.org/2001/12/soap-envelope">
<m:GetQuotationResponse xmlns:m="http://www.example.com">
<m:Worker>12345</m:Worker>
<m:Location>NYC</m:Location>
<m:Expense>
<m:ExpenseType>Meal</m:ExpenseType>
<m:Amount>150</m:Amount>
</m:Expense>
<m:Expense>
<m:ExpenseType>Gym</m:ExpenseType>
<m:Amount>200</m:Amount>
</m:Expense>
</m:GetQuotationResponse>
</SOAP-ENV:Body>
</Root>
<Root>
<SOAP-ENV:Body xmlns:SOAP-ENV="http://www.w3.org/2001/12/soap-envelope">
<m:GetQuotationResponse xmlns:m="http://www.example.com">
<m:Worker>56789</m:Worker>
<m:Location>SFO</m:Location>
<m:Expense>
<m:ExpenseType>General</m:ExpenseType>
<m:Amount>1000</m:Amount>
</m:Expense>
</m:GetQuotationResponse>
</SOAP-ENV:Body>
</Root>
18978
LAX
General
3000
Any help is appreciated to get this working using xslt 2.0 or xslt 3.0. Thank you
I'm posting the first solution that Martin Honnen has suggested.
<?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"
xmlns:SOAP-ENV = "http://www.w3.org/2001/12/soap-envelope"
xmlns:m = "http://www.example.com"
exclude-result-prefixes="xs m SOAP-ENV"
version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="Workers">
<Root>
<xsl:apply-templates/>
</Root>
</xsl:template>
<xsl:template match="Worker[City='NYC']">
<SOAP-ENV:Body xmlns:m = "http://www.example.com">
<m:GetQuotationResponse>
<m:Worker><xsl:value-of select="EmpID"/></m:Worker>
<m:Location><xsl:value-of select="City"/></m:Location>
<xsl:for-each select="Allowance">
<m:Expense>
<m:ExpenseType><xsl:value-of select="Type"/></m:ExpenseType>
<m:Amount><xsl:value-of select="Amount"/></m:Amount>
</m:Expense>
</xsl:for-each>
</m:GetQuotationResponse>
</SOAP-ENV:Body>
</xsl:template>
<xsl:template match="Worker[City='SFO']">
<SOAP-ENV:Body xmlns:m = "http://www.example.com">
<m:GetQuotationResponse>
<m:Worker><xsl:value-of select="EmpID"/></m:Worker>
<m:Location><xsl:value-of select="City"/></m:Location>
<xsl:for-each select="Base">
<m:Expense>
<m:ExpenseType><xsl:value-of select="BaseType"/></m:ExpenseType>
<m:Amount><xsl:value-of select="BaseAmount"/></m:Amount>
</m:Expense>
</xsl:for-each>
</m:GetQuotationResponse>
</SOAP-ENV:Body>
</xsl:template>
<xsl:template match="Worker[not(City = ('SFO', 'NYC'))]"/>
</xsl:stylesheet>
Here is what alternate solution returns. I'm not sure how this solution can be modified to get expected output since City='SFO' has different sibling node <Base> than City='NYC'
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<SOAP-ENV:Body xmlns:SOAP-ENV="http://www.w3.org/2001/12/soap-envelope">
<m:GetQuotationResponse xmlns:m="http://www.example.com">
<m:Worker>12345</m:Worker>
<m:Location>NYC</m:Location>
<m:Expense>
<m:ExpenseType>Meal</m:ExpenseType>
<m:Amount>150</m:Amount>
</m:Expense>
<m:Expense>
<m:ExpenseType>Gym</m:ExpenseType>
<m:Amount>200</m:Amount>
</m:Expense>
</m:GetQuotationResponse>
</SOAP-ENV:Body>
<SOAP-ENV:Body xmlns:SOAP-ENV="http://www.w3.org/2001/12/soap-envelope">
<m:GetQuotationResponse xmlns:m="http://www.example.com">
<m:Worker>56789</m:Worker>
<m:Location>SFO</m:Location>
</m:GetQuotationResponse>
</SOAP-ENV:Body>
</Root>
Following part is missing from the output
<m:Expense>
<m:ExpenseType>General</m:ExpenseType>
<m:Amount>1000</m:Amount>
</m:Expense>
Alternate solution suggested by #Martin Honnen with some minor tweaks also provide desired output.
<?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"
xmlns:SOAP-ENV = "http://www.w3.org/2001/12/soap-envelope"
xmlns:m = "http://www.example.com"
exclude-result-prefixes="xs m SOAP-ENV"
version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="Workers">
<Root>
<xsl:apply-templates select="Worker[City = ('SFO', 'NYC')]"/>
</Root>
</xsl:template>
<xsl:template match="Worker">
<SOAP-ENV:Body xmlns:m = "http://www.example.com">
<m:GetQuotationResponse>
<m:Worker><xsl:value-of select="EmpID"/></m:Worker>
<m:Location><xsl:value-of select="City"/></m:Location>
<xsl:for-each select="Allowance">
<m:Expense>
<m:ExpenseType><xsl:value-of select="Type"/></m:ExpenseType>
<m:Amount><xsl:value-of select="Amount"/></m:Amount>
</m:Expense>
</xsl:for-each>
<xsl:for-each select="Base"> <!-- This part was included to get desired output without having to use more than one templates -->
<m:Expense>
<m:ExpenseType><xsl:value-of select="BaseType"/></m:ExpenseType>
<m:Amount><xsl:value-of select="BaseAmount"/></m:Amount>
</m:Expense>
</xsl:for-each>
</m:GetQuotationResponse>
</SOAP-ENV:Body>
</xsl:template>
</xsl:stylesheet>
Start with a template
<xsl:template match="Workers">
<Root>
<xsl:apply-templates/>
</Root>
</xsl:template>
then map your selected Workers to a SOAP body with a single template using e.g. <xsl:template match="Worker[City = ('SFO', 'NYC')]"> or <xsl:template match="Worker[City = 'SFO'] | Worker[City = 'NYC']">, if you prefer.
For other Workers, set up an empty template e.g. <xsl:template match="Worker[not(City = ('SFO', 'NYC'))]"/>.
As an alternative, you can of course just use a template matching Worker to map to a SOAP body and make your desired selection in the apply-templates of the first template I have show, i.e. change that to <xsl:apply-templates select="Worker[City = ('SFO', 'NYC')]"/>, that way you also ensure that only the wanted Workers are processed e.g.
<xsl:template match="Workers">
<Root>
<xsl:apply-templates select="Worker[City = ('SFO', 'NYC')]"/>
</Root>
</xsl:template>
<xsl:template match="Worker">
<SOAP-ENV:Body xmlns:m = "http://www.example.com">
<m:GetQuotationResponse>
<m:Worker><xsl:value-of select="EmpID"/></m:Worker>
<m:Location><xsl:value-of select="City"/></m:Location>
<xsl:for-each select="Allowance">
<m:Expense>
<m:ExpenseType><xsl:value-of select="Type"/></m:ExpenseType>
<m:Amount><xsl:value-of select="Amount"/></m:Amount>
</m:Expense>
</xsl:for-each>
</m:GetQuotationResponse>
</SOAP-ENV:Body>
</xsl:template>
I have to generate the output in sequence and so I wanted to know how to access the variable defined under For-each loop/If condition and then value of select inside another for loop.
As per my example how to access partn and date3? Please help and suggest.
what is the concept for achieving the same..I have tried with-param as well, but didn't work for me.
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:func="myfunc"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions" >
<xsl:output method="text" encoding="utf-8" />
<xsl:output omit-xml-declaration="yes" />
<xsl:param name="break" select="'
'" />
<xsl:template match="ZGS/ID">
<xsl:for-each select="E1">
<xsl:if test="PA = 'CE'">
<xsl:variable name="partn" select="PAN"/>
</xsl:if>
</xsl:for-each>
<xsl:for-each select="E13">
<xsl:if test="ID = 033">
<xsl:variable name="date3"
select="substring(DAT,3,8)"/>
</xsl:if>
</xsl:for-each>
<xsl:for-each select="E1E">
<xsl:text>823</xsl:text>
<xsl:text>03</xsl:text>
<xsl:for-each select="E1ED">
<xsl:if test="QU = 012 ">
<xsl:value-of select="BEL"/>
</xsl:if>
</xsl:for-each>
<xsl:value-of select="$partn"/>
<xsl:value-of select="$date3"/>
</xsl:for-each>
</xsl:template>
INPUT:
<?xml version='1.0' encoding='utf-8'?>
<ZGS>
<ID BEGIN="1">
<E1 SEGMENT="1">
<PA>AG</PA>
<NAME>ABC</NAME>
<SP>E</SP>
<AND>0004</AND>
</E1>
<E1 SEGMENT="1">
<PA>RE</PA>
<PAN>IUIOP</PAN>
<NAME>ABC1</NAME>
<SP>EQ</SP>
<AND>0005</AND>
<EKA3 SEGMENT="1">
<QU>009</QU>
</EKA3>
</E1>
<E1 SEGMENT="1">
<PA>CE</PA>
<PAN>PODW</PAN>
<NAME>ABC2</NAME>
<SP>EP</SP>
<AND>0006</AND>
</E1>
<E13 SEGMENT="1">
<ID>001</ID>
<DAT>20190329</DAT>
</E13>
<E13 SEGMENT="1">
<ID>002</ID>
<DAT>20190429</DAT>
</E13>
<E13 SEGMENT="1">
<IDD>033</IDD>
<DAT>20190529</DAT>
</E13>
<E1E>
<E1ED>
</E1ED>
<E1ED>
</E1ED>
</E1E>
In XSLT, variables once declared/defined, they cannot be changed. And exists only in the loop they are defined.
You might not need the xsl:for-each loop here. Instead the variables can be globally defined, so that you can use them where you want in your xslt.
You can try the following:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:func="myfunc"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="text" encoding="utf-8" />
<xsl:output omit-xml-declaration="yes" />
<xsl:param name="break" select="'
'" />
<xsl:variable name="partn" select="/ZGS/ID/E1[PA = 'CE']/PAN" />
<xsl:variable name="date3" select="substring(/ZGS/ID/E13[ID = '033']/DAT,3,8)" />
<xsl:template match="ZGS/ID">
<xsl:for-each select="E1E">
<xsl:text>823</xsl:text>
<xsl:text>03</xsl:text>
<xsl:for-each select="E1ED">
<xsl:if test="QU = 012 ">
<xsl:value-of select="BEL" />
</xsl:if>
</xsl:for-each>
<xsl:value-of select="$partn" />
<xsl:value-of select="$date3" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/jyRYYiu
Using xsl:param, it can be achieved as
<xsl:param name="partn" select="/ZGS/ID/E1[PA = 'CE']/PAN" />
<xsl:param name="date3" select="substring(/ZGS/ID/E13[ID = '033']/DAT,3,8)" />
<xsl:template match="ZGS/ID">
<xsl:for-each select="E1E">
<xsl:text>823</xsl:text>
<xsl:text>03</xsl:text>
<xsl:for-each select="E1ED">
<xsl:if test="QU = 012 ">
<xsl:value-of select="BEL" />
</xsl:if>
</xsl:for-each>
<xsl:for-each select="$partn">
<xsl:value-of select="." />
</xsl:for-each>
<xsl:value-of select="$date3" />
</xsl:for-each>
</xsl:template>
https://xsltfiddle.liberty-development.net/jyRYYiu/1
I have a source document:
<?xml version = "1.0" encoding = "UTF-8"?>
<Circuit revision="B" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Block name="Design">
<Data>
<Layer name="INDEX_4" Function="PLANE"></Layer>
<Layer name="INDEX_9" Function="CORE"></Layer>
<Group name="PRIMARY">
<SubGroup name="GROUP_PRIMARY">
<SubLayer Ref="INDEX_4" thickness="0.0350"></SubLayer>
<SubLayer Ref="INDEX_9" thickness="0.1000"></SubLayer>
</SubGroup>
</Group>
</Data>
</Block>
</Circuit>
This stylesheet using current() extracts the corresponding thickness:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="/Circuit/Block/Data/Layer">
<xsl:element name="{name()}">
<xsl:attribute name="id">
<xsl:value-of select="#name"/>
</xsl:attribute>
<xsl:attribute name="Function">
<xsl:value-of select="#Function"/>
</xsl:attribute>
<xsl:value-of select="/Circuit/Block/Data/Group/SubGroup/SubLayer[#Ref=current()/#name]/#thickness"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Here's the second stylesheet to extract the PREVIOUS thickness value:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="/Circuit/Block/Data/Layer">
<xsl:element name="{name()}">
<xsl:attribute name="id">
<xsl:value-of select="#name"/>
</xsl:attribute>
<xsl:attribute name="Function">
<xsl:value-of select="#Function"/>
</xsl:attribute>
<xsl:value-of select="/Circuit/Block/Data/Group/SubGroup/SubLayer[#Ref=preceding-sibling::*[1]/#name]/#thickness"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
In both cases I get this:
<?xml version="1.0" encoding="UTF-8"?>
<Layer id="INDEX_4" Function="PLANE">0.0350</Layer>
<Layer id="INDEX_9" Function="CORE">0.1000</Layer>
I tried the preceding-sibling to get the preceding value - I guess the question is "What is the context of the preceding-sibling?"
Is it the group with the Layer element or the group with the SubLayer element?
Thanks
Ralph B
Within any predicate the context item is the selected item in the step before the predicate so with SubLayer[#Ref=preceding-sibling::*[1]/#name] the context for both #Ref as well as preceding-sibling::*[1]/#name is the SubLayer element. If you want to select relative to the currently matched Layer element in the template you need to use current()/preceding-sibling::*[1]/#name inside the predicate.
I have an issue in key function, key function not running in following code.
my input is
XML (Keys.xml)
<?xml version="1.0" encoding="UTF-8"?>
<Keys>
<Key year="2001" name="ABC"/>
<Key year="2002" name="BCA"/>
</Keys>
XML to convert
<?xml version="1.0" encoding="UTF-8"?>
<p>
<text> .. .. <key>ABC</key> ...</text>
<text> .. .. <key>BCA</key> ...</text>
</p>
XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:key name="keydata" match="*[name() = document('keys.xml')]/Keys/Key" use="#name"/>
<xsl:template match="key">
<xsl:copy>
<xsl:attribute name="ref"><xsl:value-of select="key('keydata', .)/#year"/></xsl:attribute>
<xsl:value-of select="."/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Output
<p>
<text> .. .. <key ref="">ABC</key> ...</text>
<text> .. .. <key ref="">BCA</key> ...</text>
</p>
Desired Ouput
<p>
<text> .. .. <key ref="2001">ABC</key> ...</text>
<text> .. .. <key ref="2002">BCA</key> ...</text>
</p>
This should work:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:variable name="keys" select="document('keys.xml')" as="document-node()"/>
<xsl:key name="keydata" match="Key" use="#name"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="key">
<xsl:copy>
<xsl:attribute name="ref"><xsl:value-of select="$keys/key('keydata', current())/#year"/></xsl:attribute>
<xsl:value-of select="."/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
In the XSL specification, there's an example entitled "Example: Using Keys to Reference other Documents" that exactly matches your use case.
This is the resulting document:
<?xml version="1.0" encoding="UTF-8"?>
<p>
<text> .. .. <key ref="2001">ABC</key> ...</text>
<text> .. .. <key ref="2002">BCA</key> ...</text>
</p>
Change <xsl:key name="keydata" match="*[name() = document('keys.xml')]/Keys/Key" use="#name"/> to <xsl:key name="keydata" match="Keys/Key" use="#name"/> and then <xsl:attribute name="ref"><xsl:value-of select="key('keydata', .)/#year"/></xsl:attribute> to <xsl:attribute name="ref" select="key('keydata', . doc('Keys.xml'))/#year"/></xsl:attribute>.
below one is the XML input,
<?xml version="1.0" encoding="utf-8"?>
<GSK_Canonical_MESGX2>
<header SEGMENT="1">
<orderNumber>002001454979</orderNumber>
<batchNumber>0000617944</batchNumber>
<BOM SEGMENT="1">
<operationNumber>0030</operationNumber>
<phaseIndicator>0011</phaseIndicator>
</BOM>
<BOM SEGMENT="1">
<operationNumber>0040</operationNumber>
<phaseIndicator>0012</phaseIndicator>
</BOM>
<recipe SEGMENT="1">
<phase>0011</phase>
<parentOperation>0030</parentOperation>
<workcenter>MANUOHD1</workcenter>
</recipe>
<recipe SEGMENT="1">
<phase>0012</phase>
<parentOperation>0040</parentOperation>
<workcenter>COSTOHD1</workcenter>
</recipe>
</header>
</GSK_Canonical_MESGX2>
I have an below xslt,
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" xmlns:set="http://exslt.org/sets" xmlns:str="http://exslt.org/strings" xmlns:java="http://xml.apache.org/xslt/java" xmlns:saxon="http://saxon.sf.net/" exclude-result-prefixes="exsl set str java saxon">
<xsl:output method="text"/>
<xsl:variable name="VarHash" select="'#'"/>
<xsl:variable name="VarBreak" select="'
'"/>
<xsl:variable name="pipeFieldDelimiter" select="'\|'"/>
<xsl:template match="/">
<xsl:text>HEADER</xsl:text>
<xsl:value-of select="$VarHash"/>
<xsl:value-of select="GSK_Canonical_MESGX2/header/orderNumber"/>
<xsl:value-of select="$VarHash"/>
<xsl:value-of select="GSK_Canonical_MESGX2/header/batchNumber"/>
<xsl:value-of select="$VarBreak"/>
<xsl:for-each select="GSK_Canonical_MESGX2/header/BOM">
<!--GSK_Canonical_MESGX2/Header/BOM/OperationNumber = GSK_Canonical_MESGX2/header/recipe/parentOperation and GSK_Canonical_MESGX2/Header/BOM/phaseIndicator = GSK_Canonical_MESGX2/header/recipe/phase then <xsl:value-of select="GSK_Canonical_MESGX2/header/recipe/workcenter"/> This needs to be implemented for each line item of BOM tag -->
<xsl:if test="position() != last()">
<xsl:value-of select="$VarBreak"/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
below one is the expected output,
HEADER#002001454979#0000617944
MANUOHD1
COSTOHD1
now need to implement for each BOM line item,we need to compare BOM with Recipe tags and select workcenter value if the condition satisfied.
Header/BOM/OperationNumber = header/recipe/parentOperation
and
Header/BOM/phaseIndicator = header/recipe/phase
then
<xsl:value-of select="GSK_Canonical_MESGX2/header/recipe/workcenter"/>
Please help me to achieve this.Thanks
This transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:key name="kBomByPhaseAndOperation" match="BOM"
use="concat(operationNumber, '|', phaseIndicator)"/>
<xsl:template match=
"recipe[key('kBomByPhaseAndOperation',
concat(parentOperation, '|', phase))
]">
<xsl:value-of select="concat('
', workcenter)"/>
</xsl:template>
<xsl:template match="header">
<xsl:text>HEADER</xsl:text>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="orderNumber|batchNumber">
<xsl:value-of select="concat('#', .)"/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
When applied on the provided source XML document:
<GSK_Canonical_MESGX2>
<header SEGMENT="1">
<orderNumber>002001454979</orderNumber>
<batchNumber>0000617944</batchNumber>
<BOM SEGMENT="1">
<operationNumber>0030</operationNumber>
<phaseIndicator>0011</phaseIndicator>
</BOM>
<BOM SEGMENT="1">
<operationNumber>0040</operationNumber>
<phaseIndicator>0012</phaseIndicator>
</BOM>
<recipe SEGMENT="1">
<phase>0011</phase>
<parentOperation>0030</parentOperation>
<workcenter>MANUOHD1</workcenter>
</recipe>
<recipe SEGMENT="1">
<phase>0012</phase>
<parentOperation>0040</parentOperation>
<workcenter>COSTOHD1</workcenter>
</recipe>
</header>
</GSK_Canonical_MESGX2>
produces exactly the wanted, correct result:
HEADER#002001454979#0000617944
MANUOHD1
COSTOHD1