Home > Net >  copy one complete element to a new XML when match the value of an attribute, and changing the value
copy one complete element to a new XML when match the value of an attribute, and changing the value

Time:07-11

Hello everyone Im a newbie with XSLT, I have this XML file :

 <?xml version="1.0" encoding="UTF-8"?>
 <FN>
    <START>     
       <VALUE2><VALUE2>
       <VALUE3></VALUE3>
       <VALUE4></VALUE4>
    </START>
    <SET name="-RBS_00">
        <DISH name="-00_01" QR="Buffer">
            <OBJETS>
                <Ingredients total="3">
                </Ingredients>
            </OBJETS>
            <Quantity>
            </Quantity>
        </DISH>
        <DISH name="-00_02" QR="RESTO">
            <OBJETS meat="poulet">
                <Ingredients total="5"></Ingredients>
            </OBJETS>
            <Quantity>
                <Adults numbers="4">
                </Adults>
                <Kids numbers="3">
                </Adults>
            </Quantity>
        </DISH>
        <DISH name="-00_03" QR="DELIVERY">
            <OBJETS meat="jam">
                <Slices total="3"></Slices>
            </OBJETS>
            <OBJETS meat="chicken">
                <Slices total="5"></Slices>
            </OBJETS>
            <OBJETS meat="porc">
                <Slices total="1"></Slices>
            </OBJETS>
            <Quantity>
                <Adults numbers="1">
                </Adults>
                <Kids numbers="1">
                </Adults>
            </Quantity>
        </DISH
        <DISH name="-00_04" QR="KIDS">
            <OBJETS meat="veggie">
                <Ingredients total="11"></Ingredients>
            </OBJETS>
            <Quantity>
                <Adults numbers="10">
                </Adults>
                <Kids numbers="2">
                </Adults>
            </Quantity>
        </DISH
      <SET>
</FN>

Im trying to copy just the entire element of a dish when the value of the attribute name is "-00_03" and is there any attribute inside the element that match meat="porc" change it for meat="cheese" so I would have a new xml like:

<DISH name="-00_03" QR="DELIVERY">
    <OBJETS meat="jam">
        <Slices total="3"></Slices>
    </OBJETS>
    <OBJETS meat="chicken">
        <Slices total="5"></Slices>
    </OBJETS>
    <OBJETS meat="cheese">
         <Slices total="1"></Slices>
    </OBJETS>
    <Quantity>
        <Adults numbers="1"></Adults>
        <Kids numbers="1"></Adults>
    </Quantity>
</DISH>

This is the XSLT that I have to copy the element (It doest not work at all) and for changing the value of the attribute I have no idea.

<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:template match="/FN/SET">
    <xsl:copy>
        <xsl:copy-of select="*"/>
        <xsl:copy-of select="//DISH [@name='-00_03']"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Any help would be really appreciated, thank you very much in advance.

CodePudding user response:

(There are several typos in your XML source.)

Your stylesheet should contain

  • a template that recursively copies a node (the "default rule")
  • templates for any exceptions to this default rule
  • a template with match="/" that gives the starting point.

The three templates in your case are:

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>
<xsl:template match="OBJETS/@meat[.='porc']">
  <xsl:attribute name="meat">cheese</xsl:attribute>
</xsl:template>
<xsl:template match="/">
  <xsl:apply-templates select="//DISH[@name='-00_03']"/>
</xsl:template>
  • Related