【问题标题】:transforming XML using data from another XML file使用来自另一个 XML 文件的数据转换 XML
【发布时间】:2014-10-14 22:24:58
【问题描述】:

我有以下 XML,以及一个包含 ID 类别的产品的文件。 xml 的最后一行包含带有名称的 ID 类别和带有名称的父 ID 类别。

我需要更改名称的产品 ID 类别并排列类别和父类别。 有输入 XML:

<Export>  
<Products>   
<produkt>
<Nazev>forma 21cm skl.</Nazev>  
<Code>200011</Code>
<Kategorie>3</Kategorie>  
</produkt>   
</Products>   
</Export>   

<Categories>
<kategorie><id>1</id><ParentId></ParentId><Nazev>category1</Nazev></kategorie>   
<kategorie><id>2</id><ParentId>1</ParentId><Nazev>category2</Nazev></kategorie>   
<kategorie><id>3</id><ParentId>2</ParentId><Nazev>category3</Nazev></kategorie>
</Categories>  
</Export>  

这里是输出 XML:

<Export> 
<Products>  
<produkt> 
<Nazev>forma 21cm skl.</Nazev>
<Code>200011</Code>
<Kategorie>category2 > category2 > category3</Kategorie>
</produkt>
</Products>  
</Export> 

但我不知道如何使用 XSL 或 PHP 脚本来制作它。

【问题讨论】:

    标签: php xml xslt transform


    【解决方案1】:

    这是一个棘手的问题。试试这个方法:

    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:strip-space elements="*"/>
    
    <xsl:key name="kategorie" match="kategorie" use="id" />
    
    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="Kategorie">
        <xsl:copy>
            <xsl:apply-templates select="key('kategorie', .)"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="kategorie">
        <xsl:if test="key('kategorie', ParentId)">
            <xsl:apply-templates select="key('kategorie', ParentId)" />
            <xsl:text disable-output-escaping="yes"> > </xsl:text>
        </xsl:if>
        <xsl:value-of select="Nazev" />
    </xsl:template>
    
    <xsl:template match="Categories"/>
    
    </xsl:stylesheet>
    

    应用于格式良好的(!) 输入

    <Export>
      <Products>
        <produkt>
          <Nazev>forma 21cm skl.</Nazev>
          <Code>200011</Code>
          <Kategorie>3</Kategorie>
        </produkt>
      </Products>
      <Categories>
        <kategorie>
          <id>1</id>
          <ParentId/>
          <Nazev>category1</Nazev>
        </kategorie>
        <kategorie>
          <id>2</id>
          <ParentId>1</ParentId>
          <Nazev>category2</Nazev>
        </kategorie>
        <kategorie>
          <id>3</id>
          <ParentId>2</ParentId>
          <Nazev>category3</Nazev>
        </kategorie>
      </Categories>
    </Export>
    

    结果将是:

    <?xml version="1.0" encoding="UTF-8"?>
    <Export>
       <Products>
          <produkt>
             <Nazev>forma 21cm skl.</Nazev>
             <Code>200011</Code>
             <Kategorie>category1 > category2 > category3</Kategorie>
          </produkt>
       </Products>
    </Export>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-22
      • 2021-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多