How could I edit this xsl file so that it works fo

2019-08-17 13:35发布

问题:

I have one one xsl file that includes xhtml fragments. I need to remove those so that it becomes browser compatible. I tried but then it stops working. Here is original xsl:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:html="http://www.w3.org/1999/xhtml">
  <xsl:output omit-xml-declaration="yes" />
  <xsl:template match="messages">
    <html:ul>
      <xsl:apply-templates select="message" />
    </html:ul>
  </xsl:template>

  <xsl:template match="message[message]">
    <html:li>message <xsl:value-of select="@emp_msg" /></html:li>
    <html:ul>
      <xsl:apply-templates select="message" />
    </html:ul>
  </xsl:template>

  <xsl:template match="message">
    <html:li>message <xsl:value-of select="@emp_msg" /></html:li>
    <xsl:apply-templates select="message" />
  </xsl:template>
</xsl:stylesheet>

I tried to remove xhtml fragments this way but it stops working & prints,'No style information'. What wrong I did:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
  <xsl:output omit-xml-declaration="yes" />
  <xsl:template match="messages">
    <ul>
      <xsl:apply-templates select="message" />
    </ul>
  </xsl:template>
  <xsl:template match="message[message]">
    <li>message <xsl:value-of select="@emp_msg" /></li>
    <ul>
      <xsl:apply-templates select="message" />
    </ul>
  </xsl:template>

  <xsl:template match="message">
    <li>message <xsl:value-of select="@emp_msg" /></li>
    <xsl:apply-templates select="message" />
  </xsl:template>
</xsl:stylesheet>

回答1:

I see you have:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 

Try fixing the syntax!

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


回答2:

To work in a browser you must include a stylesheet link Processing Instruction at the beginning of the XML document, as in:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="myStyleSheet.xsl"?>
<rootOfXmlDocument>
  ....

The href= attribute must point to the stylesheet in such a way that the browser can resolve it (i.e. it needs to be a URL that can be fetched by the browser).



回答3:

If your posted sample is correct, you left out the > of the start tag of your <xsl:stylesheet> element. It needs to say

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

Note that Stack Overflow's syntax highlighting helps make that more obvious.

Update: OK... in light of that, do what Jim Garrison said if you haven't already, but if the original worked fine, you must have that already.

You might also try making sure the content-type of the XML source file is XML, if it's being retrieved via HTTP. And also try using

<xsl:output method="html" />

(possibly with omit-xml-declaration="yes").



标签: xslt