使用 XSL 转换 xml

发布于 2024-11-05 17:17:58 字数 2111 浏览 2 评论 0原文

我需要帮助使用 xsl 从shops.xml 文件(其中 incity="yes" 和 type="Botique" )获取以下输出。由于我是 xslt 的新手,所以任何帮助将不胜感激。

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

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

i need help in getting the below output from shops.xml file( where incity="yes" and type="Botique" ) by using xsl . As i am new to xslt , so any help would be highly 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);
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光病人 2024-11-12 17:17:58

首先是以下内容:

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

输出:

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

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>
蓬勃野心 2024-11-12 17:17:58

这是最短的可能转换之一,也是最简单且完全“本着 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>

请注意

  1. 重写“身份模板”——最基本、最强大的 XSLT 设计模式。< /p>

  2. 模式匹配,绝对没有条件 XSLT 指令。

This is among the shortest possible transformations that is also one of the simplest and completely "in the spirit of 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:

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

  2. Pattern matching and absolutely no conditional XSLT instructions.

半﹌身腐败 2024-11-12 17:17:58

下面是一个比任何人的 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>

它很简单,因为它基本上是 HTML。只有属性不同,所以您需要 xsl:element[name]xsl:attribute[name]

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

A much simpler XSL than anyone's IMO =p is the following, 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's simple, because it's basically HTML. Only attributes are different like this, 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文