Jump to content

Tricky reformatting of XML with XSLT


Recommended Posts

Hi, I'm new here and fairly new to XSLT. I have a couple of books and so far I've limped along okay. This one has me stumped - sorry if this is too long a post but I'm including my example. I'm starting with a somewhat flat XML that is the wrong format for the system to which it will be sent: <?xml version="1.0" encoding="UTF-8"?><dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2013-01-25T16:49:40"><UnitUpdateSend><location>101xxxx6</location><day>2013-01-15</day><hour>1</hour><FACTOR1>0</FACTOR1><FACTOR2>0</FACTOR2><FACTOR3>0</FACTOR3><FACTOR4>0</FACTOR4><Status>GOOD</Status><Fixed>true</Fixed></UnitUpdateSend><!-- there are actually 24 hours of these datasets --><UnitUpdateSend><location>101xxxx6</location><day>2013-01-15</day><hour>24</hour><FACTOR1>0</FACTOR1><FACTOR2>0</FACTOR2><FACTOR3>0</FACTOR3><FACTOR4>0</FACTOR4><Status>GOOD</Status><Fixed>true</Fixed></UnitUpdateSend> <UnitUpdateSend><location>102xxx</location><day>2013-01-15</day><hour>1</hour><FACTOR1>0</FACTOR1><FACTOR2>0</FACTOR2><FACTOR3>0</FACTOR3><FACTOR4>0</FACTOR4><Status>GOOD</Status><Fixed>true</Fixed></UnitUpdateSend><!-- Hours 2 through 23 are skipped here --><UnitUpdateSend><location>102xxx</location><day>2013-01-15</day><hour>24</hour><FACTOR1>0</FACTOR1><FACTOR2>0</FACTOR2><FACTOR3>0</FACTOR3><FACTOR4>0</FACTOR4><Status>GOOD</Status><Fixed>true</Fixed></UnitUpdateSend></dataroot> And it needs to look like: <?xml version="1.0"?> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Header/> <env:Body> <SubmitRequest xmlns="http://somesite.com/xxxx/xml"> <UnitUpdate location="101xxx" day="2013-01-15"> <UnitUpdateHourly hour="01"> <ParameterA FACTOR2="0" FACTOR1="0"/> <ParameterB FACTOR4="0" FACTOR3="0"/> <Status>GOOD</Status> <Fixed>True</Fixed> </UnitUpdateHourly> <!-- Hours 2 through 23 skipped --> <UnitUpdateHourly hour="24"> <ParameterA FACTOR2="0" FACTOR1="0"/> <ParameterB FACTOR4="0" FACTOR3="0"/> <Status>GOOD</Status> <Fixed>True</Fixed> </UnitUpdateHourly> </UnitUpdate> <UnitUpdate location="102xxx" day="2013-01-15"> <UnitUpdateHourly hour="01"> <ParameterA FACTOR2="0" FACTOR1="0"/> <ParameterB FACTOR4="0" FACTOR3="0"/> <Status>GOOD</Status> <Fixed>True</Fixed> </UnitUpdateHourly> <!-- hours 2 through 23 skipped --> <UnitUpdateHourly hour="24"> <ParameterA FACTOR2="0" FACTOR1="0"/> <ParameterB FACTOR4="0" FACTOR3="0"/> <Status>GOOD</Status> <Fixed>True</Fixed> </UnitUpdateHourly> </UnitUpdate> </SubmitRequest> </env:Body> </env:Envelope> And so far I have the following XSLT: <?xml version='1.0' ?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"><xsl:text></xsl:text> <env:Header/><xsl:text></xsl:text><env:Body><xsl:text></xsl:text><SubmitRequest xmlns="http://somesite.com/xxxx/xml"><xsl:text></xsl:text> <xsl:template match="UnitUpdateSend"><xsl:element name="UnitUpdate"> <xsl:attribute name="location"><xsl:value-of select="location"/> </xsl:attribute> <xsl:attribute name="day"><xsl:value-of select="day"/> </xsl:attribute><xsl:text></xsl:text> </xsl:text><xsl:element name="UnitUpdateHourly"> <xsl:attribute name="hour"><xsl:number value="hour" format="01"/></xsl:attribute><xsl:text></xsl:text><xsl:element name="ParameterA"> <xsl:attribute name="Factor2"><xsl:value-of select="FACTOR2"/></xsl:attribute> <xsl:attribute name="Factor1"><xsl:value-of select="FACTOR1"/></xsl:attribute> </xsl:element><xsl:text> </xsl:text> <xsl:element name="ParameterB"> <xsl:attribute name="Factor4"><xsl:value-of select="FACTOR4"/></xsl:attribute> <xsl:attribute name="Factor3"><xsl:value-of select="FACTOR3"/></xsl:attribute> </xsl:element><xsl:text> </xsl:text> <Status><xsl:value-of select="Status"/></Status><xsl:text></xsl:text> <Fixed><xsl:value-of select="Fixed"/></FixedGen><xsl:text></xsl:text> </xsl:element><xsl:text></xsl:text> </xsl:element></xsl:text></SubmitRequest> <xsl:text></xsl:text></env:Body><xsl:text></xsl:text></env:Envelope></xsl:template></xsl:stylesheet> Which results in:<UnitUpdate location="101xxx" day="2013-01-15"> <UnitUpdateHourly hour="01"> <ParameterA Factor1="0.5" Factor2="0.5"/> <ParamenterB Factor3="0.5" Factor4="0.5"/> <Status>GOOD</Status> <Fixed>true</Fixed> </UnitUpdateHourly></UnitUpdate>for each and every hour! I can't figure out how to make the hourly elements (<UnitUpdateHourly>) a child of the <UnitUpdate> so that it doesn't repeat for each dataset. In practice, this will generate XML for dozens of locations so while the file will work it will be much larger than it need be and just isn't the format I'm supposed to produce. Anybody have the solution? I know large posts can be tedious for other members: sorry if this is too much! If someone will take pity on me and point me in the right direction I will be very grateful! I'll throw in a bottle of your favorite beverage! Thanks in advance.Flipper

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...