简体   繁体   中英

XML TO XML using XSL

I have trouble trying to convert this xml format

<?xml version="1.0" standalone="yes"?>
<_InventoryProgram_0_1DataSet xmlns="http://tempuri.org/_InventoryProgram_0_1DataSet.xsd">
    <ΠΡΟΙΟΝΤΑ>
    <unique_id>test</unique_id>
    <title>test</title>
    <description>test</description>
    <category_id>test</category_id>
    <price>test</price>
    <make>test</make>
    <model>test</model>
    <yearfrom>test</yearfrom>
    <yearto>test</yearto>
  </ΠΡΟΙΟΝΤΑ>
  <ΠΡΟΙΟΝΤΑ>
    <unique_id>test</unique_id>
    <title>test</title>
    <description>test</description>
    <category_id>test</category_id>
    <price>test</price>
    <make>test</make>
    <model>test</model>
    <yearfrom>test</yearfrom>
    <yearto>test</yearto>
  </ΠΡΟΙΟΝΤΑ>
  <ΠΡΟΙΟΝΤΑ>
    <unique_id>test</unique_id>
    <title>test</title>
    <description>test</description>
    <category_id>test</category_id>
    <price>test</price>
    <make>test</make>
    <model>test</model>
    <yearfrom>test</yearfrom>
    <yearto>test</yearto>
    <photo>test</photo>
    <condition>test</condition>
    <update_interval>test</update_interval>
  </ΠΡΟΙΟΝΤΑ>
</_InventoryProgram_0_1DataSet>

Into this xml format

<?xml version="1.0" encoding="UTF-8"?>
<cardealer>
    <lastupdate>2017-12-05T22:03:05Z</lastupdate>
    <classifieds>
        <classified>
            <unique_id>test</unique_id>
            <title><![CDATA[test]]></title>
            <description><![test]]></description>
            <category_id>test</category_id>
            <price>test</price>
            <makemodels>
                <makemodel>
                    <make>test</make>
                    <model>test</model>
                    <yearfrom>test</yearfrom>
                    <yearto>test</yearto>
                </makemodel>
            </makemodels>
            <photos>
                <photo>
                    test
                </photo>
            </photos>
            <condition>test</condition>
            <update_interval>test</update_interval>
        </classified>
    </classifieds>
</cardealer>

I have tried this with disasterus results... It just erases everything and gives me the raw data!

<?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="UTF-8" indent="yes"/>
    <xsl:template match="/_InventoryProgram_0_1DataSet">
        <cardealer>
            <classifieds>
                <xsl:for-each select="ΠΡΟΙΟΝΤΑ">
                    <classified>
                        <xsl:for-each select="unique_id">
                            <unique_id>
                                <xsl:value-of select="."/>
                            </unique_id>
                        </xsl:for-each>
                        <xsl:for-each select="title">
                            <title>
                                <xsl:value-of select="."/>
                            </title>
                        </xsl:for-each>
                        <xsl:for-each select="description">
                            <description>
                                <xsl:value-of select="."/>
                            </description>
                        </xsl:for-each>
                        <xsl:for-each select="category_id">
                            <category_id>
                                <xsl:value-of select="."/>
                            </category_id>
                        </xsl:for-each>
                        <xsl:for-each select="price">
                            <price>
                                <xsl:value-of select="."/>
                            </price>
                        </xsl:for-each>
                        <makemodels>
                            <makemodel>
                                <xsl:for-each select="make">
                                    <make>
                                        <xsl:value-of select="."/>
                                    </make>
                                </xsl:for-each>
                                <xsl:for-each select="model">
                                    <model>
                                        <xsl:value-of select="."/>
                                    </model>
                                </xsl:for-each>
                                <xsl:for-each select="yearfrom">
                                    <yearfrom>
                                        <xsl:value-of select="."/>
                                    </yearfrom>
                                </xsl:for-each>
                                <xsl:for-each select="yearto">
                                    <yearto>
                                        <xsl:value-of select="."/>
                                    </yearto>
                                </xsl:for-each>
                            </makemodel>
                        </makemodels>
                    </classified>
                </xsl:for-each>
            </classifieds>
        </cardealer>
    </xsl:template>
</xsl:stylesheet>

These xmls are just an example of the export(first) of a long output from a product data-table , and the second only an example of one product, but i wish this format could be applied to all the products of the first , no matter how many.

Is the XSL the correct way? Is this even possible or there is a better one? This is in a C# program Btw.

Your input has a namespace and you didn't reference it... some of your xpaths were also wrong.

Stylesheet

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:ds="http://tempuri.org/_InventoryProgram_0_1DataSet.xsd"
                exclude-result-prefixes="ds xsl"
                >
  <xsl:output method="xml"
              encoding="UTF-8"
              indent="yes"/>
  <xsl:template match="/">
    <cardealer>
      <classifieds>
        <xsl:apply-templates select="//ds:ΠΡΟΙΟΝΤΑ" />
      </classifieds>
    </cardealer>
  </xsl:template>

  <xsl:template match="ds:ΠΡΟΙΟΝΤΑ">
    <classified>
      <unique_id>
        <xsl:value-of select="ds:unique_id"/>
      </unique_id>
      <title>
        <xsl:value-of select="ds:title"/>
      </title>
      <description>
        <xsl:value-of select="ds:description"/>
      </description>
      <category_id>
        <xsl:value-of select="ds:category_id"/>
      </category_id>
      <price>
        <xsl:value-of select="ds:price"/>
      </price>
      <makemodels>
        <makemodel>
          <make>
            <xsl:value-of select="ds:make"/>
          </make>
          <model>
            <xsl:value-of select="ds:model"/>
          </model>
          <yearfrom>
            <xsl:value-of select="ds:yearfrom"/>
          </yearfrom>
          <yearto>
            <xsl:value-of select="ds:yearto"/>
          </yearto>
        </makemodel>
      </makemodels>
    </classified>
  </xsl:template>
</xsl:stylesheet>

Example

<?xml version="1.0" encoding="utf-8"?>
<cardealer>
  <classifieds>
    <classified>
      <unique_id>test</unique_id>
      <title>test</title>
      <description>test</description>
      <category_id>test</category_id>
      <price>test</price>
      <makemodels>
        <makemodel>
          <make>test</make>
          <model>test</model>
          <yearfrom>test</yearfrom>
          <yearto>test</yearto>
        </makemodel>
      </makemodels>
    </classified>
    <classified>
      <unique_id>test</unique_id>
      <title>test</title>
      <description>test</description>
      <category_id>test</category_id>
      <price>test</price>
      <makemodels>
        <makemodel>
          <make>test</make>
          <model>test</model>
          <yearfrom>test</yearfrom>
          <yearto>test</yearto>
        </makemodel>
      </makemodels>
    </classified>
    <classified>
      <unique_id>test</unique_id>
      <title>test</title>
      <description>test</description>
      <category_id>test</category_id>
      <price>test</price>
      <makemodels>
        <makemodel>
          <make>test</make>
          <model>test</model>
          <yearfrom>test</yearfrom>
          <yearto>test</yearto>
        </makemodel>
      </makemodels>
    </classified>
  </classifieds>
</cardealer>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM