问题
Id like to generate a visual "documentation" for my xsl using php. What I want to do is basically to transform my xsl without an XML inorder to display how the XML fields will be displayed in the HTML.
To clarify:
xsl:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<head>
<title>My sample</title>
</head>
<body>
My sample element: <xsl:value-of select="root/element1"/>
</body>
</xsl:template>
</xsl:stylesheet>
Requested output:
<html>
<head>
<title>My sample</title>
</head>
<body>
My sample element: root/element1
</body>
</html>
Does anyone know how to do this?
BR, Jake
回答1:
XSLT is input-driven. If will generate different output for different input.
In any real-world scenario that is more complex than your simple example, looking at the code without any input to run through it means you can't say what the output is going to look like.
For your simple example, you could run your XSLT stylesheet thorugh another XSLT stylesheet.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="text" />
<xsl:template match="*">
<xsl:value-of select="concat('<', name())" />
<xsl:apply-templates select="@*" />
<xsl:value-of select="'>'" />
<xsl:apply-templates select="*" />
<xsl:value-of select="concat('</', name(), '>')" />
</xsl:template>
<xsl:template match="@*">
<xsl:value-of select="concat(' ', name(), '="', ., '"')" />
</xsl:template>
<xsl:template match="xsl:*">
<xsl:apply-templates select="*" />
</xsl:template>
<xsl:template match="xsl:value-of">
<xsl:value-of select="concat('{{value-of: ', @select, '}}')" />
</xsl:template>
<!-- add appropriate templates for the other XSLT elements -->
</xsl:stylesheet>
With your sample, this produces the string
<head><title></title></head><body>{{value-of: root/element1}}</body>
However, the "add appropriate templates for the other XSLT elements" part is the difficult bit. Your output will be in the order of the input (XSLT is input-driven, as I said). Your XSLT program will most likely not be layed out the same way as the output it is going to produce, so generating sensible documentation from it might be quite a bit harder than you think.
来源:https://stackoverflow.com/questions/10304459/generate-xsl-documentation