为什么我的XSLT转换会丢弃大多数XML标记?

时间:2015-03-02 01:58:40

标签: xml xslt

我是XML和XSLT的新手。我正在尝试做的是一个XSLT转换,它匹配我的XML文档中的任何元素类型相册/名称,并将它们的值转换为链接。到目前为止,我所做的一半是其中的一半。我很难理解如何获得链接。任何关于如何实现这一点的建议都将受到赞赏。

编辑:使用xsltproc -o new_inventory.xml music_inventory.xsl music_inventory.xml和下面的XML和XSLT,它似乎是按照我的意愿放入链接,但是当我尝试从浏览器查看时,我收到文档为空的错误消息。

这是由上面的命令创建的new_inventory.xml。 (注意任何元素周围没有XML标签接受链接。)

<?xml version="1.0"?>


  Led Zepplin
  <id><a xmlns="http://www.w3.org/1999/xhtml" href="LEDZEP.xhtml">Mothership</a$
  1968
  Atlantic
  1
  1

   Good Times Bad Times
   Communication Breakdown
   Dazed and Confused
   Babe I'm gonna Leave You
   Whole Lotta Love
   Ramble On
   Heartbreaker
   Immigrant Song
   Since I've Been Loving You

XML:

<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>

XSLT

   <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>

1 个答案:

答案 0 :(得分:2)

  

注意任何元素周围没有xml标签接受链接

如果您将身份转换添加到XSLT,

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

  <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"?><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>
  <id><a xmlns="http://www.w3.org/1999/xhtml" href="LEDZEP.xhtml">Mothership</a></id>
  <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>
  <id><a xmlns="http://www.w3.org/1999/xhtml" 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>
</music_inventory>