使用XSL

时间:2018-06-12 21:39:39

标签: xml xslt xpath

首先,我的问题很像this one,但有一些变化,我的当前输出与我在该链接问题中看到的相反。以下是我的问题的详细信息:

目标是从包含图像而不包含其他内容的表周围删除所有table标记。我不能在桌子里面放这些图像。

以下是我尝试转换的输入XML:

    <table  frame="all">
        <tgroup cols="1">
            <colspec colname="col1" colwidth="480pt"/>
            <thead>
                <row>
                    <entry valign="middle">
                        <p>
                            <image href="Images/image33.png" width="770.0" height="122.0">
                                <alt>../../topics/media/image33.wmf</alt>
                            </image><i >italic image title</i>
                        </p>
                    </entry>
                </row>
            </thead>
            <tbody>
                <row>
                    <entry/>
                </row>
            </tbody>
        </tgroup>
    </table>

以下是我要找的输出:

<fig>
    <title>image title</title>
    <image href="Images/image33.png" width="770.0" height="122.0"></image>
</fig>

我可以使用此模板添加<fig>代码:

<xsl:template match="table//image">
    <xsl:element name="fig">
        <xsl:element name="image">
            <xsl:apply-templates select="*|@*"/>
        </xsl:element>
    </xsl:element>
</xsl:template>

但是当我添加这个模板时(上面链接问题回答):

<xsl:template match="table//image">
    <xsl:copy-of select="/tgroup/thead/row/entry/node()" />
</xsl:template>

我收到了这个输出:

<table frame="all" class="- topic/table ">
                <tgroup id="topic_61_tg_2" cols="1" class="- topic/tgroup ">
                    <colspec colname="col1" colwidth="480pt" class="- topic/colspec "/>
                    <thead class="- topic/thead ">
                        <row class="- topic/row ">
                            <entry valign="middle" class="- topic/entry ">
                                <i>italic text title</i>
                            </entry>
                        </row>
                    </thead>
                    <tbody class="- topic/tbody ">
                        <row class="- topic/row ">
                            <entry class="- topic/entry "/>
                        </row>
                    </tbody>
                </tgroup>
            </table>

如您所见,表格而非图像被保留。知道我在这里做错了什么吗?我还要注意,此XML文件中还有很多其他内容我想要保留,这就是我为<xsl:template match="table//image">部分{/ 1}尝试copy-of的原因>

3 个答案:

答案 0 :(得分:1)

你可以试试这个:

<xsl:output indent="yes"/>
<xsl:template match="table">
    <xsl:for-each select="descendant::image">
        <xsl:element name="fig">
            <xsl:element name="title">
                <xsl:value-of select="following-sibling::i"/>
            </xsl:element>
            <xsl:copy-of select="."/>
        </xsl:element>
    </xsl:for-each>
</xsl:template>

答案 1 :(得分:0)

<xsl:template match="table">
        <xsl:for-each select="//image">
            <xsl:element name="fig">
                <xsl:element name="title">
                    <xsl:value-of select="following-sibling::i"/>
                </xsl:element>
                <xsl:element name="image">
                    <xsl:attribute name="href">
                        <xsl:value-of select="@href"/>
                    </xsl:attribute>
                    <xsl:attribute name="width">
                        <xsl:value-of select="@width"/>
                    </xsl:attribute>
                    <xsl:attribute name="height">
                        <xsl:value-of select="@height"/>
                    </xsl:attribute>
                </xsl:element>
            </xsl:element>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="image">

    </xsl:template>
    Try it

答案 2 :(得分:0)

你应该使用

<xsl:template match="table"><!-- Match table instead of matching image -->
    <xsl:copy-of select="tgroup/thead/row/entry//image" /><!-- than copy image through its XPath -->
</xsl:template>

在您的示例中,您匹配image的模板,并提到xpath的{​​{1}}。因此没有用。