Work backwards from an xslt to create an xml

2019-08-06 16:50发布

So, I have quite a few XSLT files that I need to generate a preview for, however I do not have the corresponding XML files. I was wondering if it was possible to go through an XSLT file and create a list of the required fields for that XSLT? For example,

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
      <xsl:for-each select="catalog/cd">
        <tr>
          <td><xsl:value-of select="title" /></td>
          <td><xsl:value-of select="artist" /></td>
        </tr>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

I want to go from that XSLT to this: "/catalog/cd/title, /catalog/cd/artist" as they are the fields required by this XSLT, or set a default value for all of them so it outputs "/catalog/cd/title" for title and "/catalog/cd/artist" for artist.

1条回答
够拽才男人
2楼-- · 2019-08-06 17:31

just to give it a shot, also this won't work with multiple templates:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" exclude-result-prefixes="xsl">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="xsl-namespace">http://www.w3.org/1999/XSL/Transform</xsl:variable>

<xsl:template match="*[namespace-uri() != $xsl-namespace]" >
    <xsl:copy>
        <xsl:apply-templates select="@*[namespace-uri() != $xsl-namespace]"/>
        <xsl:apply-templates select="*[namespace-uri() != $xsl-namespace]"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="@*[namespace-uri() != $xsl-namespace]" >
    <xsl:copy/>
</xsl:template>

查看更多
登录 后发表回答