2013-05-16 67 views
0

我想知道如何從我的xml文檔中獲取不同的命名空間以用於轉換爲xsl-fo。我有如下:從xml文檔獲取不同的命名空間

XML:

<?xml version="1.0" ?> 
<?xml-stylesheet type="text/xsl" href="style.xsl" ?> 
<ovgs xmlns:ncb="http://www.namcobandai.com/NS" xmlns:sqe="http://www.squareenix.com/NS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="main.xsd"> 
<upcoming> 
<ncb:game> 
    <ncb:cover>nnk_cover.jpg</ncb:cover> 
    <ncb:title>Ni no Kuni: Wrath of the White Witch</ncb:title> 
    <ncb:publisher>Namco Bandai Games America</ncb:publisher> 
    <ncb:developer>Level 5</ncb:developer> 
    <ncb:genre>Role-Playing</ncb:genre> 
    <ncb:releaseDate> 
     <ncb:yyyy>2013</ncb:yyyy> 
     <ncb:mm>01</ncb:mm> 
     <ncb:dd>22</ncb:dd> 
    </ncb:releaseDate> 
    <ncb:esrbRating>E10+</ncb:esrbRating> 
    <ncb:platforms> 
     <ncb:platform>PS3</ncb:platform> 
    </ncb:platforms> 
</ncb:game> 
<sqe:game> 
    <sqe:cover>tr_cover.jpg</sqe:cover> 
    <sqe:title>Tomb Raider</sqe:title> 
    <sqe:publisher>Square Enix</sqe:publisher> 
    <sqe:developer>Crystal Dynamics</sqe:developer> 
    <sqe:genre>Action</sqe:genre> 
    <sqe:releaseDate> 
     <sqe:yyyy>2013</sqe:yyyy> 
     <sqe:mm>03</sqe:mm> 
     <sqe:dd>05</sqe:dd> 
    </sqe:releaseDate> 
    <sqe:esrbRating>M</sqe:esrbRating> 
    <sqe:platforms> 
     <sqe:platform>X360</sqe:platform> 
     <sqe:platform>PC</sqe:platform> 
     <sqe:platform>PS3</sqe:platform> 
    </sqe:platforms> 
</sqe:game> 
<sqe:game> 
    <sqe:cover>hma_cover.jpg</sqe:cover> 
    <sqe:title>Hitman: Absolution</sqe:title> 
    <sqe:publisher>Square Enix</sqe:publisher> 
    <sqe:developer>Io Interactive</sqe:developer> 
    <sqe:genre>Action</sqe:genre> 
    <sqe:releaseDate> 
     <sqe:yyyy>2012</sqe:yyyy> 
     <sqe:mm>11</sqe:mm> 
     <sqe:dd>20</sqe:dd> 
    </sqe:releaseDate> 
    <sqe:esrbRating>M</sqe:esrbRating> 
    <sqe:platforms> 
     <sqe:platform>X360</sqe:platform> 
     <sqe:platform>PC</sqe:platform> 
     <sqe:platform>PS3</sqe:platform> 
    </sqe:platforms> 
</sqe:game> 
</upcoming> 

我想是對將採取的信息,並將其轉換成一個XSL-FO文件的XSLT文件的代碼。基本上我正在努力的是如何參考節點。我有什麼至今:

<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="1.0" xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
<xsl:output method="xml" indent="yes" /> 
<xsl:template match="/"> 
    <fo:root> 
     <!-- overall layout --> 
     <fo:layout-master-set> 
      <fo:simple-page-master master-name="coverPage"> 
       <fo:region-body/> 
       <fo:region-before extent="1in" /> 
       <fo:region-after extent="1in" /> 
       <fo:region-start extent="1in" /> 
       <fo:region-end extent="1in" /> 
      </fo:simple-page-master> 

      <fo:simple-page-master master-name="contentPage"> 
       <fo:region-body/> 
       <fo:region-before extent="1in" background-color="#0000FF" /> 
       <fo:region-after extent="1in" background-color="#0000FF" /> 
       <fo:region-start/> 
       <fo:region-end/> 
      </fo:simple-page-master> 
     </fo:layout-master-set> 
     <!-- page content --> 
     <fo:page-sequence master-reference="coverPage"> 
      <fo:flow flow-name="xsl-region-body"> 
       <fo:block margin-top="2.5in" text-align="center"><fo:external-graphic content-height="scale-to-fit" height="2.5in" src="ovgsLogo.jpg" /></fo:block> 
       <fo:block font-weight="bold" text-align="center">Greetings all yee gamers!</fo:block> 
      </fo:flow> 
     </fo:page-sequence> 

     <xsl:for-each select="../upcoming/ncb:game"> 
      <fo:page-sequence master-reference="contentPage"> 
       <fo:static-content flow-name="xsl-region-before"> 
        <fo:block margin-top="0.3in" text-align="center">OVGS Upcoming Games</fo:block> 
       </fo:static-content> 

       <fo:static-content flow-name="xsl-region-after"> 
        <fo:block margin-top="0.3in" text-align="center">Page <fo:page-number /></fo:block> 
       </fo:static-content> 

       <fo:flow flow-name="xsl-region-body"> 
        <fo:block font-size="24pt" font-weight="bold" text-align="center" margin-top="2.5in" color="#FF0000"><xsl:value-of select="ncb:title" /></fo:block> 
        <fo:block text-align="center" margin-top="0.2in"><fo:external-graphic content-height="scale-to-fit" height="3in"><xsl:attribute name="src">url('<xsl:value-of select="ncb:cover" />')</xsl:attribute></fo:external-graphic></fo:block> 
        <fo:block font-weight="bold" text-align="center" margin-top="0.3in">Published by <xsl:value-of select="ncb:publisher" /></fo:block> 
        <fo:block font-weight="bold" text-align="center">Developed by <xsl:value-of select="ncb:developer" /></fo:block> 
        <fo:block font-weight="bold" text-align="center"><xsl:value-of select="ncb:genre" /></fo:block> 
        <fo:block font-weight="bold" text-align="center">Released on <xsl:value-of select="ncb:releaseDate/ncb:dd" />/<xsl:value-of select="ncb:releaseDate/ncb:mm" />/<xsl:value-of select="ncb:releaseDate/ncb:yyyy" /></fo:block> 
        <fo:block font-weight="bold" text-align="center"> 
         Available on <xsl:value-of select="ncb:platforms/ncb:platform[1]" /><xsl:for-each select="ncb:platforms/ncb:platform"> 
         <xsl:if test="position() != last() and position() != 1">, <xsl:value-of select="." /></xsl:if></xsl:for-each> and <xsl:value-of select="ncb:platforms/ncb:platform[last()]" /> 
        </fo:block> 
        <fo:block text-align="center" background-color="#9900FF" margin-top="0.3in" margin-left="1in" margin-right="1in"><xsl:value-of select="ncb:esrbRating" /></fo:block> 
       </fo:flow> 
      </fo:page-sequence> 
     </xsl:for-each> 

     <xsl:for-each select="ovgs/upcoming/sqe:game"> 
      <fo:page-sequence master-reference="contentPage"> 
       <fo:static-content flow-name="xsl-region-before"> 
        <fo:block margin-top="0.3in" text-align="center">OVGS Upcoming Games</fo:block> 
       </fo:static-content> 

       <fo:static-content flow-name="xsl-region-after"> 
        <fo:block margin-top="0.3in" text-align="center">Page <fo:page-number /></fo:block> 
       </fo:static-content> 

       <fo:flow flow-name="xsl-region-body"> 
        <fo:block font-size="24pt" font-weight="bold" text-align="center" margin-top="2.5in" color="#FF0000"><xsl:value-of select="sqe:title" /></fo:block> 
        <fo:block text-align="center" margin-top="0.2in"><fo:external-graphic content-height="scale-to-fit" height="3in"><xsl:attribute name="src">url('<xsl:value-of select="sqe:cover" />')</xsl:attribute></fo:external-graphic></fo:block> 
        <fo:block font-weight="bold" text-align="center" margin-top="0.3in">Published by <xsl:value-of select="sqe:publisher" /></fo:block> 
        <fo:block font-weight="bold" text-align="center">Developed by <xsl:value-of select="sqe:developer" /></fo:block> 
        <fo:block font-weight="bold" text-align="center"><xsl:value-of select="sqe:genre" /></fo:block> 
        <fo:block font-weight="bold" text-align="center">Released on <xsl:value-of select="sqe:releaseDate/sqe:dd" />/<xsl:value-of select="sqe:releaseDate/sqe:mm" />/<xsl:value-of select="sqe:releaseDate/sqe:yyyy" /></fo:block> 
        <fo:block font-weight="bold" text-align="center"> 
         Available on <xsl:value-of select="sqe:platforms/sqe:platform[1]" /><xsl:for-each select="sqe:platforms/sqe:platform"> 
         <xsl:if test="position() != last() and position() != 1">, <xsl:value-of select="." /></xsl:if></xsl:for-each> and <xsl:value-of select="sqe:platforms/sqe:platform[last()]" /> 
        </fo:block> 
        <fo:block text-align="center" background-color="#9900FF" margin-top="0.3in" margin-left="1in" margin-right="1in"><xsl:value-of select="sqe:esrbRating" /></fo:block> 
       </fo:flow> 
      </fo:page-sequence> 
     </xsl:for-each> 

    </fo:root> 
</xsl:template> 

+0

看來你有空「育碧:標題」這就是爲什麼你沒有得到任何輸出元素。 –

+0

您能向我們展示比40個字符更多的嘗試嗎? – JLRishe

+0

現在好了嗎? – wizzkid

回答

1

使用下面的編碼拿到冠軍

//*[local-name(.)='game'][1]/*[local-name(.)='title'] 

也能改變你輸入XML按照下面的編碼:

<?xml version="1.0"?> 
<test xmlns:ubisoft="http://www.mytext.com" xmlns:atari="http://www.yourtext.com"> 
<ubisoft:game> 
    <ubisoft:title>Assassin's creed</ubisoft:title> 
    <ubisoft:rating/> 
    <!--etc--> 
</ubisoft:game> 
<ubisoft:game> 
    <ubisoft:title>Far Cry 3</ubisoft:title> 
    <ubisoft:rating/> 
    <!--etc--> 
</ubisoft:game> 
<atari:game> 
    <atari:title/> 
    <atari:rating/> 
    <!--etc--> 
</atari:game> 
<atari:game> 
    <atari:title/> 
    <atari:rating/> 
    <!--etc--> 
</atari:game> 
</test> 

請嘗試以下XSLT進行改造:

<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="1.0" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:ncb="http://www.namcobandai.com/NS" xmlns:sqe="http://www.squareenix.com/NS"> 
<xsl:output method="xml" indent="yes" /> 
<xsl:template match="/"> 
    <fo:root> 
     <!-- overall layout --> 
     <fo:layout-master-set> 
      <fo:simple-page-master master-name="coverPage"> 
       <fo:region-body/> 
       <fo:region-before extent="1in" /> 
       <fo:region-after extent="1in" /> 
       <fo:region-start extent="1in" /> 
       <fo:region-end extent="1in" /> 
      </fo:simple-page-master> 

      <fo:simple-page-master master-name="contentPage"> 
       <fo:region-body/> 
       <fo:region-before extent="1in" background-color="#0000FF" /> 
       <fo:region-after extent="1in" background-color="#0000FF" /> 
       <fo:region-start/> 
       <fo:region-end/> 
      </fo:simple-page-master> 
     </fo:layout-master-set> 
     <!-- page content --> 
     <fo:page-sequence master-reference="coverPage"> 
      <fo:flow flow-name="xsl-region-body"> 
       <fo:block margin-top="2.5in" text-align="center"><fo:external-graphic content-height="scale-to-fit" height="2.5in" src="ovgsLogo.jpg" /></fo:block> 
       <fo:block font-weight="bold" text-align="center">Greetings all yee gamers!</fo:block> 
      </fo:flow> 
     </fo:page-sequence> 

     <xsl:for-each select="../upcoming/*[local-name(.)='game']"> 
      <fo:page-sequence master-reference="contentPage"> 
       <fo:static-content flow-name="xsl-region-before"> 
        <fo:block margin-top="0.3in" text-align="center">OVGS Upcoming Games</fo:block> 
       </fo:static-content> 

       <fo:static-content flow-name="xsl-region-after"> 
        <fo:block margin-top="0.3in" text-align="center">Page <fo:page-number /></fo:block> 
       </fo:static-content> 

       <fo:flow flow-name="xsl-region-body"> 
        <fo:block font-size="24pt" font-weight="bold" text-align="center" margin-top="2.5in" color="#FF0000"><xsl:value-of select="*[local-name(.)='title']" /></fo:block> 
        <fo:block text-align="center" margin-top="0.2in"><fo:external-graphic content-height="scale-to-fit" height="3in"><xsl:attribute name="src">url('<xsl:value-of select="*[local-name(.)='cover']" />')</xsl:attribute></fo:external-graphic></fo:block> 
        <fo:block font-weight="bold" text-align="center" margin-top="0.3in">Published by <xsl:value-of select="*[local-name(.)='publisher']" /></fo:block> 
        <fo:block font-weight="bold" text-align="center">Developed by <xsl:value-of select="*[local-name(.)='developer']" /></fo:block> 
        <fo:block font-weight="bold" text-align="center"><xsl:value-of select="*[local-name(.)='genre']" /></fo:block> 
        <fo:block font-weight="bold" text-align="center">Released on <xsl:value-of select="*[local-name(.)='releaseDate']/*[local-name(.)='dd']" />/<xsl:value-of select="*[local-name(.)='releaseDate']/*[local-name(.)='mm']" />/<xsl:value-of select="*[local-name(.)='releaseDate']/*[local-name(.)='yyyy']" /></fo:block> 
        <fo:block font-weight="bold" text-align="center"> 
         Available on <xsl:value-of select="*[local-name(.)='platforms']/*[local-name(.)='platform[1]']" /><xsl:for-each select="*[local-name(.)='platforms']/*[local-name(.)='platform']"> 
         <xsl:if test="position() != last() and position() != 1">, <xsl:value-of select="." /></xsl:if></xsl:for-each> and <xsl:value-of select="*[local-name(.)='platforms']/*[local-name(.)='platform[last()]']" /> 
        </fo:block> 
        <fo:block text-align="center" background-color="#9900FF" margin-top="0.3in" margin-left="1in" margin-right="1in"><xsl:value-of select="*[local-name(.)='esrbRating']" /></fo:block> 
       </fo:flow> 
      </fo:page-sequence> 
     </xsl:for-each> 

     <xsl:for-each select="ovgs/upcoming/*[local-name(.)='game']"> 
      <fo:page-sequence master-reference="contentPage"> 
       <fo:static-content flow-name="xsl-region-before"> 
        <fo:block margin-top="0.3in" text-align="center">OVGS Upcoming Games</fo:block> 
       </fo:static-content> 

       <fo:static-content flow-name="xsl-region-after"> 
        <fo:block margin-top="0.3in" text-align="center">Page <fo:page-number /></fo:block> 
       </fo:static-content> 

       <fo:flow flow-name="xsl-region-body"> 
        <fo:block font-size="24pt" font-weight="bold" text-align="center" margin-top="2.5in" color="#FF0000"><xsl:value-of select="*[local-name(.)='title']" /></fo:block> 
        <fo:block text-align="center" margin-top="0.2in"><fo:external-graphic content-height="scale-to-fit" height="3in"><xsl:attribute name="src">url('<xsl:value-of select="*[local-name(.)='cover']" />')</xsl:attribute></fo:external-graphic></fo:block> 
        <fo:block font-weight="bold" text-align="center" margin-top="0.3in">Published by <xsl:value-of select="*[local-name(.)='publisher']" /></fo:block> 
        <fo:block font-weight="bold" text-align="center">Developed by <xsl:value-of select="*[local-name(.)='developer']" /></fo:block> 
        <fo:block font-weight="bold" text-align="center"><xsl:value-of select="*[local-name(.)='genre']" /></fo:block> 
        <fo:block font-weight="bold" text-align="center">Released on <xsl:value-of select="*[local-name(.)='releaseDate']/*[local-name(.)='dd']" />/<xsl:value-of select="*[local-name(.)='releaseDate']/*[local-name(.)='mm']" />/<xsl:value-of select="*[local-name(.)='releaseDate']/*[local-name(.)='yyyy']" /></fo:block> 
        <fo:block font-weight="bold" text-align="center"> 
         Available on <xsl:value-of select="*[local-name(.)='platforms']/*[local-name(.)='platform[1]']" /><xsl:for-each select="*[local-name(.)='platforms']/*[local-name(.)='platform']"> 
         <xsl:if test="position() != last() and position() != 1">, <xsl:value-of select="." /></xsl:if></xsl:for-each> and <xsl:value-of select="*[local-name(.)='platforms']/*[local-name(.)='platform[last()]']" /> 
        </fo:block> 
        <fo:block text-align="center" background-color="#9900FF" margin-top="0.3in" margin-left="1in" margin-right="1in"><xsl:value-of select="*[local-name(.)='esrbRating']" /></fo:block> 
       </fo:flow> 
      </fo:page-sequence> 
     </xsl:for-each> 

    </fo:root> 
</xsl:template> 
</xsl:stylesheet> 
+0

它沒有工作。 – wizzkid

+0

@wizzkid:請查找更新的XSLT進行轉換 – siva2012

+0

謝謝!它非常完美! – wizzkid

0

好像你正在尋找兩全元素ubisoft:game/ubisoft:titleatari:game/atari:title。你有兩個選項是:

  1. 使用*:game/*:title
  2. select="ubisoft:game/ubisoft:title | atari:game/atari:title"
+0

它沒有工作。 – wizzkid

+0

首先聲明您的XML名稱空間到您的XSLT中,如聲明siva2012 。在那之後用'// ncb:game'改變'../upcoming/ncb:game'。但它只需要'ncb:game'元素。如果你想同時採用'atari:game'和'ncb:game',而不是像名字空間那樣使用星號(*),就像我在上面的第一點中已經給出的那樣。 –