XSLT将单行转换为分层xml

时间:2018-11-21 07:25:26

标签: xslt

您能帮我创建XSLT 1.0来转换此xml吗?

<s bold="true" italic="true" color="#FFF000">bold italic and colored text</s>
<s bold="true">bold text</s>
<s italic="true" bold="true">bold italic text</s>

此html

<p><b><i><span style="color:#FFF000">bold italic and colored text</span></i></b></p>
<p><b>bold text</b></p>
<p><b><i>bold italic text</i></b></p>

谢谢

1 个答案:

答案 0 :(得分:1)

这里是XSLT 3解决方案,它使用单独的模式通过模板将每个属性推入以递归的方式实现嵌套:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:output method="html" html-version="5"/>

  <xsl:template match="s">
      <p>
          <xsl:apply-templates select="." mode="att"/>
      </p>
  </xsl:template>

  <xsl:template match="s" mode="att">
      <xsl:param name="attributes" select="@*"/>
      <xsl:apply-templates select="if (head($attributes)) then head($attributes) else node()" mode="att">
          <xsl:with-param name="attributes" select="$attributes"/>
      </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="@bold[. = 'true']" mode="att">
      <xsl:param name="attributes"/>
      <b>
          <xsl:apply-templates select=".." mode="att">
              <xsl:with-param name="attributes" select="tail($attributes)"/>
          </xsl:apply-templates>
      </b>
  </xsl:template>

  <xsl:template match="@italic[. = 'true']" mode="att">
      <xsl:param name="attributes"/>
      <i>
          <xsl:apply-templates select=".." mode="att">
              <xsl:with-param name="attributes" select="tail($attributes)"/>
          </xsl:apply-templates>
      </i>
  </xsl:template> 

  <xsl:template match="@color" mode="att">
      <xsl:param name="attributes"/>
      <span style="color: {.};">
          <xsl:apply-templates select=".." mode="att">
              <xsl:with-param name="attributes" select="tail($attributes)"/>
          </xsl:apply-templates>          
      </span>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/3NzcBuc/0

它的XSLT 1音译应该是

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

  <xsl:template match="@* | node()">
      <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
      </xsl:copy>
  </xsl:template>

  <xsl:output method="html" indent="yes" version="5.0" doctype-system="about:legacy-doctype"/>

  <xsl:template match="s">
      <p>
          <xsl:apply-templates select="." mode="att"/>
      </p>
  </xsl:template>

  <xsl:template match="s" mode="att">
      <xsl:param name="attributes" select="@*"/>
      <xsl:choose>
          <xsl:when test="$attributes">
              <xsl:apply-templates select="$attributes[1]" mode="att">
                  <xsl:with-param name="attributes" select="$attributes"/>
              </xsl:apply-templates>              
          </xsl:when>
          <xsl:otherwise>
              <xsl:apply-templates/>
          </xsl:otherwise>
      </xsl:choose>
  </xsl:template>

  <xsl:template match="@bold[. = 'true']" mode="att">
      <xsl:param name="attributes"/>
      <b>
          <xsl:apply-templates select=".." mode="att">
              <xsl:with-param name="attributes" select="$attributes[position() > 1]"/>
          </xsl:apply-templates>
      </b>
  </xsl:template>

  <xsl:template match="@italic[. = 'true']" mode="att">
      <xsl:param name="attributes"/>
      <i>
          <xsl:apply-templates select=".." mode="att">
              <xsl:with-param name="attributes" select="$attributes[position() > 1]"/>
          </xsl:apply-templates>
      </i>
  </xsl:template> 

  <xsl:template match="@color" mode="att">
      <xsl:param name="attributes"/>
      <span style="color: {.};">
          <xsl:apply-templates select=".." mode="att">
              <xsl:with-param name="attributes" select="$attributes[position() > 1]"/>
          </xsl:apply-templates>          
      </span>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/3NzcBuc/1

相关问题