943,865 Members | Top Members by Rank

Ad:
Sep 15th, 2009
0

XML to XML via XSL Transform Error...

Expand Post »
Here is a sample of the XML that I am starting with:

XML: any.xml
XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?xml-stylesheet type="text/xsl" href="any.xsl"?>
  3. <root>
  4. <device>
  5. <device_Info>
  6. <name>Name 1</name>
  7. <type>Type</type>
  8. </device_Info>
  9. <drive_Pair>
  10. <status>Ready</status>
  11. <Local>
  12. <l_name>10E5</l_name>
  13. </Local>
  14. <Remote>
  15. <l_name>0651</l_name>
  16. </Remote>
  17. </drive_Pair>
  18. #Can be 1 or many <drive_Pair>'s
  19. {<drive_Pair>
  20. ...
  21. </drive_Pair>}
  22. # Can only be 1 Totals
  23. <Totals>
  24. <Local>
  25. <l1_invalid_mbs>0.0</l1_invalid_mbs>
  26. <l2_invalid_mbs>0.0</l2_invalid_mbs>
  27. </Local>
  28. </Totals>
  29. </device>
  30. #Can be 1 or many <device>'s
  31. {<device>
  32. ...
  33. </device>}
  34. </root>

XSL: any.xsl
XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  2. <xsl:output method="xml" version="1.0" standalone="yes" cdata-section-elements="devices" indent="yes"/>
  3. <xsl:template match="/root/device">
  4. <xsl:element name="devices">
  5. <xsl:for-each select="device_Info">
  6. <device name="{name}" type="{type}"/>
  7. </xsl:for-each>
  8. <xsl:for-each select="drive_Pair">
  9. <drive_Pair status="{status}" local="{Local/l_name}" remote="{Remote/l_name}"/>
  10. </xsl:for-each>
  11. <xsl:for-each select="drive_Pair_Totals/Local">
  12. <Totals l1="{l1_invalid_mbs}" l2="{l2_invalid_mbs}"/>
  13. </xsl:for-each>
  14. </xsl:element>
  15. </xsl:template>
  16. </xsl:stylesheet>

The XML Result: temp.xml
XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2. <devices>
  3. <device name="Name 1" type="backup"/>
  4. <drive_Pair status="same" local="1234" remote="1234"/>
  5. <drive_Pair status="same" local="4321" remote="4321"/>
  6. <Totals l1="0.0" l2="0.0"/>
  7. </devices>
  8. <devices>
  9. <device name="Name 2" type="Active"/>
  10. <drive_Pair status="different" local="3425" remote="3542"/>
  11. <drive_Pair status="different" local="0362" remote="9876"/>
  12. <drive_Pair status="same" local="8767" remote="8767"/>
  13. <Totals l1="0.0" l2="0.0"/>
  14. </devices>

The issue I am having is when I validate the output XML, temp.xml it comes back with an error that happens right here (line 8 of temp.xml):
XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1. <Totals l1="0.0" l2="0.0"/>
  2. </devices>
  3. <devices> #<--------- "Character 'D' is grammatically unexpected"
  4. <device name="Name 2" type="Active"/>

It references that the reason for the error is because it expects '!' or '?'.... And I am at a loss on how to "fix" it.
Similar Threads
Reputation Points: 19
Solved Threads: 9
Junior Poster in Training
sinnerFA is offline Offline
70 posts
since Sep 2009
Sep 15th, 2009
0

Re: XML to XML via XSL Transform Error...

An XML document needs exactly one root element. So you should wrap your devices tags in another tag.
See: http://en.wikipedia.org/wiki/Root_element
Sponsor
Featured Poster
Reputation Points: 550
Solved Threads: 731
Bite my shiny metal ass!
pritaeas is offline Offline
4,177 posts
since Jul 2006
Sep 15th, 2009
1

Re: XML to XML via XSL Transform Error...

XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  2. <xsl:output method="xml" version="1.0" standalone="yes" cdata-section-elements="devices" indent="yes"/>
  3. <xsl:template match="root">
  4. <devices>
  5. <xsl:apply-templates select="device"/>
  6. </devices>
  7. </xsl:template>
  8.  
  9. <xsl:template match="device">
  10. <xsl:apply-templates select="device_Info"/>
  11. <xsl:apply-templates select="drive_Pair"/>
  12. <xsl:apply-templates select="Totals"/>
  13. </xsl:template>
  14.  
  15.  
  16. <xsl:template match="device_Info">
  17. <device name="{name}" type="{type}"/>
  18. </xsl:template>
  19. <xsl:template match="drive_Pair">
  20. <drive_Pair status="{status}" local="{Local/l_name}" remote="{Remote/l_name}"/>
  21. </xsl:template>
  22.  
  23.  
  24. <xsl:template match="Totals">
  25. <Totals l1="l1_invalid_mbs" l2="l2_invalid_mbs"/>
  26. </xsl:template>
  27. </xsl:stylesheet>
Reputation Points: 16
Solved Threads: 21
Junior Poster
xml_looser is offline Offline
178 posts
since Apr 2009
Sep 15th, 2009
0

Re: XML to XML via XSL Transform Error...

Thanks for both replies, I had setup a rootElement earlier and It kept giving me another error... But thanks for pointing out the obvious and sending an example!
Reputation Points: 19
Solved Threads: 9
Junior Poster in Training
sinnerFA is offline Offline
70 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in XML, XSLT and XPATH Forum Timeline: add xml declaration and namespace to xml output in sql
Next Thread in XML, XSLT and XPATH Forum Timeline: XSLT group size





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC