如何将Namecheap API的XML响应通过XSLT转换为易读表格?
XSLT to Convert Namecheap Pricing API Response to Human-Readable Table
Here's a tailored XSLT that will take your Namecheap users.getpricing API response and turn it into a clean, easy-to-read HTML table. I've accounted for the XML namespace and typical structure of this specific API call:
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="http://api.domain.com/xml.response"> <xsl:output method="html" indent="yes" encoding="utf-8"/> <!-- Match the root API response element --> <xsl:template match="/ns:ApiResponse"> <html> <head> <title>Namecheap Pricing Details</title> <style> table { border-collapse: collapse; width: 80%; margin: 20px auto; } th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } th { background-color: #f2f2f2; } .status-ok { color: green; font-weight: bold; } .status-error { color: red; font-weight: bold; } </style> </head> <body> <h1>Namecheap API Pricing Response</h1> <!-- Display response status --> <p>Response Status: <span class="status-{@Status}"><xsl:value-of select="@Status"/></span></p> <p>Requested Command: <xsl:value-of select="ns:RequestedCommand"/></p> <!-- Check if there are errors/warnings --> <xsl:if test="ns:Errors/*"> <h3>Errors:</h3> <ul> <xsl:for-each select="ns:Errors/*"> <li><xsl:value-of select="."/></li> </xsl:for-each> </ul> </xsl:if> <xsl:if test="ns:Warnings/*"> <h3>Warnings:</h3> <ul> <xsl:for-each select="ns:Warnings/*"> <li><xsl:value-of select="."/></li> </xsl:for-each> </ul> </xsl:if> <!-- Generate pricing table --> <xsl:if test="ns:CommandResponse"> <h2>Pricing Details</h2> <table> <thead> <tr> <th>Product Name</th> <th>Price (USD)</th> <th>Type</th> <th>Duration</th> </tr> </thead> <tbody> <!-- Adjust this XPath to match your actual CommandResponse structure --> <xsl:for-each select="ns:CommandResponse/ns:Product"> <tr> <td><xsl:value-of select="@Name"/></td> <td><xsl:value-of select="ns:Price"/></td> <td><xsl:value-of select="@Type"/></td> <td><xsl:value-of select="@Duration"/></td> </tr> </xsl:for-each> </tbody> </table> </xsl:if> </body> </html> </xsl:template> </xsl:stylesheet>
Key Notes to Adapt This to Your Exact XML:
- Namespace Handling: The XML uses
http://api.domain.com/xml.response, so we've assigned the prefixnsto it. All XPath references to elements in this namespace must use thens:prefix. - CommandResponse Structure: Since your XML was cut off, I assumed a common structure for
namecheap.users.getpricing(products with name, price, type, duration). If your actual response has different elements (likeSubscriptionPrice,RenewalPrice, etc.), modify the<xsl:for-each>XPath and table columns to match. - Styling: The embedded CSS adds basic formatting to make the table readable—feel free to tweak colors, widths, or spacing to your preference.
- Error/Warning Handling: The XSLT will display any errors or warnings present in the response, which helps with debugging if something goes wrong.
To use this, simply apply the XSLT to your XML response using any XSLT processor (like xsltproc on the command line, or tools like Oxygen XML Editor).
内容的提问来源于stack exchange,提问作者Manos Krokos
相关产品推荐
相关产品推荐

