xml - 更改 XML 中所有属性的标签名称

标签 xml xslt

您好,我有 xml 输出,如下所示

<?xml version="1.0" encoding="ISO-8859-2"?>
    <shahid id="hyxq68qa41" nazwa="RAPORT BIEZACY" opis="RAPORT BIEZACY">
      <ali pos="2">
        <khan id="tlpyad6dn2" pos="1:2" multiplier="" timePeriodEx="" inne=":"/>
        <khan id="bu13zh6dnc" pos="2:2" multiplier="" timePeriodEx="" inne=":" columnSpan="11">KOMISJA NADZORU FINANSOWEGO</khan>
        <khan id="wzj6a46dne" pos="13:2" multiplier="" timePeriodEx="" inne=":"/>
      </ali>
      <ali pos="4">
        <khan id="3lmobr88c2" pos="1:4" multiplier="" timePeriodEx="" inne=":"/>
        <khan id="am4kjn88c3" pos="2:4" multiplier="" timePeriodEx="" inne=":"><html>  <head></head>  <body><font face='Times New Roman'>   <p></p>  </font></body></html></khan>
        <khan id="1aij3588c4" pos="3:4" multiplier="" timePeriodEx="" inne=":"><font face='Times New Roman'></font></khan>
        <khan id="6enzqb88c7" pos="4:4" multiplier="" timePeriodEx="" inne=":" columnSpan="4">Raport biezacy nr</khan>
        <khan id="umdc9y88o3" pos="8:4" multiplier="" timePeriodEx="" inne=":">66</khan>
        <khan id="11uzg088c9" pos="9:4" multiplier="" timePeriodEx="" inne=":">/</khan>
        <khan id="ntkz6p88ca" pos="10:4" multiplier="" timePeriodEx="" inne=":">2012</khan>
        <khan id="80nzo689x3" pos="11:4" multiplier="" timePeriodEx="" inne=":"/>
        <khan id="purzp388cb" pos="12:4" multiplier="" timePeriodEx="" inne=":"><html> <head>    </head>  <body>  </body></html>
        </ali>
    </shahid>

我想使用 XSL 转换此 xml 输出

<table id="hyxq68qa41" nazwa="RAPORT BIEZACY" opis="RAPORT BIEZACY">
  <tr pos="2">
    <td id="tlpyad6dn2" pos="1:2" multiplier="" timePeriodEx="" inne=":"/>
    <td id="bu13zh6dnc" pos="2:2" multiplier="" timePeriodEx="" inne=":" columnSpan="11">KOMISJA NADZORU FINANSOWEGO</td>
    <td id="wzj6a46dne" pos="13:2" multiplier="" timePeriodEx="" inne=":"/>
  </tr>
  <tr pos="4">
    <td id="3lmobr88c2" pos="1:4" multiplier="" timePeriodEx="" inne=":"/>
    <td id="am4kjn88c3" pos="2:4" multiplier="" timePeriodEx="" inne=":"><html>  <head></head>  <body><span face='Times New Roman'>   <p></p>  </span></body></html></td>
    <td id="1aij3588c4" pos="3:4" multiplier="" timePeriodEx="" inne=":"><span face='Times New Roman'></span></td>
    <td id="6enzqb88c7" pos="4:4" multiplier="" timePeriodEx="" inne=":" columnSpan="4">Raport biezacy nr</td>
    <td id="umdc9y88o3" pos="8:4" multiplier="" timePeriodEx="" inne=":">66</td>
    <td id="11uzg088c9" pos="9:4" multiplier="" timePeriodEx="" inne=":">/</td>
    <td id="ntkz6p88ca" pos="10:4" multiplier="" timePeriodEx="" inne=":">2012</td>
    <td id="80nzo689x3" pos="11:4" multiplier="" timePeriodEx="" inne=":"/>
    <td id="purzp388cb" pos="12:4" multiplier="" timePeriodEx="" inne=":"><html> <head>    </head>  <body>  </body></html>
    </tr>
</table>
  • 标签“shahid”替换为表格标签(开始标签和结束标签)
  • 标签“ali”被替换为 tr 标签
  • 标签“khan”已替换为 td 标签
  • 标签“font”被替换为 span 标签
  • 标签的所有属性也应该复制

我不知道如何为此编写 XSl。

最佳答案

这是 Identity Transform 的工作这是 XSLT 中最常见的设计模式之一。首先使用此模板,它仅复制节点(包括属性)

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

您可以简单地扩展它以添加模板,以根据需要将元素转换为新名称。例如,要将 shahid 元素转换为 table 元素,请执行以下操作:

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

除了创建新元素之外,它还会添加所有现有属性,然后继续处理其子元素。

对于其他更改来说,这是一个简单的情况。这是完整的 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>

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

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

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

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

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

当应用于您的 XML 时,输出以下内容

<table id="hyxq68qa41" nazwa="RAPORT BIEZACY" opis="RAPORT BIEZACY">
   <tr pos="2">
      <td id="tlpyad6dn2" pos="1:2" multiplier="" timePeriodEx="" inne=":"/>
      <td id="bu13zh6dnc" pos="2:2" multiplier="" timePeriodEx="" inne=":" columnSpan="11">KOMISJA NADZORU FINANSOWEGO</td>
      <td id="wzj6a46dne" pos="13:2" multiplier="" timePeriodEx="" inne=":"/>
   </tr>
   <tr pos="4">
      <td id="3lmobr88c2" pos="1:4" multiplier="" timePeriodEx="" inne=":"/>
      <td id="am4kjn88c3" pos="2:4" multiplier="" timePeriodEx="" inne=":">
         <html>
            <head/>
            <body>
               <span face="Times New Roman">
                  <p/>
               </span>
            </body>
         </html>
      </td>
      <td id="1aij3588c4" pos="3:4" multiplier="" timePeriodEx="" inne=":">
         <span face="Times New Roman"/>
      </td>
      <td id="6enzqb88c7" pos="4:4" multiplier="" timePeriodEx="" inne=":" columnSpan="4">Raport biezacy nr</td>
      <td id="umdc9y88o3" pos="8:4" multiplier="" timePeriodEx="" inne=":">66</td>
      <td id="11uzg088c9" pos="9:4" multiplier="" timePeriodEx="" inne=":">/</td>
      <td id="ntkz6p88ca" pos="10:4" multiplier="" timePeriodEx="" inne=":">2012</td>
      <td id="80nzo689x3" pos="11:4" multiplier="" timePeriodEx="" inne=":"/>
      <td id="purzp388cb" pos="12:4" multiplier="" timePeriodEx="" inne=":">
         <html>
            <head/>
            <body/>
         </html>
      </td>
   </tr>
</table>

关于xml - 更改 XML 中所有属性的标签名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13647055/

相关文章:

vcl - C++ 生成器 XE2,TXMLDocument 'DTD is prohibited'

javascript - jQuery.ajax 给出 "TypeError: Cannot read property ' documentElement' of null"on server but not local

android - 禁用 EditText 的可编辑性和焦点(如 TextView)

xml - InstallShield XML 文件更改 - 安装时更改节点属性

html - 通过 xslt 和 xml 错误呈现的 xforms

java - 需要帮助从 xml 递归获取数据

java - JAVA 中的 XSD 验证错误

java - 如何从 Java 应用程序使用 XSLT 3.0?

xslt - XSL 模板匹配语法

java - XSL 导入在 Web 应用程序中导致 FileNotFoundException