Developer forum

Forum » Templates » Improove DataList with XML publication

Improove DataList with XML publication

George Nelzo Pereira
Reply
Hi...
 
I need to have one export like this
 
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"  encoding="utf-8" />
  <xsl:template match="/">
 <catalog>
      <xsl:for-each select="View/Rows/Row">
      <product>
         <sku>PROD1</sku>
         <name>XPTO</name>
         <product_type>1</product_type>
         <url><![CDATA[http://www.lojaexemplo.com.br/mesa-de-centro-de-vidro]]></url>
         <status>1</status>
         <availability>1</availability>
         <manufacturer><![CDATA[Marca ou Fabricante]]></manufacturer>
         <color><![CDATA[Marrom, Branco, Preto]]></color>
         <model><![CDATA[ABC-123]]></model>
         <material><![CDATA[Madeira, Alumínio, Vidro]]></material>
         <height>40.5</height>
         <width>50</width>
         <length>41.8</length>
         <weight>12.6</weight>
         <price>815.05</price>
      </product>
   <product>...</product>
      </xsl:for-each>
   </catalog>
  </xsl:template>
</xsl:stylesheet>
 
Im using one datalist and the DataManagement/XSLT/Publishing
I create this XSLT. It's working but I don't think thats really good.
Someone have any idea how to improve without this IF's ?
The query returns with the alias for the fields and in the same order as the XML
 
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"  encoding="utf-8" />
 
  <xsl:template match="/">
    <catalog>
      <xsl:for-each select="View/Rows/Row">
        <product>
          <xsl:for-each select="Value">
           
            <xsl:if test="position() = 1">
              <xsl:element name="sku"><xsl:value-of select="current()"/></xsl:element>
            </xsl:if>
           
            <xsl:if test="position() = 2">
              <xsl:element name="name"><xsl:value-of select="current()"/></xsl:element>
            </xsl:if>
           
            <xsl:if test="position() = 3">
              <xsl:element name="product_type"><xsl:value-of select="current()"/></xsl:element>
            </xsl:if>
           
            <xsl:if test="position() = 4">
              <xsl:element name="url"><xsl:value-of select="current()"/></xsl:element>
            </xsl:if>
           
            <xsl:if test="position() = 5">
              <xsl:element name="status"><xsl:value-of select="current()"/></xsl:element>
            </xsl:if>
           
            <xsl:if test="position() = 6">
              <xsl:element name="availability"><xsl:value-of select="current()"/></xsl:element>
            </xsl:if>
           
            <xsl:if test="position() = 7">
              <xsl:element name="manufacturer"><xsl:value-of select="current()"/></xsl:element>
            </xsl:if>
           
            <xsl:if test="position() = 8">
              <xsl:element name="color"><xsl:value-of select="current()"/></xsl:element>
            </xsl:if>
           
            <xsl:if test="position() = 9">
              <xsl:element name="model"><xsl:value-of select="current()"/></xsl:element>
            </xsl:if>
           
            <xsl:if test="position() = 10">
              <xsl:element name="material"><xsl:value-of select="current()"/></xsl:element>
            </xsl:if>
           
            <xsl:if test="position() = 11">
              <xsl:element name="height"><xsl:value-of select="current()"/></xsl:element>
            </xsl:if>
          
            <xsl:if test="position() = 12">
              <xsl:element name="width"><xsl:value-of select="current()"/></xsl:element>
            </xsl:if>
           
            <xsl:if test="position() = 13">
              <xsl:element name="length"><xsl:value-of select="current()"/></xsl:element>
            </xsl:if>
           
            <xsl:if test="position() = 14">
              <xsl:element name="weight"><xsl:value-of select="current()"/></xsl:element>
            </xsl:if>
           
            <xsl:if test="position() = 15">
              <xsl:element name="price"><xsl:value-of select="current()"/></xsl:element>
            </xsl:if>
           
          </xsl:for-each>
        </product> 
      </xsl:for-each>
    </catalog>
  </xsl:template>
</xsl:stylesheet>

Replies

 
Konstantin
Reply
This post has been marked as an answer

Hi George! 
You can do it like this:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">   
  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"  encoding="utf-8" />  

  <xsl:template match="/">   
    <catalog>       
      <xsl:for-each select="View/Rows/Row">         
        <product>
          <sku><xsl:value-of select="Value[1]"/></sku>
          <name><xsl:value-of select="Value[2]"/></name>     
          <product_type><xsl:value-of select="Value[3]"/></product_type>     
          <url><xsl:value-of select="Value[4]"/></url>     
          <status><xsl:value-of select="Value[5]"/></status>     
        </product>
      </xsl:for-each>   
    </catalog>   
  </xsl:template> 
</xsl:stylesheet>


Best regards
Konstantin Landyshev
Frontend developer

Votes for this answer: 1
 
George Nelzo Pereira
Reply

Hi Konstantin, thanks for your help.

 

You must be logged in to post in the forum