【问题标题】:how to add img tag when any attribute is present in xml in xslt?当xslt的xml中存在任何属性时如何添加img标签?
【发布时间】:2017-01-27 10:01:36
【问题描述】:

data-type='image' 存在时,我想添加img 标签。我们可以这样做是xslt 吗?

这是我的代码 (http://xsltransform.net/pPJ8LVQ/3):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
   <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
   <xsl:template match="/">
      <hmtl>
         <head>
            <title>New Version!</title>
         </head>
         <xsl:for-each select=".//a/cd">
            <li>
               <strong>
                  <xsl:value-of select="title" />
               </strong>
               <div>
                  <xsl:copy-of select="body/node()" />
               </div>
            </li>
         </xsl:for-each>
      </hmtl>
   </xsl:template>
   <xsl:template match="div[@data-type = 'image']">
      <img src="@src" />
   </xsl:template>
</xsl:transform>

输入

<a>
    <cd>
           <title>dddd</title>

        <body>
            <div col="1">d<p>ddd</p></div>
        </body>
    </cd>
     <cd>
       <title>ccc</title>
        <body>
            <div col="1">iii<p>ddd</p>

                <div data-type="image" src="abc.png" id='234'></div>
            </div>
        </body>
    </cd>
</a>

预期输出

<li><strong>dddd</strong><div>

    <div col="1">d
        <p>ddd</p>
    </div>

</div>
</li>
<li><strong>ccc</strong><div>

    <div col="1">iii
        <p>ddd</p>

         <div id="234">
        <img  src="abc.png"/>
         </div>

    </div>

</div>
</li>

【问题讨论】:

    标签: xml xslt xpath xslt-1.0


    【解决方案1】:

    您需要了解apply-templates 和模板匹配,然后才能转换元素:

    <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
       <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
       <xsl:template match="/">
          <hmtl>
             <head>
                <title>New Version!</title>
             </head>
             <xsl:for-each select=".//a/cd">
                <li>
                   <strong>
                      <xsl:value-of select="title" />
                   </strong>
                   <div>
                      <xsl:apply-templates select="body/node()" />
                   </div>
                </li>
             </xsl:for-each>
          </hmtl>
       </xsl:template>
    
       <xsl:template match="@* | node()">
           <xsl:copy>
               <xsl:apply-templates select="@* | node()"/>
           </xsl:copy>
       </xsl:template>
    
       <xsl:template match="div[@data-type = 'image']">
         <xsl:copy>
             <xsl:apply-templates select="@* except (@data-type, @src)"/>
             <img src="{@src}"/>
             <xsl:apply-templates/>
         </xsl:copy>
       </xsl:template>
    </xsl:transform>
    

    【讨论】:

    • 你使用的是 xslt 2...我使用的是 xslt 1.0
    • @user5711656 那你必须告诉我们。不幸的是,您使用xslt-1.0xslt-2.0 标记了问题,并且您链接到的代码也声明了XSLT 2.0。解决问题的一种方法是select="@*[not(name() = 'data-type' or name() = 'src')]"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    相关资源
    最近更新 更多