XSL转换为包含css

时间:2015-03-06 04:55:18

标签: css xml xslt

我试图通过XSLT复制xml,并且源XML中的CSS被放置在xhtml xml文件的主体中,并且head元素看起来是空的。如何在正确的位置获取CSS引用,以便格式化新创建的XML文件。

源XML

<?xml version="1.0" encoding="utf-8"?>

<?DOCTYPE album SYSTEM "music_inventory.dtd"?>
<?xml-stylesheet type="text/css" href="music_inventory.css"?>
<music_inventory>
 <album id="LEDZEP" type="full_length" albumart="http://upload.wikimedia.org/wikipedia/en/c/cb/Led_Zeppelin_-_Mothership.jpg">
  <artist>Led Zepplin</artist>
  <name>Mothership</name>
  <year>1968</year>
  <label>Atlantic</label>
  <disc>1</disc>
  <totaldiscs>1</totaldiscs>
  <tracklist>
   <track id="1">Good Times Bad Times</track>
   <track id="2">Communication Breakdown</track>
   <track id="3">Dazed and Confused</track>
   <track id="4">Babe I'm gonna Leave You</track>
   <track id="5">Whole Lotta Love</track>
   <track id="6">Ramble On</track>
   <track id="7">Heartbreaker</track>
   <track id="8">Immigrant Song</track>
   <track id="9">Since I've Been Loving You</track>
   <track id="10">Rock and Roll</track>
   <track id="11">Black Dog</track>
   <track id="12">When the Levee Breaks</track>
   <track id="13">Stairway to Heaven</track>
  </tracklist>
 </album>
 <album id="SUBL" type="full_length" albumart="http://upload.wikimedia.org/wikipedia/en/thumb/9/94/Sublime_Self-Titled.jpg/220px-Sublime_Self-Titled.jpg">
  <artist>Sublime</artist>
  <name>Sublime</name>
  <year>1996</year>
  <label>MCA</label>
  <disc>1</disc>
  <totaldiscs>1</totaldiscs>
  <tracklist>
   <track id="1">Garden Grove</track>
   <track id="2">What I Got</track>
   <track id="3">Wrong Way</track>
   <track id="4">Same in the End</track>
   <track id="5">April 29, 1992 (Miami)</track>
   <track id="6">Santeria</track>
   <track id="7">Seed</track>
   <track id="8">Jailhouse</track>
   <track id="9">Pawn Shop</track>
   <track id="10">Paddle Out</track>
   <track id="11">The Ballad of Johnny Butt</track>
   <track id="12">Burritos</track>
   <track id="13">Under My Voodoo</track>
   <track id="14">Get Ready</track>
   <track id="15">Caress Me Down</track>
   <track id="16">What I Got (Reprise)</track>
   <track id="17">Doin' Time</track>
  </tracklist>
 </album>
</music_inventory>

XSL

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

       <xsl:output method="xml" />


       <xsl:template match="* | @* | processing-instruction()">
          <xsl:copy>
              <xsl:apply-templates select="* | @* | text() | processing-instruction()"/>
          </xsl:copy>
       </xsl:template>



      <xsl:param name="albumid">SUBL</xsl:param>
       <xsl:template match="/">
         <html> 
         <body>
          <xsl:apply-templates/>
         </body>
         </html>
       </xsl:template>
       <xsl:template match="music_inventory">
         <xsl:apply-templates select="album[@id=$albumid]"/>
       </xsl:template>

       <!--creates hyperlink-->
       <xsl:template match="album/name">
        <id>
         <a xmlns="http://www.w3.org/1999/xhtml"
          href="{../@id}.xhtml">
           <xsl:value-of select="."/>
         </a>
        </id>
       </xsl:template>

    </xsl:stylesheet>

生成的XML(注意样式表引用的位置)

    <?xml version="1.0" encoding="UTF-8"?><html xmlns="http://www.w3.org/1999/xhtml">
<body><?xml-stylesheet type="text/css" href="music_inventory.css"?><album xmlns="" id="SUBL" type="full_length" albumart="http://upload.wikimedia.org/wikipedia/en/thumb/9/94/Sublime_Self-Titled.jpg/220px-Sublime_Self-Titled.jpg">
      <artist>Sublime</artist>
      <id xmlns="http://www.w3.org/1999/xhtml"><a href="SUBL.xhtml">Sublime</a></id>
      <year>1996</year>
      <label>MCA</label>
      <disc>1</disc>
      <totaldiscs>1</totaldiscs>
      <tracklist>
       <track id="1">Garden Grove</track>
       <track id="2">What I Got</track>
       <track id="3">Wrong Way</track>
       <track id="4">Same in the End</track>
       <track id="5">April 29, 1992 (Miami)</track>
       <track id="6">Santeria</track>
       <track id="7">Seed</track>
       <track id="8">Jailhouse</track>
       <track id="9">Pawn Shop</track>
       <track id="10">Paddle Out</track>
       <track id="11">The Ballad of Johnny Butt</track>
       <track id="12">Burritos</track>
       <track id="13">Under My Voodoo</track>
       <track id="14">Get Ready</track>
       <track id="15">Caress Me Down</track>
       <track id="16">What I Got (Reprise)</track>
       <track id="17">Doin' Time</track>
      </tracklist>
     </album></body></html>

2 个答案:

答案 0 :(得分:2)

匹配/的模板存在问题。您可以使用此样式表:

<xsl:stylesheet  version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" >
    <xsl:output method="xml" />
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:param name="albumid">SUBL</xsl:param>
    <xsl:template match="/">
        <xsl:apply-templates select="processing-instruction()"/>
        <html>
            <body>
                <xsl:apply-templates select="*"/>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="music_inventory">
        <xsl:apply-templates select="album[@id=$albumid]"/>
    </xsl:template>
    <!--creates hyperlink-->
    <xsl:template match="album/name">
        <id>
            <a xmlns="http://www.w3.org/1999/xhtml" href="{../@id}.xhtml">
                <xsl:value-of select="."/>
            </a>
        </id>
    </xsl:template>
</xsl:stylesheet>

答案 1 :(得分:0)

  

head元素显示为空。

输出中没有头元素。如果你想要一个头部元素,你必须创建它 - 如果你不想让它成为空的,你必须把它放进去。

例如,你可以这样做:

<xsl:template match="/">
    <html> 
        <head>
            <xsl:copy-of select="processing-instruction('xml-stylesheet')" />
        </head>
        <body>
            <xsl:apply-templates select="music_inventory/album[@id=$albumid]"/>
        </body>
    </html>
</xsl:template>

这会在输出文档中创建一个head部分,并将链接到样式表的处理指令复制到其中,而不做任何更改。

这对你来说不是很清楚。我认为你的样式表存在许多问题,这些问题与你当前的问题无关,而我现在还没有时间进入。结果,很难说出你想要最终得到的结果究竟是什么,以及这样的结果是否对任何事物都有用。