使用 XSL 转换 xml
我需要帮助使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先是以下内容:
输出:
Here's something to start with:
Output:
这是最短的可能转换之一,也是最简单且完全“本着 XSLT”的转换之一:
当应用于提供的 XML 文档时:
想要的,就会产生正确的结果:
请注意:
重写“身份模板”——最基本、最强大的 XSLT 设计模式。< /p>
模式匹配,绝对没有条件 XSLT 指令。
This is among the shortest possible transformations that is also one of the simplest and completely "in the spirit of XSLT":
when applied on the provided XML document:
the wanted, correct result is produced:
Do note:
Overriding of the "identity template" -- the most fundamental and powerful XSLT design pattern.
Pattern matching and absolutely no conditional XSLT instructions.
下面是一个比任何人的 IMO =p 更简单的 XSL,非常易读,非常简单:
它很简单,因为它基本上是 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:
It's simple, because it's basically HTML. Only attributes are different like this, so you need
xsl:element[name]
andxsl:attribute[name]
.edit
See XML, XSL and PHP source: http://hotblocks.nl/tests/xsl(t).php?source