28758

Adding new element on top of XML data via XSLT

Question:

I am new to XML and XSLT so I need little bit of help. I have this kind of XML:

<Catalogue> <App id="1"> <Car>BMW</Car> <BodyType>Sedan</BodyType> <PartType>Wiper</PartType> <PartNumber>1234</PartNumber> </App> <App id="2"> <Car>Dodge</Car> <DriveType>FWD</DriveType> <PartType>Wiper</PartType> <PartNumber>5678</PartNumber> </App> <App id="3"> <Car>Chevrolet</Car> <EngineVIN>G</EngineVIN> <PartType>Wiper</PartType> <PartNumber>9012</PartNumber> </App> <App id="4"> <Car>VW</Car> <PartType>Wiper</PartType> <PartNumber>3456</PartNumber> </App> </Catalogue>

I can't figure out how to write a XSLT to add top App with id=0 so it would look like this:

<Catalogue> <App id="0"> <Car></Car> <BodyType></BodyType> <DriveType></DriveType> <EngineVIN></EngineVIN> <PartType></PartType> <PartNumber></PartNumber> </App> <App id="1"> <Car>BMW</Car> <BodyType>Sedan</BodyType> <PartType>Wiper</PartType> <PartNumber>1234</PartNumber> </App> <App id="2"> <Car>Dodge</Car> <DriveType>FWD</DriveType> <PartType>Wiper</PartType> <PartNumber>5678</PartNumber> </App> <App id="3"> <Car>Chevrolet</Car> <EngineVIN>G</EngineVIN> <PartType>Wiper</PartType> <PartNumber>9012</PartNumber> </App> <App id="4"> <Car>VW</Car> <PartType>Wiper</PartType> <PartNumber>3456</PartNumber> </App> </Catalogue>

I am trying for couple of days now with no luck, so any help would be appreciated. I need this because when I want to import this XML into Access, for some reason if in first "App" all fields aren't defined (which occur in other "App"-s), they don't get imported.

Thank you!

Answer1:

If you are satisfied with a fixed set of tags it can be done in XSLT 1.0 quite easily as follows:

<xsl:template match="/Catalogue"> <Catalogue> <App id="0"> <Car></Car> <BodyType></BodyType> <DriveType></DriveType> <EngineVIN></EngineVIN> <PartType></PartType> <PartNumber></PartNumber> </App> <xsl:copy-of select="App"/> </Catalogue> </xsl:template>

Note that I added closing tags for <App>. These were missing in your XML file.

Recommend

  • javascript using select box to fill other select box
  • Correct usage of addtoStage when loading external swf
  • c# Eject USB - code for LockVolume, DismountVolume, and PrepareRemovalOfVolume
  • Adding horizontal lines to a ggplot-barchart with the value of selected factor-levels
  • Listbox in Ms access
  • can local or transient data be represented by URLs?
  • Heroku Issue When Editing Blog (Random Routing to Matchheight.js)
  • Dictionary Ordering
  • Bars in geom_bar are not of equal width
  • Checking boxes and unchecking boxes with certain events
  • how to unpack stxetx data in python
  • Question about List scope in C#
  • How to transform y-axis for barplot (ggplot2)
  • Parse returned C# list in AJAX success function
  • How to properly add strings to QListWidgets?
  • richtextbox to string
  • How to align an image side by side with a heading element?
  • Oracle ListaGG, Top 3 most frequent values, given in one column, grouped by ID
  • php show all images in directory and sort by last modified
  • Copying rows in a database when rows have children
  • Receive list of elements in their visual order
  • Can't compile Arduino CapSense example
  • Want to understand iframe breakout code
  • Selectively hide background elements when overlayed with transparent div
  • Enumerating Controls on a Form
  • PayPal API Listener Website Payments Standard URI
  • chrome video src change not working
  • Why isn't obj.style.left = “200px”; working in this code?
  • Installed module is empty
  • how to display data from 1st point on words on y axis for line chart in d3.js
  • Change multiple background-images with jQuery
  • Algorithm for a smudge tool?
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Can Jackson SerializationFeature be overridden per field or class?
  • How to add date and time under each post in guestbook in google app engine
  • Android Studio and gradle
  • coudnt use logback because of log4j
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • UserPrincipal.Current returns apppool on IIS