Xml conversion using XSL

I need help getting the result below from the shops.xml file (where incity = "yes" and type = "Botique") using xsl. Since I am new to xslt, so any help would be greatly appreciated.

shops.xml:

<shops>     
<shop incity="yes" onlineorder="yes">         
<type>Botique</type>         
<address>  
<streetno>23</streetno>
<streetname>collins</streetname>
<suburb>Melbourne</suburb>
</address>     
</shop> 
<shop incity="yes" onlineorder="yes">         
<type>Botique</type>         
<address>  
<streetno>25</streetno>
<streetname>little collins</streetname>
<suburb>Melbourne</suburb>
</address>     
</shop> 
<shop incity="no" onlineorder="yes">         
<type>Tailoring</type>         
<address>  
<streetno>2</streetno>
<streetname>cosmos street</streetname>
<suburb>Glenroy</suburb>
</address>     
</shop>  
</shops>

output:

<shops>     
<shop  onlineorder="yes">         
<type>Botique</type>         
<address>  23 collins,Melbourne </address>     
</shop> 
<shop onlineorder="yes">         
<type>Botique</type>         
<address> 25 little collins, Melbourne </address>     
</shop> 
</shops>

shop.xsl:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">   
<xsl:template match="shop[@incity='no']" />    
<xsl:template match="@* | node()">     
<xsl:copy>    
<xsl:apply-templates select="@* | node()"/>     
</xsl:copy>   
</xsl:template> 
</xsl:stylesheet>

shop.php

<?php
$xmlDoc = new DOMDocument('1.0');
$xmlDoc->formatOutput = true;
$xmlDoc->load("shops.xml");
$xslDoc = new DomDocument;
$xslDoc->load("shop.xsl");
$proc = new XSLTProcessor;
$proc->importStyleSheet($xslDoc);
$strxml= $proc->transformToXML($xmlDoc);
echo ($strxml);
?>
+3
source share
4 answers

Here's something to start with:

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

  <xsl:template match="shops">
    <xsl:element name="shops">
      <xsl:for-each select="shop">
        <xsl:if test="@incity='yes'">
          <xsl:if test="type='Botique'">
            <xsl:element name="shop">
              <xsl:attribute name="onlineorder">
                <xsl:value-of select="@onlineorder"/>
              </xsl:attribute>
              <xsl:element name="type">
                <xsl:value-of select="type"/>
              </xsl:element>
              <xsl:element name="address">
                <xsl:value-of select="address/streetno"/>
                <xsl:text> </xsl:text>
                <xsl:value-of select="address/streetname"/>
                <xsl:text>, </xsl:text>
                <xsl:value-of select="address/suburb"/>
              </xsl:element>
            </xsl:element>
          </xsl:if>
        </xsl:if>
      </xsl:for-each>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

Conclusion:

<?xml version="1.0"?>
<shops>
  <shop onlineorder="yes">
    <type>Botique</type>
    <address>23 collins, Melbourne</address>
  </shop>
  <shop onlineorder="yes">
    <type>Botique</type>
    <address>25 little collins, Melbourne</address>
  </shop>
</shops>
+1
source

This link should help you get this idea. But without a tool, creating a complex XLST can be a nightmare.

http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog

0
source

, " XSLT":

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="address">
  <address>
   <xsl:value-of select=
    "concat(streetno, ' ', streetname, ', ', suburb)"/>
  </address>
 </xsl:template>

 <xsl:template match=
  "@incity | shop[not(@incity='yes' and type='Botique')]"/>
</xsl:stylesheet>

XML-:

<shops>
    <shop incity="yes" onlineorder="yes">
        <type>Botique</type>
        <address>
            <streetno>23</streetno>
            <streetname>collins</streetname>
            <suburb>Melbourne</suburb>
        </address>
    </shop>
    <shop incity="yes" onlineorder="yes">
        <type>Botique</type>
        <address>
            <streetno>25</streetno>
            <streetname>little collins</streetname>
            <suburb>Melbourne</suburb>
        </address>
    </shop>
    <shop incity="no" onlineorder="yes">
        <type>Tailoring</type>
        <address>
            <streetno>2</streetno>
            <streetname>cosmos street</streetname>
            <suburb>Glenroy</suburb>
        </address>
    </shop>
</shops>

, :

<shops>
   <shop onlineorder="yes">
      <type>Botique</type>
      <address>23 collins, Melbourne</address>
   </shop>
   <shop onlineorder="yes">
      <type>Botique</type>
      <address>25 little collins, Melbourne</address>
   </shop>
</shops>

:

  • Overriding the identity template is the most fundamental and powerful XSLT design template.

  • Comparison of templates and absolutely no conditional XSLT instructions.

0
source

XSL is much simpler than any IMO = p, it is very readable, very simple:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="/">
  <shops>
   <xsl:for-each select="shops/shop[@incity!='no']">
    <xsl:element name="shop">
     <xsl:attribute name="onlineorder"><xsl:value-of select="@onlineorder" /></xsl:attribute>
     <type><xsl:value-of select="type" /></type>
     <address>
      <xsl:value-of select="address/streetno" />
      <xsl:text> </xsl:text>
      <xsl:value-of select="address/streetname" />
      <xsl:text>, </xsl:text>
      <xsl:value-of select="address/suburb" />
     </address>
    </xsl:element>
   </xsl:for-each>
  </shops>
 </xsl:template>
</xsl:stylesheet>

It is simple because it is mostly HTML. Only attributes are different from each other, so you need xsl:element[name]and xsl:attribute[name].

edit
See XML, XSL and PHP source: http://hotblocks.nl/tests/xsl(t).php?source

0
source

All Articles