I’m fairly new to XML and XSL Stylesheets, and I’ve been tasked with creating a stylesheet for one of our clients. I have already created a stylesheet that outputs an XML in the following format:
<Trip TripType="Normal">
<Plan BeginTime="2011-08-13T10:00:00" UserDefinedTripID="777" UserDefinedRouteID="777">
<PlanStop ArrivalTime="2011-08-13T15:30:00" ArrivalLock="true" SiteID="1" PassThru1="test1" PassThru2="test2" PassThru3="test3" PassThru4="test4">
<PlanNote Line1="Freeform Text" Line2="Line2" Line3="Line3" />
<PlanCargo Duration="60" BillID="" Weight="100" Units="100.0" XUnitTypeID="10" Action="Pick" />
<PlanNote Line1="Freeform Text" Line2="Line2" Line3="Line3" />
<PlanCargo Duration="60" BillID="" Weight="100" Units="100.0" XUnitTypeID="12" Action="Pick" />
</PlanStop>
</Plan>
</Trip>
I need to take the output and insert the contents into an attribute within the Trip element to look like this:
<Trip TripID="-1" CurrentRevisionNumber="1" IsDispatch="1" IsActive="0"
IsComplete="0" OrganizationID="4"
TripData="<Trip TripType="Normal">
<Plan BeginTime="2011-08-13T10:00:00" UserDefinedTripID="777"
UserDefinedRouteID="777">
<PlanStop ArrivalTime="2011-08-13T10:00:00" ArrivalLock=& quot;true" SiteID="1" PassThru1="test1" PassThru2=& quot;test2" PassThru3="test3" PassThru4="test4">
<PlanCargo Duration="45" BillID="" Weight=& quot;100" Units="100.0" XUnitTypeID="9" Action="Pick" />
</PlanStop> />
So in other words, I need to take an existing XML output and put it in an attribute while performing some character transformations.
Yes, it is extremely ugly, but this is how they want it. I was thinking of making another XSL that will copy over the XML output from the original XSL transformation and place it in an attribute while converting <, >, “, etc into < , > , " , etc (not sure what they’re called).
I’ve scoured the internet for solutions, but I can’t seem to find any that are quite like this (I’d imagine that’s because this is a ridiculous request). I can provide my original XSL if necessary, but I’d rather not change it, if possible.
Thanks in advance!
I would save myself time, effort and depression from others’ stupidity and will implement this with a tiny transformation that uses a tiny extension function.
Here is example using the .NET XslCompiledTransform XSLT processor:
When applied on any XML document, it produces the top element with a
stupidattribute that contains the escaped representation of the body of the top element.For example, when the transformation is applied on this XML document:
the wanted result is produced:
A transformation implementing the same idea most probably can be written for almost every XSLT processor with an extension function that itself can be written in variety of programming languages.