0

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!

3
  • Is the set of the tags under <App id="0"> fixed or is this supposed to be a dynamic union of all tags used under the other <App> tags? Commented Oct 13, 2014 at 11:19
  • Can you use XSLT 2.0? Commented Oct 13, 2014 at 11:19
  • Well, it is dynamic depending of a XML i receive, but my idea is to add them fixed. Because I am a beginner with XSLT I was thinking to add all tags possible regardless of the XML I receive. I have no idea how I would do it dynamically. Also I don't know if I can use XSLT 2.0, I don't know does Access 2007 work with XSLT 2.0. Commented Oct 13, 2014 at 11:24

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@Marcus Rickert: Can I kindly ask you for little bit more help? Answer you posted here works, but I need to integrate it into another problem which is on this link. Anyway I need to add this <App id="0"> to a more complex XML than I posted here.
@michael.hor257k: Can you please take a look at my other question on this link. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.