1 year ago
#189618
Robert Kerketta
Convert xml to json with requirement of replacing complex element with special character [
I need to get below output using xslt where results tag denotes array in output.{Ignoring Metadata using xslt}
INPUT
<container_trip><results><__metadata><id>http://hxaix48.hq.auspost.com.au:8001/sap/opu/odata/SAP/ZGW_CONTAINER_TRIP_EVENT_SRV/container_tripSet('338760110225')</id><uri>http://hxaix48.hq.auspost.com.au:8001/sap/opu/odata/SAP/ZGW_CONTAINER_TRIP_EVENT_SRV/container_tripSet('338760110225')</uri><type>ZGW_CONTAINER_TRIP_EVENT_SRV.container_trip</type></__metadata><originator><__metadata><type>ZGW_CONTAINER_TRIP_EVENT_SRV.event_originator</type></__metadata><person><__metadata><type>ZGW_CONTAINER_TRIP_EVENT_SRV.Person</type></__metadata></person><system><__metadata><type>ZGW_CONTAINER_TRIP_EVENT_SRV.System</type></__metadata></system></originator><container><__metadata><type>ZGW_CONTAINER_TRIP_EVENT_SRV.Container</type></__metadata><product_group><__metadata><type>ZGW_CONTAINER_TRIP_EVENT_SRV.Product_group</type></__metadata></product_group></container><audit><__metadata><type>ZGW_CONTAINER_TRIP_EVENT_SRV.Audit</type></__metadata><em_last_event_guid>00000000-0000-0000-0000-000000000000</em_last_event_guid></audit><device><__metadata><type>ZGW_CONTAINER_TRIP_EVENT_SRV.Device</type></__metadata><is_charging>false</is_charging><is_charged>false</is_charged><communications><__metadata><type>ZGW_CONTAINER_TRIP_EVENT_SRV.communications</type></__metadata><is_communication_available>false</is_communication_available><communication_duration>0</communication_duration></communications><location><__metadata><type>ZGW_CONTAINER_TRIP_EVENT_SRV.Location</type></__metadata><address><__metadata><type>ZGW_CONTAINER_TRIP_EVENT_SRV.Address</type></__metadata><addressline><__metadata><type>ZGW_CONTAINER_TRIP_EVENT_SRV.Addressline</type></__metadata></addressline></address><geocode><__metadata><type>ZGW_CONTAINER_TRIP_EVENT_SRV.Geocode</type></__metadata><latitude>0.000000</latitude><longitude>0.000000</longitude><height>0.000</height><positional_uncertainty>0.000000</positional_uncertainty><is_containment>false</is_containment><precision>0</precision></geocode></location></device><to_container><__metadata><type>ZGW_CONTAINER_TRIP_EVENT_SRV.To_container</type></__metadata></to_container><container_trip_id>338760110225</container_trip_id><container_trip_guid>00000000-0000-0000-0000-000000000000</container_trip_guid><container_id>TRK186345678</container_id><container_type>TRK1</container_type><container_full_barcode>80049312650TRK186345678A</container_full_barcode><container_sealed>false</container_sealed><system_item_counter>2</system_item_counter><manual_item_counter>0</manual_item_counter><container_gross_weight_kg>0.00</container_gross_weight_kg><is_active>true</is_active><available_to_load_into>true</available_to_load_into><can_be_loaded_as_child>false</can_be_loaded_as_child><security_seal_ids><results><__metadata><id> </id></__metadata><seal_id>AA0000000001</seal_id></results><results><__metadata><id> </id></__metadata><seal_id>AA0000000002</seal_id></results><results><__metadata><id> </id></__metadata><seal_id>AA0000000002</seal_id></results></security_seal_ids><trip_legs><results><__metadata><id>http://hxaix48.hq.auspost.com.au:8001/sap/opu/odata/SAP/ZGW_CONTAINER_TRIP_EVENT_SRV/trip_legsSet(container_trip_id='',origin_facility_code='201531',event_type='')</id><uri>http://hxaix48.hq.auspost.com.au:8001/sap/opu/odata/SAP/ZGW_CONTAINER_TRIP_EVENT_SRV/trip_legsSet(container_trip_id='',origin_facility_code='201531',event_type='')</uri><type>ZGW_CONTAINER_TRIP_EVENT_SRV.trip_legs</type></__metadata><origin_facility_code>201531</origin_facility_code><destination_facility_code>308371</destination_facility_code><origin_facility_name>COFFS HARBOUR DC</origin_facility_name><destination_facility_name>360 COLLINS ST PARCEL LOCKER</destination_facility_name></results></trip_legs><container_trip_event><__deferred><uri>http://hxaix48.hq.auspost.com.au:8001/sap/opu/odata/SAP/ZGW_CONTAINER_TRIP_EVENT_SRV/container_tripSet('338760110225')/container_trip_event</uri></__deferred></container_trip_event><facility><__deferred><uri>http://hxaix48.hq.auspost.com.au:8001/sap/opu/odata/SAP/ZGW_CONTAINER_TRIP_EVENT_SRV/container_tripSet('338760110225')/facility</uri></__deferred></facility></results></container_trip>
OUTPUT
{ "container_trip": [ { "originator": { "person": {}, "system": {} }, "container": { "product_group": {} }, "audit": { "em_last_event_guid": "00000000-0000-0000-0000-000000000000" }, "device": { "is_charging": "false", "is_charged": "false", "communications": { "is_communication_available": "false", "communication_duration": "0" }, "location": { "address": { "addressline": {} }, "geocode": { "latitude": "0.000000", "longitude": "0.000000", "height": "0.000", "positional_uncertainty": "0.000000", "is_containment": "false", "precision": "0" } } }, "to_container": {}, "container_trip_id": "338760110225", "container_trip_guid": "00000000-0000-0000-0000-000000000000", "container_id": "TRK186345678", "container_type": "TRK1", "container_full_barcode": "80049312650TRK186345678A", "container_sealed": "false", "system_item_counter": "2", "manual_item_counter": "0", "container_gross_weight_kg": "0.00", "is_active": "true", "available_to_load_into": "true", "can_be_loaded_as_child": "false", "security_seal_ids": [ { "seal_id": "AA0000000001" }, { "seal_id": "AA0000000002" }, { "seal_id": "AA0000000002" } ], "trip_legs": [ { "origin_facility_code": "201531", "destination_facility_code": "308371", "origin_facility_name": "COFFS HARBOUR DC", "destination_facility_name": "360 COLLINS ST PARCEL LOCKER" } ], "container_trip_event": {}, "facility": {} } ]
}
XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="utf-8"/>
<xsl:template match="/*[node()]">
<xsl:text>{</xsl:text>
<xsl:apply-templates select="." mode="detect"/>
<xsl:text>}</xsl:text>
</xsl:template>
<xsl:template match="*" mode="detect">
<xsl:variable name="localNameObj" select="local-name()"/>
<xsl:variable name="nextChildObj" select="name(*[1])"/>
<xsl:variable name="varObj" select="concat('result','s')"/>
<xsl:variable name="followingSiblingObj" select="name(following-sibling::*[1])"/>
<xsl:variable name="precedingSiblingObj" select="name(preceding-sibling::*[1])"/>
<xsl:choose>
<xsl:when test="starts-with(local-name(),'__') or parent::*[starts-with(local-name(), '__')]"> </xsl:when>
<xsl:when test="name(preceding-sibling::*[1]) = name(current()) and name(following-sibling::*[1]) != name(current())">
<xsl:apply-templates select="." mode="obj-content"/>
<xsl:text>]</xsl:text>
<xsl:if test="count(following-sibling::*[name() != name(current())]) > 0">, </xsl:if>
</xsl:when>
<xsl:when test="name(preceding-sibling::*[1]) = name(current())">
<xsl:apply-templates select="." mode="obj-content"/>
<xsl:if test="name(following-sibling::*) = name(current())">, </xsl:if>
</xsl:when>
<xsl:when test="following-sibling::*[1][name() = name(current())]">
<xsl:text>"</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>" : [</xsl:text>
<xsl:apply-templates select="." mode="obj-content"/>
<xsl:text>, </xsl:text>
</xsl:when>
<xsl:when test="count(./child::*) > 0 or count(@*) > 0">
<xsl:text>"</xsl:text>
<xsl:value-of select="name()"/>" : <xsl:apply-templates select="." mode="obj-content"/>
<xsl:if test="count(following-sibling::*) > 0">, </xsl:if>
</xsl:when>
<xsl:when test="count(./child::*) = 0">
<xsl:text>"</xsl:text>
<xsl:value-of select="name()"/>" : "<xsl:apply-templates select="."/>
<xsl:text>"</xsl:text>
<xsl:if test="count(following-sibling::*) > 0">, </xsl:if>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="*" mode="obj-content">
<xsl:variable name="localNameObj" select="local-name()"/>
<xsl:variable name="nextChildObj" select="name(*[1])"/>
<xsl:variable name="varObj" select="concat('result','s')"/>
<xsl:variable name="followingSiblingObj" select="name(following-sibling::*[1])"/>
<xsl:variable name="precedingSiblingObj" select="name(preceding-sibling::*[1])"/>
<xsl:text>{</xsl:text>
<xsl:apply-templates select="@*" mode="attr"/>
<xsl:if test="count(@*) > 0 and (count(child::*) > 0 or text())">, </xsl:if>
<xsl:apply-templates select="./*" mode="detect"/>
<xsl:if test="count(child::*) = 0 and text() and not(@*)">
<xsl:text>"</xsl:text>
<xsl:value-of select="name()"/>" : "<xsl:value-of select="text()"/>
<xsl:text>"</xsl:text>
</xsl:if>
<xsl:if test="count(child::*) = 0 and text() and @*">
<xsl:text>"text" : "</xsl:text>
<xsl:value-of select="text()"/>
<xsl:text>"</xsl:text>
</xsl:if>
<xsl:text>}</xsl:text>
<xsl:if test="position() < last()">, </xsl:if>
</xsl:template>
<xsl:template match="@*" mode="attr">
<xsl:text>"</xsl:text>
<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>
<xsl:text>"</xsl:text>
<xsl:if test="position() < last()">,</xsl:if>
</xsl:template>
<xsl:template match="node/@TEXT | text()" name="removeBreaks">
<xsl:param name="pText" select="normalize-space(.)"/>
<xsl:choose>
<xsl:when test="not(contains($pText, '
'))">
<xsl:copy-of select="$pText"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(substring-before($pText, '
'), ' ')"/>
<xsl:call-template name="removeBreaks">
<xsl:with-param name="pText" select="substring-after($pText, '
')"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
I would really appreciate if anyone can help. Thanks in advance...
json
xml
xslt
xml-to-json
0 Answers
Your Answer