从xml文档中获取不同的命名空间

时间:2013-05-16 11:49:38

标签: xml xslt xpath namespaces xsl-fo

我想知道如何从我的xml文档中获取不同的命名空间,以用于转换为xsl-fo。我所拥有的是以下内容:

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>

我想要的是xslt文件的代码,该文件将获取该信息并将其转换为xsl-fo文档。基本上我所讨论的是如何引用节点。到目前为止我所拥有的:

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

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

您似乎希望获得元素ubisoft:game/ubisoft:titleatari:game/atari:title。你有两个选择:

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