what can I do if I have this:

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

and I want to translate with XSLT in:

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

I've tried with:

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

But i receive a mistake as value-of should be empty.

有帮助吗?

解决方案

xsl:value-of must be empty, no doubt. But you don't need anything inside it. The stylesheet below is an identity transform with two exceptions, namely replacing the element names of e1 and e2.

It is quite generic in the sense that it replaces e1 and e2 in any XML document and leaves all the rest untouched.

Stylesheet

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

Output

<?xml version="1.0" encoding="UTF-8"?>
<c1>t1<c2>t2</c2></c1>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top