【问题标题】:transforming xml file转换xml文件
【发布时间】:2011-05-09 01:43:43
【问题描述】:

如何使用 XSLT 样式表。结果将是具有以下结构的 XML 文件 (item.xml) 其中 ocshelf="yes" 和 type="cosmetic"

products.xml------------------------------------

<products>
<product onshelf=“yes” condition=“ok”>
<type>cosmetic</type>
<price>680</price>
<address>
<streetNo>1</streetNo>
<street>Jefer Street</street>
<suburb>Melbourne</suburb>
</address>
</product>
<product onshelf=“yes” condition=“ok”>
<type>noncosmetic</type>
<price>600</price>
<address>
<streetNo>2</streetNo>
<street>Colo Street</street>
<suburb>Melbourne</suburb>
</address>
</product>
</products>

item.xml--------------------------------

<products>
<product condition=“ok”>
<type>cosmetic</type>
<price>680</price>
<address>1 Jefer Street , Melbourne </address>
</product>
</products>

【问题讨论】:

  • 到目前为止你有什么,它怎么不工作?
  • @ignacio 实际上我不知道如何使用 xslt n php 将 products.xml 转换为 item.xml
  • 这里的回答者基本上免费提供他们的服务,如果没有为解决问题付出任何努力,甚至没有阅读教程的程度,大多数人都不愿意提供帮助。
  • 您是否在编写 XSLT 代码或从 PHP 运行它时遇到问题?或两者?为了帮助您,我们需要知道您遇到的问题。

标签: php xml xslt


【解决方案1】:

你可以试试这样的:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema" 
                exclude-result-prefixes="xs" 
                version="2.0">
    <xsl:template match="/">
        <products>
            <xsl:apply-templates 
                select="/products/product[@onshelf='yes' and type='cosmetic']"/>
        </products>
    </xsl:template>
    <xsl:template match="product">
        <product condition="{@condition}">
            <xsl:apply-templates />
        </product>
    </xsl:template>
    <xsl:template match="address">
        <address>
            <xsl:value-of select="concat(streetNo, ' ', street, ', ', suburb)" />
        </address>
    </xsl:template>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

如果您是 XSLT 新手,我建议您查看此线程以获得教程建议:https://stackoverflow.com/questions/1858345/xsltwhich-is-the-best-tutorial-you-would-like-to-recommend

【讨论】:

    猜你喜欢
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多