【问题标题】:XSLT conditional apply-templatesXSLT 条件应用模板
【发布时间】:2015-07-07 07:41:45
【问题描述】:

设置: Apache Xalan 2.7.1

输入:

<?xml version="1.0" encoding="UTF-8"?>
<root>
        <p>
            <code>111</code>
            <attr>good</attr>
        </p>
        <p>
            <code>222</code>
            <attr>bad</attr>
        </p>

        <s>
            <ps>
                <p>
                    <code>111</code>
                </p>
            <ps>
        </s>
        <s>
            <ps>
                <p>
                    <code>222</code>
                </p>
                <p>
                    <code>333</code>
                </p>
            <ps>
        </s>
</root>

XSLT:

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


  <xsl:template match="/">  
        <xsl:apply-templates select="
                  root[./p/code = ./s/ps[1]/p/code and ./p/attr= 'bad']/s" />
  </xsl:template>



 <xsl:template match="s">
       <xsl:copy-of select="."/>
 </xsl:template>

</xsl:stylesheet>

所需选择:

我只想选择那些ss/ps 中的第一个pcode 指向p 元素和bad attr

注意:仅支持 xslt 1.0 解决方案

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    考虑使用键来查找p 元素

    <xsl:key name="attr" match="root/p" use="code" />
    

    然后你的xsl:apply-templates 简化为这个

    <xsl:apply-templates select="root/s[key('attr', ps/p[1]/code)/attr = 'bad']" />
    

    试试这个 XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
        <xsl:key name="attr" match="root/p" use="code" />
    
        <xsl:template match="/">  
            <xsl:apply-templates select="root/s[key('attr', ps/p[1]/code)/attr = 'bad']" />
        </xsl:template>
    
        <xsl:template match="s">
            <xsl:copy-of select="."/>
         </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 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
      相关资源
      最近更新 更多