2014-03-13 113 views
0

我有下面的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> 

问题: WIt小时以上的XSLT(XPath的)我得到以下输出:

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

的最内层嵌套的附图元件two.svg和three.svg在输出没有出现。但文字来自任何层面。我有一个dtd文件,它已经定义了在章节元素中可以有图元素的任何地方。但是元素的顺序是未知的。 所以我正在检查,如果它有一个数字元素,然后显示它。 我想要PDF格式的XSL FO输出。 在XPath表达式名称(./ following-sibling :: *)='figure'中还有更多需要的东西。我没有得到它。 请帮帮我。

其他解决方案的尝试:

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

但只有第一张图,出现abc.svg。 谢谢。

+1

如果你“想要XSL FO输出”,那么请显示实际的XSL-FO代码作为输出 - 任何文本近似都没有多大帮助。另外,您的XSLT样式表不完整。 –

回答

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()之后运行,除非你必须

+0

明白了:)谢谢:) – Chai