Need to include the element inside the attribute template

ε祈祈猫儿з 提交于 2020-08-10 13:11:39

问题


Hi I want to include the element inside the attribute based template, I'm using the XSL which is using for changing the old attribute value to New value.

Input HTML:

<section>
   <p class="p heading">Heading</p>
   <p class="normal">Text</p>
</section>

XSL I'm having:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    xmlns:map="http://www.w3.org/2005/xpath-functions/map"
    xmlns:array="http://www.w3.org/2005/xpath-functions/array"
    exclude-result-prefixes="#all"
    version="3.0">
    
  <xsl:param name="class-map">
   <name>
      <old>heading</old>
      <new>Headings</new>
   </name>
   <name>
      <old>normal</old>
      <new>Actual</new>
   </name>
  </xsl:param>
  
  <xsl:key name="class-map" match="name/new" use="../old"/>
  
  <xsl:template match="p/@class[key('class-map', tokenize(.), $class-map)]">
      <xsl:attribute name="style">
      <xsl:attribute name="{name()}" select="key('class-map', tokenize(.) , $class-map)"/>
      </xsl:attribute>
      <normal>
         <xsl:value-of select="p"/>
      </normal>
  </xsl:template>
  
  <xsl:mode on-no-match="shallow-copy"/>

</xsl:stylesheet>

Expected Output:

<section>
<p style="Headings"><normal>Heading</normal></p>
<p style="Actual"><normal>Text</normal></p>
</section>

Need to include the normal element inside the para.


回答1:


If the task is to wrap the contents of any p element in the input into a normal element then you can simply add a template

<xsl:template match="p">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <normal>
      <xsl:apply-templates/>
    </normal>
  </xsl:copy>
</xsl:template>


来源:https://stackoverflow.com/questions/63245283/need-to-include-the-element-inside-the-attribute-template

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!