I am new to xml and xsl transformation, I have an xml file which need to be transformed by xslt mapping and my input file is like
<?xml version="1.0" encoding="UTF-8"?>
<UBLExtensions>
<Name xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 ..\..\XML\Inter\Namefile_01.xsd" xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:sac="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">
<ext:ExtensionAgencyURI>urn:invoice:hr:issueplace</ext:ExtensionAgencyURI>
<ext:ExtensionContent>
<ext:InvoiceIssuePlace>London</ext:InvoiceIssuePlace>
</ext:ExtensionContent>
</Name>
</UBLExtensions>
I want to generate an output xml with few more header fields,
the output file needs like
<?xml version="1.0" encoding="UTF-8"?>
<Header>
<details>
<author>Knorr, Stefan</author>
<title>Creepy Crawlies</title>
<genre>Horror</genre>
<price>4.95</price>
<publish_date>2000-12-06</publish_date>
<description>An anthology of horror stories about roaches,
centipedes, scorpions and other insects.</description>
</details>
<UBLExtensions>
<Name xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 ..\..\XML\Inter\Namefile_01.xsd" xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:sac="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">
<ext:ExtensionAgencyURI>urn:invoice:hr:issueplace</ext:ExtensionAgencyURI>
<ext:ExtensionContent>
<ext:InvoiceIssuePlace>London</ext:InvoiceIssuePlace>
</ext:ExtensionContent>
</Name>
</UBLExtensions>
</Header>
Use
<xsl:template match="/">
<Header>
<details>
<author>Knorr, Stefan</author>
<title>Creepy Crawlies</title>
<genre>Horror</genre>
<price>4.95</price>
<publish_date>2000-12-06</publish_date>
<description>An anthology of horror stories about roaches,
centipedes, scorpions and other insects.</description>
</details>
<xsl:copy-of select="node()"/>
</Header>
</xsl:template>
Related
I have records in database which has same order header with all lines records
Could anybody please help me how to use xsl to transform
<?xml version='1.0' encoding='UTF-8'?>
<getParentChildOutputCollection xmlns="http://xmlns.oracle.com/cloud/adapter/dbaasdatabase/getParentChild_REQUEST/types">
<getParentChildOutput>
<PID>1</PID>
<PNAME>Xerox</PNAME>
<CID>101</CID>
<CNAME>Order 101</CNAME>
<CDESC>Order Paper</CDESC>
</getParentChildOutput>
<getParentChildOutput>
<PID>1</PID>
<PNAME>Xerox</PNAME>
<CID>102</CID>
<CNAME>Order 102</CNAME>
<CDESC>Order Black Ink</CDESC>
</getParentChildOutput>
<getParentChildOutput>
<PID>1</PID>
<PNAME>Xerox</PNAME>
<CID>103</CID>
<CNAME>Order 103</CNAME>
<CDESC>Order Staple Pin</CDESC>
</getParentChildOutput>
<getParentChildOutput>
<PID>2</PID>
<PNAME>HP</PNAME>
<CID>230</CID>
<CNAME>Order 230</CNAME>
<CDESC>Order Red Ink</CDESC>
</getParentChildOutput>
<getParentChildOutput>
<PID>2</PID>
<PNAME>HP</PNAME>
<CID>231</CID>
<CNAME>Order 231</CNAME>
<CDESC>Order Blue Ink</CDESC>
</getParentChildOutput>
</getParentChildOutputCollection>
I want to transform above sml using xsl to below output
<?xml version="1.0" encoding="utf-8"?>
<request-wrapper>
<TransmissionID>1234</TransmissionID>
<DeliveryOrders>
<OrderCode>1</OrderCode>
<Company>Xerox</Company>
<Lines>
<c1d>101</c1d>
<cname>Order 101</cname>
<cdesc>Order Paper</cdesc>
</Lines>
<Lines>
<c1d>102</c1d>
<cname>Order 102</cname>
<cdesc>Order Black Ink</cdesc>
</Lines>
<Lines>
<c1d>3</c1d>
<cname>Order 103</cname>
<cdesc>Order Staple Pin</cdesc>
</Lines>
</DeliveryOrders>
<DeliveryOrders>
<OrderCode>2</OrderCode>
<Company>p2</Company>
<Lines>
<c1d>230</c1d>
<cname>Order 230</cname>
<cdesc>Order Red Ink</cdesc>
</Lines>
<Lines>
<c1d>231</c1d>
<cname>Order 231</cname>
<cdesc>Order Blue Ink</cdesc>
</Lines>
</DeliveryOrders>
</request-wrapper>
I have used the below xsl but in the application I am working it is not working, it is not recognize the key and generate-id commands. Are there any other way I can acheive this?
<?xml version = '1.0' encoding = 'UTF-8'?>
<xsl:stylesheet version="2.0" xml:id="id_1" xmlns:nssrcmpr="http://www.oracle.com/2014/03/ics/schedule" xmlns:nstrgdfl="http://xmlns.oracle.com/cloud/adapter/ftp/writejson/types" xmlns:oraext="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.ExtFunc" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:plnk="http://schemas.xmlsoap.org/ws/2003/05/partner-link/" xmlns:xp20="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.Xpath20" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:nstrgmpr="http://xmlns.oracle.com/cloud/adapter/ftp/writejson_REQUEST/types" xmlns:ora="http://schemas.oracle.com/xpath/extension" xmlns:oracle-xsl-mapper="http://www.oracle.com/xsl/mapper/schemas" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:ns1="http://xml.oracle.com/adapters/extension" xmlns:ns5="http://xmlns.oracle.com/cloud/adapter/ftp/writejson_REQUEST" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes=" nssrcmpr oraext xsd xp20 ora oracle-xsl-mapper xsi fn ns1 xsl ignore01" xmlns:ignore01="http://www.oracle.com/XSL/Transform/java" ignore01:ignorexmlids="true" xmlns:nsmpr0="http://xmlns.oracle.com/cloud/adapter/dbaasdatabase/getParentChild_REQUEST/types" xmlns:nsmpr1="http://www.oracle.com/2014/03/ic/integration/metadata" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:ns23="http://xmlns.oracle.com/cloud/ftp/write/response/pull" xmlns:flt="http://xmlns.oracle.com/cloud/generic/service/fault" xmlns:dvm="http://www.oracle.com/XSL/Transform/java/com.bea.wli.sb.functions.dvm.DVMFunctions" xmlns:orajs0="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.JsExecutor_xpath1453381219" xmlns:ns22="http://xml.oracle.com/types" xmlns:orajs6="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.JsExecutor_xpath1211296200" xmlns:orajs3="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.JsExecutor_xpath213937888" xmlns:orajs1="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.JsExecutor_xpath562866038" xmlns:orajs7="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.JsExecutor_xpath86288" xmlns:tns="http://xmlns.oracle.com/cloud/adapter/dbaasdatabase/getParentChild_REQUEST" xmlns:ns2="http://www.oracle.com/XSL/Transform/java/com.bea.wli.sb.resources.icsxpathfunctions.ICSInstanceTrackingFunctions" xmlns:orajs2="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.JsExecutor_xpath1600802978" xmlns:orajs5="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.JsExecutor_xpath2113524327" xmlns:orajs4="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.JsExecutor_xpath1290874520" xmlns:ns0="http://www.oracle.com/XSL/Transform/java/oracle.tip.dvm.LookupValue">
<oracle-xsl-mapper:schema xml:id="id_2">
<!--SPECIFICATION OF MAP SOURCES AND TARGETS, DO NOT MODIFY.-->
<oracle-xsl-mapper:mapSources xml:id="id_3">
<oracle-xsl-mapper:source type="XSD" xml:id="id_4">
<oracle-xsl-mapper:schema location="../../processor_13/resourcegroup_14/ICSSchedule_1.xsd" xml:id="id_5"/>
<oracle-xsl-mapper:rootElement name="schedule" namespace="http://www.oracle.com/2014/03/ics/schedule" xml:id="id_6"/>
</oracle-xsl-mapper:source>
<oracle-xsl-mapper:source type="WSDL" xml:id="id_13">
<oracle-xsl-mapper:schema location="../../application_27/inbound_28/resourcegroup_29/getParentChild_REQUEST.wsdl" xml:id="id_14"/>
<oracle-xsl-mapper:rootElement name="getParentChildOutputCollection" namespace="http://xmlns.oracle.com/cloud/adapter/dbaasdatabase/getParentChild_REQUEST/types" xml:id="id_15"/>
<oracle-xsl-mapper:param name="getParentChild" xml:id="id_16"/>
</oracle-xsl-mapper:source>
</oracle-xsl-mapper:mapSources>
<oracle-xsl-mapper:mapTargets xml:id="id_7">
<oracle-xsl-mapper:target type="WSDL" xml:id="id_8">
<oracle-xsl-mapper:schema location="../../application_49/inbound_50/resourcegroup_51/writejson_REQUEST.wsdl" xml:id="id_9"/>
<oracle-xsl-mapper:rootElement name="WriteFile" namespace="http://xmlns.oracle.com/cloud/adapter/ftp/writejson_REQUEST/types" xml:id="id_10"/>
</oracle-xsl-mapper:target>
</oracle-xsl-mapper:mapTargets>
<!--GENERATED BY ORACLE XSL MAPPER 12.1.2.0.0-->
</oracle-xsl-mapper:schema>
<!--User Editing allowed BELOW this line - DO NOT DELETE THIS LINE-->
<xsl:param name="getParentChild" xml:id="id_25"/>
<xsl:key name="keyHeader" match="nsmpr0:getParentChildOutput" use="nsmpr0:PID" />
<xsl:key name="keyLines" match="nsmpr0:getParentChildOutput" use="concat(nsmpr0:PID,'#',nsmpr0:CID)" />
<xsl:template match="/" xml:id="id_11">
<nstrgmpr:WriteFile xml:id="id_12">
<nstrgdfl:request-wrapper xml:id="id_31">
<!--<xsl:for-each xml:id="id_33" select="$getParentChild/nsmpr0:getParentChildOutputCollection/nsmpr0:getParentChildOutput">-->
<xsl:for-each xml:id="id_33" select="row[generate-id() = generate-id(key('keyHeader', nsmpr0:PID)[1])]"> <!--Sreejit 1 -->
<nstrgdfl:DeliveryOrders xml:id="id_34">
<nstrgdfl:OrderCode xml:id="id_40">
<xsl:value-of xml:id="id_41" select="nsmpr0:PID"/>
</nstrgdfl:OrderCode>
<nstrgdfl:Company xml:id="id_38">
<xsl:value-of xml:id="id_42" select="nsmpr0:PNAME"/>
</nstrgdfl:Company>
<!-- <xsl:for-each xml:id="id_36" select="."> -->
<xsl:for-each xml:id="id_36" select="key('keyHeader', nsmpr0:PID)[generate-id() = generate-id(key('keyLines', concat(nsmpr0:PID,'#',nsmpr0:CID))[1])]"> <!--Sreejit 2 -->
<nstrgdfl:Lines xml:id="id_37">
<nstrgdfl:c1d xml:id="id_43">
<xsl:value-of xml:id="id_44" select="nsmpr0:CID"/>
</nstrgdfl:c1d>
<nstrgdfl:cname xml:id="id_45">
<xsl:value-of xml:id="id_46" select="nsmpr0:CNAME"/>
</nstrgdfl:cname>
<nstrgdfl:cdesc xml:id="id_47">
<xsl:value-of xml:id="id_48" select="nsmpr0:CDESC"/>
</nstrgdfl:cdesc>
</nstrgdfl:Lines>
</xsl:for-each>
</nstrgdfl:DeliveryOrders>
</xsl:for-each>
</nstrgdfl:request-wrapper>
</nstrgmpr:WriteFile>
</xsl:template>
</xsl:stylesheet>
Regards,
Sree
Your instruction:
<xsl:for-each xml:id="id_33" select="row[generate-id() = generate-id(key('keyHeader', nsmpr0:PID)[1])]">
selects nothing because there is no element named row in the current context (or in the entire input document, for that matter).
Side note: your stylesheet is tagged version="2.0" yet you are attempting to use Muenchian grouping - which makes very little sense.
I pasted the code which worked, just for others in case anybody want the code
<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version="2.0" xml:id="id_1" xmlns:nssrcmpr="http://www.oracle.com/2014/03/ics/schedule" xmlns:nstrgdfl="http://xmlns.oracle.com/cloud/adapter/ftp/writejson/types" xmlns:oraext="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.ExtFunc" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:plnk="http://schemas.xmlsoap.org/ws/2003/05/partner-link/" xmlns:xp20="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.Xpath20" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:nstrgmpr="http://xmlns.oracle.com/cloud/adapter/ftp/writejson_REQUEST/types" xmlns:ora="http://schemas.oracle.com/xpath/extension" xmlns:oracle-xsl-mapper="http://www.oracle.com/xsl/mapper/schemas" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:ns1="http://xml.oracle.com/adapters/extension" xmlns:ns5="http://xmlns.oracle.com/cloud/adapter/ftp/writejson_REQUEST" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes=" nssrcmpr oraext xsd xp20 ora oracle-xsl-mapper xsi fn ns1 xsl ignore01" xmlns:ignore01="http://www.oracle.com/XSL/Transform/java" ignore01:ignorexmlids="true" xmlns:nsmpr0="http://xmlns.oracle.com/cloud/adapter/dbaasdatabase/getParentChild_REQUEST/types" xmlns:nsmpr1="http://www.oracle.com/2014/03/ic/integration/metadata" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:ns23="http://xmlns.oracle.com/cloud/ftp/write/response/pull" xmlns:flt="http://xmlns.oracle.com/cloud/generic/service/fault" xmlns:dvm="http://www.oracle.com/XSL/Transform/java/com.bea.wli.sb.functions.dvm.DVMFunctions" xmlns:orajs0="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.JsExecutor_xpath1453381219" xmlns:ns22="http://xml.oracle.com/types" xmlns:orajs6="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.JsExecutor_xpath1211296200" xmlns:orajs3="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.JsExecutor_xpath213937888" xmlns:orajs1="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.JsExecutor_xpath562866038" xmlns:orajs7="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.JsExecutor_xpath86288" xmlns:tns="http://xmlns.oracle.com/cloud/adapter/dbaasdatabase/getParentChild_REQUEST" xmlns:ns2="http://www.oracle.com/XSL/Transform/java/com.bea.wli.sb.resources.icsxpathfunctions.ICSInstanceTrackingFunctions" xmlns:orajs2="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.JsExecutor_xpath1600802978" xmlns:orajs5="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.JsExecutor_xpath2113524327" xmlns:orajs4="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.JsExecutor_xpath1290874520" xmlns:ns0="http://www.oracle.com/XSL/Transform/java/oracle.tip.dvm.LookupValue">
<oracle-xsl-mapper:schema xml:id="id_2">
<!--SPECIFICATION OF MAP SOURCES AND TARGETS, DO NOT MODIFY.-->
<oracle-xsl-mapper:mapSources xml:id="id_3">
<oracle-xsl-mapper:source type="XSD" xml:id="id_4">
<oracle-xsl-mapper:schema location="../../processor_13/resourcegroup_14/ICSSchedule_1.xsd" xml:id="id_5"/>
<oracle-xsl-mapper:rootElement name="schedule" namespace="http://www.oracle.com/2014/03/ics/schedule" xml:id="id_6"/>
</oracle-xsl-mapper:source>
<oracle-xsl-mapper:source type="WSDL" xml:id="id_13">
<oracle-xsl-mapper:schema location="../../application_27/inbound_28/resourcegroup_29/getParentChild_REQUEST.wsdl" xml:id="id_14"/>
<oracle-xsl-mapper:rootElement name="getParentChildOutputCollection" namespace="http://xmlns.oracle.com/cloud/adapter/dbaasdatabase/getParentChild_REQUEST/types" xml:id="id_15"/>
<oracle-xsl-mapper:param name="getParentChild" xml:id="id_16"/>
</oracle-xsl-mapper:source>
</oracle-xsl-mapper:mapSources>
<oracle-xsl-mapper:mapTargets xml:id="id_7">
<oracle-xsl-mapper:target type="WSDL" xml:id="id_8">
<oracle-xsl-mapper:schema location="../../application_49/inbound_50/resourcegroup_51/writejson_REQUEST.wsdl" xml:id="id_9"/>
<oracle-xsl-mapper:rootElement name="WriteFile" namespace="http://xmlns.oracle.com/cloud/adapter/ftp/writejson_REQUEST/types" xml:id="id_10"/>
</oracle-xsl-mapper:target>
</oracle-xsl-mapper:mapTargets>
<!--GENERATED BY ORACLE XSL MAPPER 12.1.2.0.0-->
</oracle-xsl-mapper:schema>
<!--User Editing allowed BELOW this line - DO NOT DELETE THIS LINE-->
<xsl:param name="getParentChild" xml:id="id_25"/>
<xsl:output method="xml" version="1.0" indent="yes" omit-xml-declaration="no"/>
<xsl:template match="/" xml:id="id_11">
<nstrgmpr:WriteFile xml:id="id_12">
<nstrgdfl:request-wrapper xml:id="id_31">
<xsl:for-each-group xml:id="id_33" select="$getParentChild/nsmpr0:getParentChildOutputCollection/nsmpr0:getParentChildOutput" group-by="nsmpr0:PID">
<nstrgdfl:DeliveryOrders xml:id="id_34">
<nstrgdfl:OrderCode xml:id="id_40">
<xsl:value-of xml:id="id_41" select="nsmpr0:PID"/>
</nstrgdfl:OrderCode>
<nstrgdfl:Company xml:id="id_38">
<xsl:value-of xml:id="id_42" select="nsmpr0:PNAME"/>
</nstrgdfl:Company>
<xsl:for-each select="fn:current-group()">
<nstrgdfl:Lines xml:id="id_37">
<nstrgdfl:c1d xml:id="id_43">
<xsl:value-of xml:id="id_44" select="nsmpr0:CID"/>
</nstrgdfl:c1d>
<nstrgdfl:cname xml:id="id_45">
<xsl:value-of xml:id="id_46" select="nsmpr0:CNAME"/>
</nstrgdfl:cname>
<nstrgdfl:cdesc xml:id="id_47">
<xsl:value-of xml:id="id_48" select="nsmpr0:CDESC"/>
</nstrgdfl:cdesc>
</nstrgdfl:Lines>
</xsl:for-each>
</nstrgdfl:DeliveryOrders>
</xsl:for-each-group>
</nstrgdfl:request-wrapper>
</nstrgmpr:WriteFile>
</xsl:template>
</xsl:stylesheet>
I am trying to replicate the example located here Click Here
However, I am unable to achieve the desired results.
The output is not at all getting generated as per desired.
The input I have used :
<?xml version="1.0" encoding="UTF-8" ?>
<Order xmlns="http://www.book.org">
<Bundle>
<authors>
<author>
<authorId>100</authorId>
<authorName>Kathisiera</authorName>
</author>
<author>
<authorId>200</authorId>
<authorName>Bates</authorName>
</author>
<author>
<authorId>300</authorId>
<authorName>Gavin King</authorName>
</author>
</authors>
<books>
<book>
<orderId>1111</orderId>
<bookName>Head First Java</bookName>
<bookAuthorId>100</bookAuthorId>
</book>
<book>
<orderId>5555</orderId>
<bookName>Head First Servlets</bookName>
<bookAuthorId>200</bookAuthorId>
</book>
<book>
<orderId>1111</orderId>
<bookName>Hibernate In Action</bookName>
<bookAuthorId>300</bookAuthorId>
</book>
</books>
</Bundle>
</Order>
**The Schema I have used in for my transformation**
<!-- begin snippet: js hide: false console: true babel: false -->
The XSLT I am using for my transformation :
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:socket="http://www.oracle.com/XSL/Transform/java/oracle.tip.adapter.socket.ProtocolTranslator"
xmlns:oracle-xsl-mapper="http://www.oracle.com/xsl/mapper/schemas"
xmlns:dvm="http://www.oracle.com/XSL/Transform/java/oracle.tip.dvm.LookupValue"
xmlns:mhdr="http://www.oracle.com/XSL/Transform/java/oracle.tip.mediator.service.common.functions.MediatorExtnFunction"
xmlns:oraxsl="http://www.oracle.com/XSL/Transform/java"
xmlns:oraext="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.ExtFunc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xp20="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.Xpath20"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xref="http://www.oracle.com/XSL/Transform/java/oracle.tip.xref.xpath.XRefXPathFunctions"
xmlns:ns0="http://www.book.org"
exclude-result-prefixes="oracle-xsl-mapper xsi xsd xsl ns0 socket dvm mhdr oraxsl oraext xp20 xref">
<oracle-xsl-mapper:schema>
<!--SPECIFICATION OF MAP SOURCES AND TARGETS, DO NOT MODIFY.-->
<oracle-xsl-mapper:mapSources>
<oracle-xsl-mapper:source type="XSD">
<oracle-xsl-mapper:schema location="../Schemas/BooksOrder.xsd"/>
<oracle-xsl-mapper:rootElement name="Order" namespace="http://www.book.org"/>
</oracle-xsl-mapper:source>
</oracle-xsl-mapper:mapSources>
<oracle-xsl-mapper:mapTargets>
<oracle-xsl-mapper:target type="XSD">
<oracle-xsl-mapper:schema location="../Schemas/BooksOrder.xsd"/>
<oracle-xsl-mapper:rootElement name="Order" namespace="http://www.book.org"/>
</oracle-xsl-mapper:target>
</oracle-xsl-mapper:mapTargets>
<!--GENERATED BY ORACLE XSL MAPPER 12.2.1.2.0(XSLT Build 161003.0739.0018) AT [THU JAN 23 13:13:32 IST 2020].-->
</oracle-xsl-mapper:schema>
<!--User Editing allowed BELOW this line - DO NOT DELETE THIS LINE-->
<xsl:key name="k" match="ns0:Order/ns0:Bundle/ns0:books/ns0:book" use="ns0:orderId"/>
<xsl:key name="a" match="ns0:Order/ns0:Bundle/ns0:books/ns0:author" use="ns0:authorId"/>
<xsl:template match="/ns0:Order">
<xsl:copy>
<xsl:apply-templates select="//ns0:books"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//ns0:books">
<xsl:apply-templates select="ns0:book[generate-id(.) = generate-id(key('k', ns0:orderId))]"/>
</xsl:template>
<xsl:template match="ns0:book">
<Bundle>
<authors>
<xsl:apply-templates select="key('a', string(key('k', ns0:orderId)/ns0:bookAuthorId ))" />
</authors>
<books>
<xsl:copy-of select="key('k', ns0:orderId)"/>
</books>
</Bundle>
</xsl:template>
<xsl:template match="ns0:author">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
However the output I am getting is :
<?xml version = '1.0' encoding = 'UTF-8'?>
<Order xmlns="http://www.book.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.book.org file:/C:/JDeveloper/mywork/SOADevelopment/XsltLearner/SOA/Schemas/BooksOrder.xsd">
<Bundle>
<authors/>
<books>
<book>
<orderId>1111</orderId>
<bookName>Head First Java</bookName>
<bookAuthorId>100</bookAuthorId>
</book>
<book>
<orderId>1111</orderId>
<bookName>Hibernate In Action</bookName>
<bookAuthorId>300</bookAuthorId>
</book>
</books>
</Bundle>
<Bundle>
<authors/>
<books>
<book>
<orderId>5555</orderId>
<bookName>Head First Servlets</bookName>
<bookAuthorId>200</bookAuthorId>
</book>
</books>
</Bundle>
</Order>
The node is not getting populated.
I have tried using copy-of to see the output for the second key but it prints nothing.
Could you please suggest me where I am going wrong.
Any Idea what I am doing incorrect?
Please help
Thanks!
The pattern for the key would <xsl:key name="a" match="ns0:Order/ns0:Bundle/ns0:authors/ns0:author" use="ns0:authorId"/>, as far as I understand your posted document sample. I would also remove the string call in any use of the key function e.g. use <xsl:apply-templates select="key('a', key('k', ns0:orderId)/ns0:bookAuthorId )"/>.
I am very new to xslt programming. Can any one help with this:
input xml:
<?xml version="1.0" encoding="UTF-8"?>
<MESSAGE>
<ER>
<MXITEMOUT xmlns="http://www.mro.com/mx/integration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" language="EN">
<Header operation="Notify" event="1">
<SenderID type="MAXIMO" majorversion="6" minorversion="0" build="02" dbbuild="V600-76">MX</SenderID>
<CreationDateTime>2005-08-15T14:28:06-05:00</CreationDateTime>
<RecipientID>EXTSYS1234</RecipientID>
<MessageID>1124</MessageID>
</Header>
<Content>
<MXITEM>
<ITEM action="Add">
<ITEMNUM>I1001</ITEMNUM>
<DESCRIPTION langenabled="1">test item</DESCRIPTION>
<ROTATING>1</ROTATING>
<LOTTYPE maxvalue="NOLOT">NOLOT</LOTTYPE>
<CAPITALIZED>0</CAPITALIZED>
<CREATEDDATE>2014-05-22T13:00:46+10:00</CREATEDDATE>
</ITEM>
</MXITEM>
</Content>
</MXITEMOUT>
</ER>
<IR>
<MXITEMOUT xmlns="http://www.mro.com/mx/integration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" language="EN">
<Header operation="Notify" event="1">
<SenderID type="MAXIMO" majorversion="6" minorversion="0" build="02" dbbuild="V600-76">MX</SenderID>
<CreationDateTime>2005-08-15T14:28:06-05:00</CreationDateTime>
<RecipientID>EXTSYS1234</RecipientID>
<MessageID>1124</MessageID>
</Header>
<Content>
<MXITEM>
<ITEM action="Add">
<ITEMNUM>I1001</ITEMNUM>
<DESCRIPTION langenabled="1">test item</DESCRIPTION>
<ROTATING>1</ROTATING>
<LOTTYPE maxvalue="NOLOT">NOLOT</LOTTYPE>
<CAPITALIZED>0</CAPITALIZED>
<CREATEDDATE>2014-05-22T13:00:46+10:00</CREATEDDATE>
</ITEM>
</MXITEM>
</Content>
</MXITEMOUT>
</IR>
</MESSAGE>
My XSLT is:
<?xml version="1.0"?>
<!--
This XSL is supposed to change the ITEMNUM tag value by prepending 001 to the existing value.
It also strips off the MESSAGE and IR wrapper tags so that the resultant data is consistent with
our XML schema
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:mro="http://www.mro.com/mx/integration" version="1.0" exclude-result-prefixes="mro">
<xsl:template match="/MESSAGE">
<xsl:apply-templates select="IR"/>
</xsl:template>
<xsl:template match="IR">
<xsl:apply-templates select="#*|*|text()"/>
</xsl:template>
<xsl:template match="#*|*|text()">
<xsl:copy>
<xsl:apply-templates select="#*|*|text()"/>
</xsl:copy>
</xsl:template>
<!--
Change the ITEMNUM tag value by prepending 001 to the existing value
-->
<xsl:template match="mro:ITEMNUM">
<xsl:element name="ITEMNUM" namespace="http://www.mro.com/mx/integration">
<xsl:text>MAX-</xsl:text><xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Current output is:
<?xml version="1.0" encoding="UTF-8"?>
<MXITEMOUT xmlns="http://www.mro.com/mx/integration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" language="EN">
<Header operation="Notify" event="1">
<SenderID type="MAXIMO" majorversion="6" minorversion="0" build="02" dbbuild="V600-76">MX</SenderID>
<CreationDateTime>2005-08-15T14:28:06-05:00</CreationDateTime>
<RecipientID>EXTSYS1234</RecipientID>
<MessageID>1124</MessageID>
</Header>
<Content>
<MXITEM>
<ITEM action="Add">
<mro:ITEMNUM xmlns:mro="http://www.mro.com/mx/integration">MAX-I1001</mro:ITEMNUM>
<DESCRIPTION langenabled="1">test item</DESCRIPTION>
<ROTATING>1</ROTATING>
<LOTTYPE maxvalue="NOLOT">NOLOT</LOTTYPE>
<CAPITALIZED>0</CAPITALIZED>
<CREATEDDATE>2014-05-22T13:00:46+10:00</CREATEDDATE>
</ITEM>
</MXITEM>
</Content>
</MXITEMOUT>
Expected output is:
<?xml version="1.0" encoding="UTF-8"?>
<MXITEMOUT xmlns="http://www.mro.com/mx/integration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" language="EN">
<Header operation="Notify" event="1">
<SenderID type="MAXIMO" majorversion="6" minorversion="0" build="02" dbbuild="V600-76">MX</SenderID>
<CreationDateTime>2005-08-15T14:28:06-05:00</CreationDateTime>
<RecipientID>EXTSYS1234</RecipientID>
<MessageID>1124</MessageID>
</Header>
<Content>
<MXITEM>
<ITEM action="Add">
<mro:ITEMNUM xmlns:mro="http://www.mro.com/mx/integration">MAX-I1001</mro:ITEMNUM>
<DESCRIPTION langenabled="1">test item</DESCRIPTION>
<ROTATING>1</ROTATING>
<LOTTYPE maxvalue="NOLOT">NOLOT</LOTTYPE>
<CAPITALIZED>0</CAPITALIZED>
<CREATEDDATE>2014-05-22T23:00:46.000Z</CREATEDDATE>
</ITEM>
</MXITEM>
</Content>
</MXITEMOUT>
I am trying find logic in google to convert date to utc format in xslt.
It is throwing strange errors.
XML date:2014-05-22T13:00:46+10:00 Expected Date: 2014-05-22T23:00:46.000Z
I have a xml document which looks like
<!-- language: lang-xml -->
<?xml version="1.0" encoding="UTF-8"?>
<Request xmlns="fst" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Header>
<AccountServer e-dtype="int">3</AccountServer>
</Header>
<Response>
<ResponseList e-dtype="list">
<Response>
<RequestId e-dtype="string">ServiceOrderGetRef</RequestId>
<RequestObjName e-dtype="string">ServiceOrder</RequestObjName>
<ServiceOrder>
<CreateDt e-dtype="dateTime">2014-03-01 00:00:00</CreateDt>
<CreateWho e-dtype="string">vc</CreateWho>
<WorkflowStartDt e-dtype="dateTime">2014-04-01 00:00:00</WorkflowStartDt>
</ServiceOrder>
</Response>
<Response>
<ComponentList e-dtype="list"/>
<Count e-dtype="int">0</Count>
<RequestId e-dtype="string">ComponentFindRef</RequestId>
<RequestObjName e-dtype="string">Component</RequestObjName>
<TotalCount e-dtype="int">0</TotalCount>
</Response>
<Response>
<Count e-dtype="int">0</Count>
<CustomerContractList e-dtype="list"/>
<RequestId e-dtype="string">CustomerContractRef</RequestId>
<RequestObjName e-dtype="string">CustomerContract</RequestObjName>
<TotalCount e-dtype="int">0</TotalCount>
</Response>
<Response>
<Count e-dtype="int">0</Count>
<ProductList e-dtype="list"/>
<RequestId e-dtype="string">ProductRef</RequestId>
<RequestObjName e-dtype="string">Product</RequestObjName>
<TotalCount e-dtype="int">0</TotalCount>
</Response>
<Response>
<Count e-dtype="int">0</Count>
<NrcList e-dtype="list"/>
<RequestId e-dtype="string">NrcFindRef</RequestId>
<RequestObjName e-dtype="string">Nrc</RequestObjName>
<TotalCount e-dtype="int">0</TotalCount>
</Response>
</ResponseList>
</Response>
</Request>
I am using copy-of function to copy node ServiceOrder within another xml document
I want to modify text of node WorkFlowStartDt and CreateDt and then do a copy-of. How can I do this?
My copied serviceorder node should look like this after modifing text. Below is the result xml
<?xml version="1.0" encoding="UTF-8"?>
<Request>
<Header>
<OperatorName e-dtype="string">ws</OperatorName>
<ApplicationName e-dtype="string">ws</ApplicationName>
</Header>
<CustomerUdtRequest>
<RequestList e-dtype="list">
<LogicalServiceOrder>
<RequestId e-dtype="string">MyExistingOrder</RequestId>
<LogicalServiceOrderPreProcess>
<Fetch e-dtype="boolean">true</Fetch>
<Order>
<AccountInternalId e-dtype="int">12345</AccountInternalId>
<Key>
<OrderId e-dtype="numeric">12345678</OrderId>
</Key>
</Order>
<ServiceOrderList e-dtype="list">
<ServiceOrder xmlns="fst" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance">
<CreateDt e-dtype="dateTime">2014-03-02 00:00:00</CreateDt>
<CreateWho e-dtype="string">vc</CreateWho>
<WorkflowStartDt e-dtype="dateTime">2014-05-01 00:00:00</WorkflowStartDt>
</ServiceOrder>
</ServiceOrderList>
</LogicalServiceOrderPreProcess>
</LogicalServiceOrder>
</RequestList>
</CustomerUdtRequest>
</Request>
Below is my xslt processor file
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="/">
<Request>
<Header>
<OperatorName e-dtype="string">ws</OperatorName>
<ApplicationName e-dtype="string">ws</ApplicationName>
</Header>
<CustomerUdtRequest>
<RequestList e-dtype="list">
<LogicalServiceOrder>
<RequestId e-dtype="string">MyExistingOrder</RequestId>
<LogicalServiceOrderPreProcess> <Order> <AccountInternalId e-dtype="int">
<xsl:value-of
select="/Request/Response/ResponseList/Response/ServiceOrder/AccountInternalId"/>
</AccountInternalId> <Key> <OrderId e-dtype="numeric"> <xsl:value-of select="/Request/Response/ResponseList/Response/ServiceOrder/OrderId"/>
</OrderId>
</Key>
</Order>
<ServiceOrderList e-dtype="list">
<xsl:copy-of select="/Request/Response/ResponseList/Response/ServiceOrder"/>
</ServiceOrderList>
</LogicalServiceOrderPreProcess>
</LogicalServiceOrder>
</xsl:if>
</RequestList>
</CustomerUdtRequest>
</Request>
</xsl:template>
</xsl:stylesheet>
I want to modify text of node WorkFlowStartDt and CreateDt and then do
a copy-of.
That would be an unnecessary complication. You can modify nodes while you add them to the output tree.
Since you essentially want to copy everything "as is" except two nodes, it would be best to start with an identity transform template, then add an "exception" template for the two specific nodes that need modifying (one template for both, since the modification is identical).:
<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"/>
<!-- identity transformation -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="CreateDt | WorkFlowStartDt">
<xsl:copy>
<xsl:value-of select="concat('**', ., '**')"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Added:
In response to the edited question, try this stylesheet:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="fst"
exclude-result-prefixes="ns">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<Request>
<Header>
<OperatorName e-dtype="string">ws</OperatorName>
<ApplicationName e-dtype="string">ws</ApplicationName>
</Header>
<CustomerUdtRequest>
<RequestList e-dtype="list">
<LogicalServiceOrder>
<RequestId e-dtype="string">MyExistingOrder</RequestId>
<LogicalServiceOrderPreProcess>
<Order>
<AccountInternalId e-dtype="int">
<!-- THIS DOESN'T POINT TO ANY EXISTING NODE!! -->
<xsl:value-of select="ns:Request/ns:Response/ns:ResponseList/ns:Response/ns:ServiceOrder/ns:AccountInternalId"/>
</AccountInternalId>
<Key>
<OrderId e-dtype="numeric">
<!-- THIS DOESN'T POINT TO ANY EXISTING NODE!! -->
<xsl:value-of select="ns:Request/ns:Response/ns:ResponseList/ns:Response/ns:ServiceOrder/ns:OrderId"/>
</OrderId>
</Key>
</Order>
<xsl:apply-templates select="ns:Request/ns:Response/ns:ResponseList/ns:Response/ns:ServiceOrder"/>
</LogicalServiceOrderPreProcess>
</LogicalServiceOrder>
</RequestList>
</CustomerUdtRequest>
</Request>
</xsl:template>
<xsl:template match="ns:ServiceOrder">
<xsl:copy>
<xsl:copy-of select="ns:CreateWho"/>
<xsl:apply-templates select="ns:CreateDt | ns:WorkflowStartDt"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ns:CreateDt | ns:WorkflowStartDt">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:value-of select="concat('**', ., '**')"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
When applied to your input (minus the illegal opening comment), the following result is obtained:
<?xml version="1.0" encoding="UTF-8"?>
<Request>
<Header>
<OperatorName e-dtype="string">ws</OperatorName>
<ApplicationName e-dtype="string">ws</ApplicationName>
</Header>
<CustomerUdtRequest>
<RequestList e-dtype="list">
<LogicalServiceOrder>
<RequestId e-dtype="string">MyExistingOrder</RequestId>
<LogicalServiceOrderPreProcess>
<Order>
<AccountInternalId e-dtype="int"/>
<Key>
<OrderId e-dtype="numeric"/>
</Key>
</Order>
<ServiceOrder xmlns="fst" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<CreateWho e-dtype="string">vc</CreateWho>
<CreateDt e-dtype="dateTime">**2014-03-01 00:00:00**</CreateDt>
<WorkflowStartDt e-dtype="dateTime">**2014-04-01 00:00:00**</WorkflowStartDt>
</ServiceOrder>
</LogicalServiceOrderPreProcess>
</LogicalServiceOrder>
</RequestList>
</CustomerUdtRequest>
</Request>
Here is my input XML:
<Books>
<Book>
<BookId>1</BookId>
<Des>Dumm1</Des>
<Comments/>
<OrderDateTime>04/06/2009 12:37</OrderDateTime>
</Book>
<Book>
<BookId>2</BookId>
<Des>Dummy2</Des>
<Comments/>
<OrderDateTime>04/07/2009 12:37</OrderDateTime>
</Book>
<Book>
<BookId>3</BookId>
<Des>Dumm12</Des>
<Comments/>
<OrderDateTime>05/06/2009 12:37</OrderDateTime>
</Book>
<Book>
<BookId>4</BookId>
<Des>Dummy2</Des>
<Comments/>
<OrderDateTime>06/07/2009 12:37</OrderDateTime>
</Book>
</Books>
I pass an XML param and my Input XML is
<BookIDs>
<BookID>2</BookID>
<BookID>3</BookID>
</BookIDs>
My output should be like
<Books>
<Book>
<BookId>2</BookId>
<Des>Dummy2</Des>
<Comments/>
<OrderDateTime>04/07/2009 12:37</OrderDateTime>
</Book>
<Book>
<BookId>3</BookId>
<Des>Dumm12</Des>
<Comments/>
<OrderDateTime>05/06/2009 12:37</OrderDateTime>
</Book>
</Books>
How do I accomplish this using XSLT?
This works in Saxon 6.5.5...
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.1">
<xsl:param name="nodeset">
<BookIDs><BookID>2</BookID><BookID>3</BookID></BookIDs>
</xsl:param>
<xsl:template match="/Books">
<Books>
<xsl:variable name="Copy">
<wrap>
<xsl:copy-of select="Book"/>
</wrap>
</xsl:variable>
<xsl:for-each select="$nodeset/BookIDs/BookID">
<xsl:copy-of select="$Copy/wrap/Book[BookId=current()]"/>
</xsl:for-each>
</Books>
</xsl:template>
</xsl:stylesheet>
A pure XSLT solution will be pretty brittle though. Sub-query predicates didn't work, neither did a key. It is dependent upon the param being recognized as a node-set--which I was unable to achieve with a dynamic value (as opposed to the default in my example), even with exsl:node-set. This is also wasteful in that it copies all the Book elements from the source document.
There may be a better solution in XSLT 2.0. Alternately, if you are initiating your transform with some other language/tool, there may be better approaches available there. Another possibility could include the use of exsl:document to load your source document or params.