使用XSLT重命名XML元素

2 xml xslt

我需要更改原始XML中的一些元素名称.我试图用XSLT做到这一点,但无法让它工作.

这是一个XML示例:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="test.xsl" type="text/xsl"?>
<html>
<body>
    <section>Jabber</section>       
            <itemtitle>JabberJabber</itemtitle>
                    <p>Always Jabber Jabber Jabber</p>
            <h3>Emboldened Requests </h3>
                    <p>Somemore Jabber Here</p>
                    <img scr="bigpicture.jpg"></img>
            <poll><p>Which statement best characterizes you?</p></poll>
            <pcredit>Left: Jumpin Jasper/Jumpy Images</pcredit>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我需要将其更改为:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="test.xsl" type="text/xsl"?>
<html>
<body>
   <div class="issuehead">Jabber</div>   
   <div class="issuetitle">JabberJabber</div>
      <p>Always Jabber Jabber Jabber</p>
   <h3>Emboldened Requests </h3>
      <p>Somemore Jabber Here</p>
   <img scr="bigpicture.jpg"></img>
   <div class="poll"><p>Which statement best characterizes you?</p></div>
   <div class="pcredit">Left: Jumpin Jasper/Jumpy Images</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

这是我做的XSLT,但我无法让它工作:

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

<xsl:output method="html" />

<xsl:template match="/">

<html>
<head>  
</head>
<body>
   <xsl:apply-templates/>
</body>    
</html>

  <xsl:template match="section">
    <div class="issuehead"><xsl:value-of select="."/></div>
  </xsl:template>

  <xsl:template match="itemtitle">
    <div class="issuetitle"> <xsl:value-of select="."/></div>
  </xsl:template>

  <xsl:template match="img"></xsl:template>

  <xsl:template match="poll">
    <div class="poll"><xsl:value-of select="."/></div>
  </xsl:template>

  <xsl:template match="pcredit">
    <div class="pcredit"><xsl:value-of select="."/></div>
  </xsl:template>

  <xsl:template match="p"></xsl:template>

  <xsl:template match="h3"></xsl:template>

 </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助!

Joh*_*ers 10

对于这样的事情,从身份变换开始:

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

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

</xsl:stylesheet>  
Run Code Online (Sandbox Code Playgroud)

这将只复制每个节点.但是,您可以添加其他模板来执行您需要的操作:

<xsl:template match="section"> 
  <div class="issuehead">
      <xsl:apply-templates select="@* | node( )"/>
  </div>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

这种模式应该可以满足您的需求.