需要在属性模板中包​​含元素

时间:2020-08-04 10:49:47

标签: xml xslt-3.0

嗨,我想将元素包括在基于属性的模板中,我正在使用XSL,该XSL用于将旧属性值更改为新值。

输入HTML:

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

我拥有的XSL:

<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>

预期输出:

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

需要在段落中包含normal元素。

1 个答案:

答案 0 :(得分:1)

如果任务是将输入中任何p元素的内容包装到normal元素中,则只需添加模板即可

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