Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.8 kB
2
Indexable
Never
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>XML Transformation</title>
</head>
<body>
    <textarea id="xmlInput" rows="10" cols="50">
        <товар>
            <наименование>Пример товара</наименование>
            <цена>100.00</цена>
            <количество>5</количество>
        </товар>
    </textarea>
    <br>
    <label for="percentage">Процент увеличения цены: </label>
    <input type="number" id="percentage" step="0.01" />
    <br>
    <button onclick="transformXml()">Преобразовать XML</button>
    <br>
    <textarea id="xmlOutput" rows="10" cols="50" readonly></textarea>

    <script>
        function transformXml() {
            const xmlString = document.getElementById('xmlInput').value;
            const percentage = parseFloat(document.getElementById('percentage').value);

            const parser = new DOMParser();
            const xmlDoc = parser.parseFromString(xmlString, 'application/xml');

            const priceElements = xmlDoc.getElementsByTagName('цена');

            for (let i = 0; i < priceElements.length; i++) {
                const currentPrice = parseFloat(priceElements[i].textContent);
                const newPrice = currentPrice * (1 + percentage / 100);
                priceElements[i].textContent = newPrice.toFixed(2);
            }

            const transformedXml = new XMLSerializer().serializeToString(xmlDoc);
            document.getElementById('xmlOutput').value = transformedXml;
        }
    </script>
</body>
</html>
Leave a Comment