HTML : Example of xquery in html

Blessings Photo

Blessings
2 years 2 Views
Category:
Description:

HTML : Example of xquery in html To Access My Live Chat Page, On Google, Search for "hows tech developer connect" As I ...

Here's a simple example of how to use XQuery within an HTML page. This example assumes you are working with an XML document and want to display the results of an XQuery execution.

Example XML Data

Let's consider the following XML data saved in a file named products.xml:

xml
<products>
    <product>
        <name>Apple</name>
        <price>1.20</price>
        <category>Fruit</category>
    </product>
    <product>
        <name>Banana</name>
        <price>0.50</price>
        <category>Fruit</category>
    </product>
    <product>
        <name>Carrot</name>
        <price>0.70</price>
        <category>Vegetable</category>
    </product>
</products>

XQuery Example

You can write an XQuery to extract product names and prices. Here’s how you would structure the HTML file:

html

            // Parse the XML response
            const parser = new DOMParser();
            const xmlDoc = parser.parseFromString(xmlData, "application/xml");

            // Extract product names and prices
            const products = xmlDoc.getElementsByTagName('product');
            const productList = document.getElementById('product-list');

            Array.from(products).forEach(product => {
                const name = product.getElementsByTagName('name')[0].textContent;
                const price = product.getElementsByTagName('price')[0].textContent;

                const productItem = document.createElement('div');
                productItem.textContent = `${name}: $${price}`;
                productList.appendChild(productItem);
            });
        }

        // Call the function to fetch and display products
        fetchProducts();
    </script>
</body>
</html>

html 1

Explanation

  1. HTML Structure: The HTML document includes a header and a div to display the product list.
  2. JavaScript Function: The fetchProducts function makes an asynchronous request to an endpoint where the XQuery is executed.
  3. XML Parsing: The response is parsed into an XML document, and products are extracted using getElementsByTagName.
  4. Display Products: Each product's name and price are displayed in the div.

Note

  • Replace 'path/to/your/xquery-endpoint' with the actual endpoint that executes your XQuery and returns the XML data.
  • This example demonstrates how to integrate XQuery results into an HTML page using JavaScript for dynamic content rendering.

Feel free to adapt this example to fit your specific use case!