在任何级别匹配具有相同名称的节点

时间:2014-03-13 04:38:13

标签: xml xslt

我有以下xml:

<xml>
  <start>
    <p>Welcome</p>
    <figure>
      <graphic name="abc.svg"></graphic>      
    </figure>
    <chapter>
      <p>Sample text</p>
      <figure>
        <graphic name="one.svg"></graphic>       
      </figure>
      <chapter>
         <figure>
           <graphic name="two.svg"></graphic>       
         </figure>
         <p>Hello World</p>
      </chapter>
      <chapter>
          <p>Life is good</p>
         <figure>
           <graphic name="three.svg"></graphic>       
         </figure>            
      </chapter>
    </chapter>
  </start>
</xml>

预期输出:

 Welcome
abc.svg
Sample Text
one.svg
two.svg
Hello World
Life is good
three.svg

XSLT:

<?xml>
 <xsl:apply-templates select="$context[name() = 'CHAPTER']" mode="chapter" />

<xsl:template match="node()" mode="chapter">
    <xsl:for-each select="node()">
        <fo:block margin-left="1mm">                
            <xsl:if test="name(./following-sibling::*) = 'figure'">             
                 <xsl:apply-templates select="current()[name() = 'FIGURE']" mode="figure" 
                    /> 
            </xsl:if>

            <xsl:value-of select="current()" />
        </fo:block>
    </xsl:for-each>
</xsl:template>

   <xsl:template match="node()[name() = 'figure']" mode="figure">               
    <xsl:variable name="ImageName">
        <xsl:value-of
            select="current()/L-GRAPHIC/GRAPHIC/@FILENAME" />
    </xsl:variable>     
    <fo:block>
        <fo:external-graphic>
            <xsl:attribute name="src"> <xsl:value-of
                select="concat('../resources/', 
    $ImageName)" /> </xsl:attribute>
        </fo:external-graphic>
    </fo:block>     
</xsl:template>

问题: 使用上面的xslt(XPath)我得到以下输出:

Welcome
abc.svg
Sample Text
one.svg
Hello World
Life is good

最里面的嵌套图元素 two.svg和three.svg没有出现在输出中。但是文本来自任何层面。我有一个dtd文件,它定义了在chapter元素中可以有figure元素。但元素的顺序尚不清楚。 所以我正在检查它是否有一个数字元素,然后显示它。 我想要pdf格式的XSL FO输出。 XPath表达式名称中需要更多内容(./ following-sibling :: *)=&#39; figure&#39;。我没有得到它。 请帮帮我。

尝试了其他解决方案:

<xsl:if test="current()[name() = 'figure']"></xsl:if>

但只有第一个数字,abc.svg出现。 谢谢。

1 个答案:

答案 0 :(得分:0)

您需要遵循的一般模式是:

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

        <xsl:template match="/">
            <xsl:apply-templates select="//figure" />
        </xsl:template>

        <xsl:template match="figure">
            <xsl:value-of select="preceding-sibling::p"/>
            <xsl:value-of select="graphic/@name"/>
            <xsl:value-of select="following-sibling::p"/>
        </xsl:template>

    </xsl:stylesheet>

如果你需要“装饰”段落(我使用HTML列表,但你可以在那里撒上你的FO),那么你需要条件格式化,使用它:

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

        <xsl:template match="/">
            <ol>
                <xsl:apply-templates select="//figure" />
            </ol>
        </xsl:template>

        <xsl:template match="figure">
            <xsl:if test="preceding-sibling::p">
                <li>
                    <xsl:value-of select="preceding-sibling::p" />
                </li>
            </xsl:if>
            <li>
                <xsl:value-of select="graphic/@name" />
            </li>
            <xsl:if test="following-sibling::p">
                <li>
                    <xsl:value-of select="following-sibling::p" />
                </li>
            </xsl:if>
        </xsl:template>

    </xsl:stylesheet>

希望有所帮助。当你命名时,XSLT最有效,除非你必须

,否则不要继续使用node()
相关问题