1

xslt スタイルシートを使用して html と xml が混在するドキュメントを変換し、html 要素のみを抽出しています。

ソースファイル:

<?xml version="1.0" encoding="utf-8" ?>
<html >
  <head>
    <title>Simplified Example Form</title>
  </head>
  <body>
    <TLA:document xmlns:TLA="http://www.TLA.com">
      <TLA:contexts>
        <TLA:context id="id_1" value=""></TLA:context>
      </TLA:contexts>
      <table id="table_logo" style="display:inline">
        <tr>
          <td height="20" align="middle">Big Title Goes Here</td>
        </tr>
        <tr>
          <td align="center">
            <img src="logo.jpg" border="0"></img>
          </td>
        </tr>
      </table>
      <TLA:page>
        <TLA:question id="q_id_1">
          <table id="table_id_1">
            <tr>
              <td>Label text goes here</td>
              <td>
                <input id="input_id_1" type="text"></input>
              </td>
            </tr>
          </table>
        </TLA:question>
      </TLA:page>
      <!-- Repeat many times -->
    </TLA:document>
  </body>
</html>

スタイルシート:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:TLA="http://www.TLA.com" exclude-result-prefixes="TLA">
  <xsl:output method="html" indent="yes" version="4.0" />
  <xsl:strip-space elements="*" />

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

  <!-- This element-only identity template prevents the 
       TLA namespace declaration from being copied to the output -->
  <xsl:template match="*">
    <xsl:element name="{name()}">
      <xsl:apply-templates select="@* | node()" />
    </xsl:element>
  </xsl:template>

  <!-- Pass processing on to child elements of TLA elements -->
  <xsl:template match="TLA:*">
    <xsl:apply-templates select="*" />
  </xsl:template>
</xsl:stylesheet>

出力:

<html>
  <head>
    <META http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Simplified Example Form</title>
  </head>
  <body>
    <table id="table_logo" style="display:inline">
      <tr>
        <td height="20" align="middle">Big Title Goes Here</td>
      </tr>
      <tr>
        <td align="center"><img src="logo.jpg" border="0"></td>
      </tr>
    </table>
    <table id="table_id_1">
      <tr>
        <td>Label text goes here</td>
        <td><input id="input_id_1" type="text"></td>
      </tr>
    </table>
  </body>
</html>

ただし、me​​ta、img、input 要素が正しく閉じられていないという問題があります。xsl:output を html に、バージョンを 4.0 に設定したので、正しい html を出力するはずです。

最初の xsl:template/xsl:copy 命令を微妙に変更する必要があると思いますが、私の xslt スキルは非常に限られています。

タグを正しく閉じるには、どのような変更が必要ですか?

PS さまざまなツール/パーサーに違いがあるかどうかはわかりませんが、Visual Studio 2012 を使用してスタイルシートをデバッグしているので、変更の影響をすぐに確認できます。

4

2 に答える 2

1

、および要素を閉じる必要<meta>はありません — それは依然として有効な HTML です。<img><input>

それらを閉じたい場合は、出力方法として(私が知る限り、xmlXSLT2.0でも使用できます)を使用し、必要に応じてタグを自分で追加できます。例えば:xhtml<meta>

スタイルシート

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:TLA="http://www.TLA.com" exclude-result-prefixes="TLA">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
  <xsl:strip-space elements="*" />

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

  <xsl:template match="head">
    <xsl:copy>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- This element-only identity template prevents the 
       TLA namespace declaration from being copied to the output -->
  <xsl:template match="*">
    <xsl:element name="{name()}">
      <xsl:apply-templates select="@* | node()" />
    </xsl:element>
  </xsl:template>

  <!-- Pass processing on to child elements of TLA elements -->
  <xsl:template match="TLA:*">
    <xsl:apply-templates select="*" />
  </xsl:template>
</xsl:stylesheet>

出力

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Simplified Example Form</title>
  </head>
  <body>
    <table id="table_logo" style="display:inline">
      <tr>
        <td height="20" align="middle">Big Title Goes Here</td>
      </tr>
      <tr>
        <td align="center">
          <img src="logo.jpg" border="0"/>
        </td>
      </tr>
    </table>
    <table id="table_id_1">
      <tr>
        <td>Label text goes here</td>
        <td>
          <input id="input_id_1" type="text"/>
        </td>
      </tr>
    </table>
  </body>
</html>
于 2013-04-09T09:58:27.917 に答える