0

如果我有这个,我该怎么办:

<e1>t1<e2>t2</e2></e1>

我想用 XSLT 翻译:

<c1>t1<c2>t2</c2></c1>

我试过:

<xsl:template match="e1">
  <c1>
     <xsl:value-of select=".">
        <xsl:apply-templates/>
     </xsl:value-of>
  </c1>
</xsl:template>
<xsl:template match="e2">
  <c2>
     <xsl:value-of select="."/>
  </c2>
</xsl:template>

但是我收到一个错误,因为 value-of 应该为空。

4

1 回答 1

2

xsl:value-of一定是空的,毫无疑问。但你不需要里面的任何东西。下面的样式表是一个身份转换,有两个例外,即替换 和 的元素e1名称e2

从某种意义上说,它是相当通用的,它取代e1e2任何 XML 文档,而其余部分保持不变。

样式表

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:output method="xml" indent="yes"/>

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

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

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

</xsl:stylesheet>

输出

<?xml version="1.0" encoding="UTF-8"?>
<c1>t1<c2>t2</c2></c1>
于 2014-02-05T08:13:02.607 回答