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

Xml 匹配任何级别上具有相同名称的节点,xml,xslt,Xml,Xslt,我有以下xml: <xml> <start> <p>Welcome</p> <figure> <graphic name="abc.svg"></graphic> </figure> <chapter> <p>Sample text</p> <figure>

我有以下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>
XSLT:

最里面的嵌套图形元素two.svg和three.svg不会出现在输出中。但文本来自任何层面。我有一个dtd文件,它定义了在chapter元素中,任何地方都可以有figure元素。但是元素的顺序是未知的。 所以我要检查它是否有一个figure元素,然后显示它。 我想要pdf格式的XSL FO输出。 XPath表达式名称(./following sibling::*)='figure'中还需要一些东西。我不明白。 请帮帮我

尝试过的其他解决方案:

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

但只有第一个图形abc.svg出现。
谢谢。

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

    <?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()之后,除非您必须

    如果您“想要XSL-FO输出”,那么请将实际的XSL-FO代码显示为输出-任何文本近似都没有多大帮助。此外,XSLT样式表不完整。
    <xsl:if test="current()[name() = 'figure']"></xsl:if>
    
        <?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>
    
        <?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>