XML 将值替换为 XSL 的公式,并从 PHP 创建 CSV


xml replace value with formula by xsl and create csv from php

我有一个来自供应商的XML提要..

<?xml version="1.0" encoding="windows-1250"?>
<maps>
  <map>
    <sku>1534133010</sku>
    <name>10 LED dekor. řetěz - sníh 2M IP44 modrá</name>
    <price>165,0000</price>
    <qty>0</qty>
  </map>
</maps>

我需要根据公式进行价格转换:

=IIf([map]![price]<="100";[map]![price]*1,43;IIf([map]![price]<="300";[map]![price]*1,4;IIf([map]![price]<="500";[map]![price]*1,38;IIf([map]![price]<="700";[map]![price]*1,35;IIf([map]![price]>"1200";[map]![price]*1,34;0)))))

和价格四舍五入为整数。

并完成转换为CSV。

CSV的愿望输出:

sku,name,price,qty
1534133010,10 LED dekor. řetěz - sníh 2M IP44 modrá,220,0

我尝试跟随但没有工作..

PHP脚本:

<?php
$xml = new DOMDocument;
$xml->load('sample.xml');
$xsl = new DOMDocument;
$xsl->load('transformace.xsl');
$proc = new XSLTProcessor();
$proc->importStylesheet($xsl);
$newDOM = $proc->transformToDoc($xml);
$newDOM->formatOutput = true;
$newDOM->save("newfile.xml")

XSLT

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="windows-1250" />   
    <xsl:template match="/">
      <maps>
        <xsl:for-each select="SHOP/SHOPITEM">
        <map>
          <sku><xsl:value-of select="CODE"/></sku>
          <name><xsl:value-of select="PRODUCT"/></name>
          <price><xsl:value-of select="format-number( round(100*'price) div 100 ,"##0.00" )"/></price>
          <qty><xsl:value-of select="AVAILABILITY"/></qty>      
        </map>
      </xsl:for-each>
    </maps>
  </xsl:template>    
</xsl:stylesheet>

有人知道怎么做吗?

要根据您的公式转换价格,首先需要通过将十进制逗号替换为小数点将价格转换为数字。

然后使用xsl:choose为每个价格水平应用适当的乘数。

下面是一个使用最小化测试输入的示例:

.XML

<maps>
  <map>
    <sku>12345</sku>
    <price>100,0000</price>
  </map>
  <map>
    <sku>12346</sku>
    <price>300,0000</price>
  </map>
  <map>
    <sku>12347</sku>
    <price>500,0000</price>
  </map>
  <map>
    <sku>12348</sku>
    <price>700,0000</price>
  </map>
  <map>
    <sku>12349</sku>
    <price>800,0000</price>
  </map>
</maps>

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/maps">
    <maps>
        <xsl:for-each select="map">
            <map>
                <sku><xsl:value-of select="sku"/></sku>
                <price>
                    <xsl:variable name="price" select="translate(price, ',', '.')" />
                    <xsl:choose>
                        <xsl:when test="$price &lt;= 100">
                            <xsl:value-of select="round($price * 1.43)"/>
                        </xsl:when>
                        <xsl:when test="$price &lt;= 300">
                            <xsl:value-of select="round($price * 1.4)"/>
                        </xsl:when>
                        <xsl:when test="$price &lt;= 500">
                            <xsl:value-of select="round($price * 1.38)"/>
                        </xsl:when>
                        <xsl:when test="$price &lt;= 700">
                            <xsl:value-of select="round($price * 1.35)"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="round($price * 1.34)"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </price>
            </map>
        </xsl:for-each>
    </maps>
</xsl:template>
</xsl:stylesheet>

结果

<?xml version="1.0" encoding="UTF-8"?>
<maps>
   <map>
      <sku>12345</sku>
      <price>143</price>
   </map>
   <map>
      <sku>12346</sku>
      <price>420</price>
   </map>
   <map>
      <sku>12347</sku>
      <price>690</price>
   </map>
   <map>
      <sku>12348</sku>
      <price>945</price>
   </map>
   <map>
      <sku>12349</sku>
      <price>1072</price>
   </map>
</maps>

  1. XSLT 处理输入中未出现的元素,例如商店、商店项目代码和产品。要么你的输入非常不同从您向我们展示的内容来看,或者您的 XSLT 与输入无关;

  2. 您的公式在<=700>1200之间有差距;

  3. 我不禁想知道为什么价格需要调整道路;通常,您会看到价格按数量调整,而不是按本身。

我建议您询问有关CSV输出的单独问题,因为它与上述问题无关。