简体   繁体   English

使用XSL转换xml

[英]transforming xml by using XSL

i need help in getting the below output from shops.xml file( where incity="yes" and type="Botique" ) by using xsl . 我需要帮助从shops.xml文件(其中incity =“是”和type =“Botique”)获取以下输出,使用xsl。 As i am new to xslt , so any help would be highly appreciated. 因为我是xslt的新手,所以任何帮助都将受到高度赞赏。

shops.xml: 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: 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 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);
?>

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>

Output: 输出:

<?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>

This link should help you getting the idea. 这个链接可以帮助你理解。 But without a tool, creating complex XLST can be a nightmare. 但是如果没有工具,创建复杂的XLST可能是一场噩梦。

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

This is among the shortest possible transformations that is also one of the simplest and completely "in the spirit of XSLT ": 这是最短的转换之一,也是最简单且完全“在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>

when applied on the provided XML document : 当应用于提供的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>

the wanted, correct result is produced : 产生了想要的正确结果

<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>

Do note : 请注意

  1. Overriding of the "identity template" -- the most fundamental and powerful XSLT design pattern. 覆盖“身份模板” - 最基本和最强大的XSLT设计模式。

  2. Pattern matching and absolutely no conditional XSLT instructions. 模式匹配,绝对没有条件XSLT指令。

A much simpler XSL than anyone's IMO =p is the following, very readable, very simple: 一个比任何人的IMO = p简单得多的XSL是以下,非常易读,非常简单:

<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's simple, because it's basically HTML. 它很简单,因为它基本上是HTML。 Only attributes are different like this, so you need xsl:element[name] and xsl:attribute[name] . 只有属性不同,所以你需要xsl:element[name]xsl:attribute[name]

edit 编辑
See XML, XSL and PHP source: http://hotblocks.nl/tests/xsl(t).php?source 请参阅XML,XSL和PHP源代码: http//hotblocks.nl/tests/xsl(t) .php?source

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM