繁体   English   中英

使用XSLT重命名XML元素

[英]Rename XML elements using 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>

我需要将其更改为:

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

这是我做的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>

谢谢您的帮助!

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

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

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

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

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

以下模板可实现您正在寻找的结果:

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

    <xsl:output method="html" />

    <!-- Copy all elements and attributes from the source to the target -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>

    <!-- Transform the given elements to divs with class names matching the source element names -->
    <xsl:template match="itemtitle|poll|pcredit">
        <div class="{local-name(.)}">
          <xsl:value-of select="."/>
        </div>
    </xsl:template>

    <!-- Transform the section element to a div of class 'issuehead' -->
    <xsl:template match="section">
        <div class="issuehead">
            <xsl:value-of select="."/>
        </div>
    </xsl:template>

</xsl:stylesheet> 

如果您想要更好的标记,请修改xsl:output标记,如下所示:

<xsl:output method="xml" 
    version="1.0" 
    encoding="UTF-8" 
    indent="yes" 
    omit-xml-declaration="yes" 
    doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" 
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM