2

こんにちは、以下のような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 の書き方がわかりません。

4

2 に答える 2

4

これは、 XSLT で最も一般的なデザイン パターンの 1 つであるIdentity Transformの仕事です。まず、ノード (アトリビュートを含む) をコピーするだけのこのテンプレートを使用します。

<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>
于 2012-11-30T15:23:45.413 に答える
0
    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml"/>
    <xsl:template match="/shahid">
        <table>
            <xsl:copy-of select="@*"/>
            <xsl:for-each select="ali">
                <tr>
                    <xsl:copy-of select="@*"/>
                    <xsl:apply-templates select="khan"/>
                </tr>
            </xsl:for-each>
        </table>
    </xsl:template>
    <xsl:template match="khan">
        <td>
            <xsl:apply-templates select="@*|node()"/>
        </td>
    </xsl:template>

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

    <xsl:template match="font">
    <span>
    <xsl:copy-of select="@*"/>
    </span>
    </xsl:template>
</xsl:stylesheet>
于 2012-11-30T14:36:07.637 に答える