我需要通过使用xsl从shops.xml文件(incity=“yes”和type=“botique”)获得以下输出的帮助。由于我是xslt新手,因此非常感谢您的帮助。
Sux.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>
Sux.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>
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);
?>
最佳答案
以下是一些开始:
<?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>