v.hozii
v.hozii

Reputation: 1

XML with XSLT multiple tags

I would like to interpret a file.xml to file.txt, this is not a problem but when I try to "take" many things in different tags.

Please take a look at my file.xml :

<racine>
        <balise1>
                 <info>
                      <Commercial>1000</Commercial>
                      <OrdId>42</OrdId>
                      <CustomerId>314159</CustomerId>
                 </info>
        </balise1>
        <balise2>
                 <info2>
                       <Quantity>1</Quantity>
                       <Price>10.0</Price>
                       <Currency>CHF</Currency>
                 </info2>
        </balise2>
</racine>

I want this output;

Commercial,Order,CustomerId,Quantity,Price,Currency
1000,42,314159,1,10.0,CHF

I tried many solutions, but I'm a noob in XSLT.

Here is an example of my xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSl/Transform">
<xsl:output method="text" />

<xsl:template match="/">
<xsl:text>Commercial,OrdId,CustomerId,Quantity,Price,Currency</xsl:text>
<xsl:text>&#xa;</xsl:text>
<xsl:apply-templates select="racine"/>     
</xsl:template>

<xsl:template match="racine">
<xsl:apply-templates select="balise1/info"/>
</xsl:template>

<xsl:template match="info">
  <xsl:value-of select="Commercial"/>
  <xsl:text>,</xsl:text>
  <xsl:value-of select="OrdId"/>
  <xsl:text>,</xsl:text>
  <xsl:value-of select="CustomerId"/>
  <xsl:text>&#xa;</xsl:text>
</xsl:template>

<xsl:template match="info">
  <xsl:apply-templates select="balise2/info2"/>
</xsl:template>

<xsl:template match="info2">
<xsl:value-of select="ItemQuantity"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="PriceValue"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="CurrencyCode"/>
<xsl:text>&#xa;</xsl:text>
</xsl:template>

</xsl:stylesheet>

Upvotes: 0

Views: 608

Answers (3)

Ajeet Singh
Ajeet Singh

Reputation: 1076

Check this code

<xsl:template match="/">
<xsl:text>Commercial,OrdId,CustomerId,Quantity,Price,Currency</xsl:text>
<xsl:text>&#xa;</xsl:text>
<xsl:apply-templates select="racine"/>     
</xsl:template>

<xsl:template match="info">
<xsl:value-of select="Commercial"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="OrdId"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="CustomerId"/>
</xsl:template>

<xsl:template match="info2">
<xsl:value-of select="Quantity"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="Price"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="Currency"/>
<xsl:text>&#xa;</xsl:text>
</xsl:template>

Upvotes: -1

Rupesh_Kr
Rupesh_Kr

Reputation: 3435

if your input has fixed format than you can handle this easily in XSLT 2.0

<xsl:template match="/">
    <xsl:text>Commercial,OrdId,CustomerId,Quantity,Price,Currency</xsl:text>
    <xsl:text>&#xa;</xsl:text>
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="racine">
    <xsl:value-of select="balise1/info/*|balise2/info2/*" separator=","/>
    <xsl:text>&#xa;</xsl:text>
</xsl:template>

Upvotes: 1

Tim C
Tim C

Reputation: 70628

You have two templates matching "info" which is considered an error in XSLT. From looking at what you require, the body of the second template should probably be called from the template matching racine

<xsl:template match="racine">
  <xsl:apply-templates select="balise1/info"/>
  <xsl:apply-templates select="balise2/info2"/>
</xsl:template>

Other things to note

  1. Namespaces are case-sensitive. You had specified the xsl namespace as http://www.w3.org/1999/XSl/Transform when it should be http://www.w3.org/1999/XSL/Transform
  2. Your info2 template was referring to elements which did not exist in the XML.

Try this XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />

<xsl:template match="/">
  <xsl:text>Commercial,OrdId,CustomerId,Quantity,Price,Currency</xsl:text>
  <xsl:text>&#xa;</xsl:text>
  <xsl:apply-templates select="racine"/>     
</xsl:template>

<xsl:template match="racine">
  <xsl:apply-templates select="balise1/info"/>
  <xsl:apply-templates select="balise2/info2"/>
  <xsl:text>&#xa;</xsl:text>
</xsl:template>

<xsl:template match="info">
  <xsl:value-of select="Commercial"/>
  <xsl:text>,</xsl:text>
  <xsl:value-of select="OrdId"/>
  <xsl:text>,</xsl:text>
  <xsl:value-of select="CustomerId"/>
</xsl:template>

<xsl:template match="info2">
  <xsl:text>,</xsl:text>
  <xsl:value-of select="Quantity"/>
  <xsl:text>,</xsl:text>
  <xsl:value-of select="Price"/>
  <xsl:text>,</xsl:text>
  <xsl:value-of select="Currency"/>
</xsl:template>

</xsl:stylesheet>

Upvotes: 2

Related Questions