Search code examples
phpxmlxsltfeed

Edit xml file with PHP or XSLT


i need big help from you, i am totaly lost in this problem, i googling all the time but nothing what can help me. When I found some solutions, it did not work.....Help me pleas :/

Edit it in PHP or XSLT pleas, Thx.

Input:

<SHOP>
    <SHOPITEM>
        <PRODUCTNAME>Table</PRODUCTNAME>
        <DESCRIPTION>slaklasdk askdalsd dsasd</DESCRIPTION>
        <URL>http://www.google.com/</URL>
        <IMGURL>http://www.google.com/</IMGURL>
        <PRICE>79</PRICE>
        <CATEGORIES>
            <CATEGORY>Work, table</CATEGORY>
        </CATEGORIES>
        <MOTIVES>
            <MOTIVE>Brown oak</MOTIVE>
        </MOTIVES>
        <SIZES>
            <SIZE>
                <SIZEID>38626</SIZEID>
                <SIZENAME>uni</SIZENAME>
                <STOREPIECES>12</STOREPIECES>
            </SIZE>
        </SIZES>
</SHOPITEM>
<SHOPITEM>
        <PRODUCTNAME>Book</PRODUCTNAME>
        <DESCRIPTION>asdasdasdawra asdaseasd</DESCRIPTION>
        <URL>http://www.google.com/</URL>
        <IMGURL>http://www.google.com/</IMGURL>
        <PRICE>79</PRICE>
        <CATEGORIES>
            <CATEGORY>Book, Magazine</CATEGORY>
        </CATEGORIES>
        <MOTIVES>
            <MOTIVE>Black with blue stars</MOTIVE>
        </MOTIVES>
        <SIZES>
            <SIZE>
                <SIZEID>38626</SIZEID>
                <SIZENAME>uni</SIZENAME>
                <STOREPIECES>0</STOREPIECES>
            </SIZE>
        </SIZES>
    </SHOPITEM>

I NEED:

1)delete <URL> and <IMGURL>

2)<PRODUCTNAME> rename <title>

3)<CATEGORIES> delete and rename <CATEGORY> on <ctg_0>

4)<MOTIVE> merge with <DESCRIPTION> and delete <MOTIVES>,<MOTIVE>

5)delete <SIZES>, <SIZE> and <SIZEID> after that <SIZENAME> merge with <DISCRIPTION> and add new element <unit>0</unit> and rename <STOREPIECES> on <pieces>

Output:

<SHOP>
    <SHOPITEM>
        <title>Table</title>
        <DESCRIPTION>slaklasdk askdalsd dsasd, Motive:Brown oak, size:uni</DESCRIPTION>
        <PRICE>79</PRICE>
        <ctg_0>Book, Magazine</ctg_0>
        <pieces>12</pieces>
        <unit>0</unit>
    </SHOPITEM>
    <SHOPITEM>
        <title>Book</title>
        <DESCRIPTION>asdasdasdawra asdaseasd, Motive:Black with blue stars, size:uni</DESCRIPTION>
        <PRICE>79</PRICE>
        <ctg_0>Book, Magazine</ctg_0>
        <pieces>0</pieces>
        <unit>0</unit>
    </SHOPITEM>
</SHOP>

Solution

  • Learn XSL. Quick crash course: xsl matches the least generic match first so if you have a rule for * and a rule for URL, it will apply the rule for URL instead of *. . is the current node. * are all child nodes of the current node. text() is all the text under the current node, or a single text node in a match. @* are all the attributes of the current node. name(.) is the name of the current node.

    Therefore, this recursively copies (piecemeal) every node/text/attribute in the file:

    <xsl:template match="*">
        <xsl:element name="{name(.)}">
            <xsl:apply-templates select="*|@*|text()"/>
        </xsl:element>
    </xsl:template>
    
    <xsl:template match="@*|text()">
        <xsl:copy-of select='.'/>
    </xsl:template>
    

    Now add the more specific matches for those you want to change. If you want to remove a node:

    <xsl:template match="URL|IMGURL"/><!-- Do nothing, don't copy it -->
    

    Remember, the more specific match comes first. Renaming a node, replace {name(.)} (the name of the current node) with what you want to change it to.

    That should get you started, the rest is up to you.