【问题标题】:XSLT apply templates issueXSLT 应用模板问题
【发布时间】:2018-03-09 14:19:59
【问题描述】:

如果我们说为特定节点应用模板,它会在找到该节点/标记的任何位置应用到整个 xml。

我的问题是我有一个 xml,我需要根据条件应用模板,基本上保持一些秩序。

输入xml:

enter code here
<step>
 <note>..</note>
 <para>..</para>
 <table>
  ..
  ..
 </table>
 <note>...</note>
 <table>
 ..
 ..
 </table>
 <table>
 ..
 ..
</table>
<note>...</note>
<text>...</text>
</step>

输出应该是,

<fc:topic>
 <fc:subTask id="S0EC0A941" lbl="E.">
 <fc:para>..</fc:para>
 <fc:text>...</fc:text>
 <fc:note>..</fc:note>
 <fc:table>
  ..
  ..
  </fc:table>
  <fc:note>...</fc:note>
  <fc:table>
   ..
   ..
  </fc:table>
  <fc:table>
   ..
   ..
  </fc:table>
 <fc:note>...</fc:note>
 </fc:subTask>
</fc:topic>

我的 XSLT 是这样的,

<xsl:template match="step">
<fc:topic>
    <fc:subTask>
        <xsl:if test="@id">
            <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
        </xsl:if>
        <xsl:attribute name="lbl"><xsl:number format="A."/></xsl:attribute>         
        <xsl:apply-templates select="para"/>
        <xsl:apply-templates select="text"/>
        <xsl:apply-templates select="note"/>
        <xsl:apply-templates select="table"/>               
    </fc:subTask>
</fc:topic>

需要根据以下条件重构XSL,

只有当注释不是作为 table 的前面或后面的兄弟姐妹时,我才想应用注释模板。如果它的表的后续或前面的兄弟,那么它应该在表之间,在表的下方或上方,就像在输出中一样。

谢谢

【问题讨论】:

    标签: xml xslt xslt-1.0 xslt-2.0


    【解决方案1】:

    替换

        <xsl:apply-templates select="para"/>
        <xsl:apply-templates select="text"/>
        <xsl:apply-templates select="note"/>
        <xsl:apply-templates select="table"/>  
    

    <xsl:apply-templates select="para, text, note[not(preceding-sibling::*[1][self::table] | following-sibling::*[1][self::table])]"/>
    <xsl:apply-templates select="table | note[preceding-sibling::*[1][self::table] or following-sibling::*[1][self::table]]"/> 
    

    【讨论】:

    • 非常感谢马丁
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多