Answered by:
'Xslt' must be a valid XML error for Choose condition

Question
-
Hi Team,
Currently i have requirement to write simple xslt and use it for Development purpose, when i create to choose conditions its getting error as a Xslt' must be a valid XML error So there have format invalid issue, Could you please support to correct it, For your reference here with attached input xml and current xslt and expected output.
Input xml-
<?xml version="1.0" encoding="UTF-8"?>
<ns0:utrrada_Order xmlns:ns0="http://utrrada.com/3pl/FMS/XSDLibrary">
<Orders>
<Order></Order>
<DocType Variant="ZNB">PO</DocType>
<Action>CHG</Action>
<SoldTo>
<ID>1000</ID>
</SoldTo>
<ShipTo>
<ID>9001</ID>
</ShipTo>
<GoodsSupplier>
<ID>0000602110</ID>
<Name>NONSNOW LTD</Name>
</GoodsSupplier>
<Vendor>
<ID>0000402062</ID>
<Name>HON</Name>
</Vendor>
</Orders>
</ns0:utrrada_Order>
XSLT-
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="http://utrrada.com/3pl/FMS/XSDLibrary" exclude-result-prefixes="ns0">
<xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/>
<xsl:template match="/">
<UniversalShipment xmlns="http://www.case.com/Schemas/Universal/2011/11" version="1.1">
<Shipment>
<DataContext>
<DataTargetCollection>
<DataTarget>
<Type>OrderManagerOrder</Type>
<xsl:for-each select="ns0:utrrada_Order/Orders">
<xsl:choose>
<xsl:when test="!(ns0:utrrada_Order/Orders/Order == null || ns0:utrrada_Order/Orders/Order.equals(""))">
<xsl:value-of select="ns0:utrrada_Order/Orders/Order"/>
</:when>
</xsl:choose>
</xsl:for-each>
</DataTarget>
</DataTargetCollection>
<Company>
<Code>USA</Code>
</Company>
<EnterpriseID>EXK</EnterpriseID>
<ServerID>TST</ServerID>
</DataContext>
<xsl:for-each select="ns0:utrrada_Order/Orders/DocType">
<xsl:choose>
<xsl:when test="@Variant = 'ZNB' and DocType = 'PO'">
<CustomizedField>
<DataType>String</DataType>
<Key>PO Type</Key>
<Value>ZNB</Value>
</CustomizedField>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</Shipment>
</UniversalShipment>
</xsl:template>
</xsl:stylesheet>expected output-
<UniversalShipment xmlns="http://www.case.com/Schemas/Universal/2011/11" version="1.1">
<Shipment>
<DataContext>
<DataTargetCollection>
<DataTarget>
<Type>OrderManagerOrder</Type>
</DataTarget>
</DataTargetCollection>
<Company>
<Code>USA</Code>
</Company>
<EnterpriseID>EXK</EnterpriseID>
<ServerID>TST</ServerID>
</DataContext>
<CustomizedField>
<DataType>String</DataType>
<Key>PO Type</Key>
<Value>ZNB</Value>
</CustomizedField>
</Shipment>
</UniversalShipment>
Answers
-
Hi Mahesh,
I made few modifications to both test conditions.
It is not clear why you need two loops. The input XML has just one <Order> element.
Input XML:
<?xml version="1.0" encoding="UTF-8"?> <ns0:utrrada_Order xmlns:ns0="http://utrrada.com/3pl/FMS/XSDLibrary"> <Orders> <Order>4202623821</Order> <DocType Variant="ZNB">PO</DocType> <Action>CHG</Action> <SoldTo> <ID>1000</ID> </SoldTo> <ShipTo> <ID>9001</ID> </ShipTo> <GoodsSupplier> <ID>0000602110</ID> <Name>NONSNOW LTD</Name> </GoodsSupplier> <Vendor> <ID>0000402062</ID> <Name>HON</Name> </Vendor> </Orders> </ns0:utrrada_Order>
XSLT:
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="http://utrrada.com/3pl/FMS/XSDLibrary" exclude-result-prefixes="ns0"> <xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/> <xsl:template match="/"> <UniversalShipment xmlns="http://www.case.com/Schemas/Universal/2011/11" version="1.1"> <Shipment> <DataContext> <DataTargetCollection> <DataTarget> <Type>OrderManagerOrder</Type> <xsl:for-each select="ns0:utrrada_Order/Orders"> <xsl:choose> <xsl:when test="not(Order[.= ''])"> <Order><xsl:value-of select="Order"/></Order> </xsl:when> <xsl:otherwise> <!--No Order element--> </xsl:otherwise> </xsl:choose> </xsl:for-each> </DataTarget> </DataTargetCollection> <Company> <Code>USA</Code> </Company> <EnterpriseID>EXK</EnterpriseID> <ServerID>TST</ServerID> </DataContext> <xsl:for-each select="ns0:utrrada_Order/Orders/DocType"> <xsl:choose> <xsl:when test="@Variant = 'ZNB' and . = 'PO'"> <CustomizedField> <DataType>String</DataType> <Key>PO Type</Key> <Value>ZNB</Value> </CustomizedField> </xsl:when> </xsl:choose> </xsl:for-each> </Shipment> </UniversalShipment> </xsl:template> </xsl:stylesheet>
Output:<UniversalShipment xmlns="http://www.case.com/Schemas/Universal/2011/11" version="1.1"> <Shipment> <DataContext> <DataTargetCollection> <DataTarget> <Type>OrderManagerOrder</Type> <Order>4202623821</Order> </DataTarget> </DataTargetCollection> <Company> <Code>USA</Code> </Company> <EnterpriseID>EXK</EnterpriseID> <ServerID>TST</ServerID> </DataContext> <CustomizedField> <DataType>String</DataType> <Key>PO Type</Key> <Value>ZNB</Value> </CustomizedField> </Shipment> </UniversalShipment>
- Edited by Yitzhak Khabinsky Wednesday, September 25, 2019 4:04 AM
- Marked as answer by Maheshma Thursday, September 26, 2019 12:36 PM
All replies
-
Hi Mahesh,
I made few modifications to both test conditions.
It is not clear why you need two loops. The input XML has just one <Order> element.
Input XML:
<?xml version="1.0" encoding="UTF-8"?> <ns0:utrrada_Order xmlns:ns0="http://utrrada.com/3pl/FMS/XSDLibrary"> <Orders> <Order>4202623821</Order> <DocType Variant="ZNB">PO</DocType> <Action>CHG</Action> <SoldTo> <ID>1000</ID> </SoldTo> <ShipTo> <ID>9001</ID> </ShipTo> <GoodsSupplier> <ID>0000602110</ID> <Name>NONSNOW LTD</Name> </GoodsSupplier> <Vendor> <ID>0000402062</ID> <Name>HON</Name> </Vendor> </Orders> </ns0:utrrada_Order>
XSLT:
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="http://utrrada.com/3pl/FMS/XSDLibrary" exclude-result-prefixes="ns0"> <xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/> <xsl:template match="/"> <UniversalShipment xmlns="http://www.case.com/Schemas/Universal/2011/11" version="1.1"> <Shipment> <DataContext> <DataTargetCollection> <DataTarget> <Type>OrderManagerOrder</Type> <xsl:for-each select="ns0:utrrada_Order/Orders"> <xsl:choose> <xsl:when test="not(Order[.= ''])"> <Order><xsl:value-of select="Order"/></Order> </xsl:when> <xsl:otherwise> <!--No Order element--> </xsl:otherwise> </xsl:choose> </xsl:for-each> </DataTarget> </DataTargetCollection> <Company> <Code>USA</Code> </Company> <EnterpriseID>EXK</EnterpriseID> <ServerID>TST</ServerID> </DataContext> <xsl:for-each select="ns0:utrrada_Order/Orders/DocType"> <xsl:choose> <xsl:when test="@Variant = 'ZNB' and . = 'PO'"> <CustomizedField> <DataType>String</DataType> <Key>PO Type</Key> <Value>ZNB</Value> </CustomizedField> </xsl:when> </xsl:choose> </xsl:for-each> </Shipment> </UniversalShipment> </xsl:template> </xsl:stylesheet>
Output:<UniversalShipment xmlns="http://www.case.com/Schemas/Universal/2011/11" version="1.1"> <Shipment> <DataContext> <DataTargetCollection> <DataTarget> <Type>OrderManagerOrder</Type> <Order>4202623821</Order> </DataTarget> </DataTargetCollection> <Company> <Code>USA</Code> </Company> <EnterpriseID>EXK</EnterpriseID> <ServerID>TST</ServerID> </DataContext> <CustomizedField> <DataType>String</DataType> <Key>PO Type</Key> <Value>ZNB</Value> </CustomizedField> </Shipment> </UniversalShipment>
- Edited by Yitzhak Khabinsky Wednesday, September 25, 2019 4:04 AM
- Marked as answer by Maheshma Thursday, September 26, 2019 12:36 PM
-