0

以下のような入力HTMLを、一連の兄弟要素をグルーピングする目的でXSLT1.0で処理したいと思っています。

<html>
  <head>
    <meta charset="utf-8" />
  </head>
  <body>
    <div>
      <p>A</p>
      <div class="case">0</div>
      <div class="case">1</div>
      <div class="case">2</div>
      <p>B</p>
      <p>C</p>
      <div class="case">3</div>
      <div class="case">4</div>
      <p>D</p>
      <p>E</p>
      <div class="case">5</div>
      <div class="case">6</div>
    </div>
  </body>
</html>

期待する出力は以下のようなものです。

<?xml version="1.0"?>
<html>
  <head>
    <meta charset="utf-8"/>
  </head>
  <body>
    <div>
      <p>A</p>
      <box>
        <p>0</p>
        <p>1</p>
        <p>2</p>
      </box>
      <p>B</p>
      <p>C</p>
      <box>
        <p>3</p>
        <p>4</p>
      </box>
      <p>D</p>
      <p>E</p>
      <box>
        <p>5</p>
        <p>6</p>
      </box>
    </div>
  </body>
</html>

以下のXSLを書いてみましたが、一番下の<xsl:template match="div[contains(@class, 'case')]" />が効いてしまい、boxの中身が空になってしまいます。

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

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

  <xsl:template match="div[contains(@class, 'case')][not(preceding-sibling::*[1][contains(@class, 'case')])]">
    <box>
      <xsl:apply-templates select="." mode="inbox" />
      <xsl:apply-templates select="following-sibling::div[contains(@class, 'case')][preceding-sibling::*[not(self::div[contains(@class, 'case')])][1]/following-sibling::div[contains(@class, 'case')][1] = current()]" mode="inbox" />
    </box>
  </xsl:template>
  <xsl:template match="div[contains(@class, 'case')]" mode="inbox">
    <p><xsl:apply-templates select="node()" /></p>
  </xsl:template>
  <xsl:template match="div[contains(@class, 'case')]" />
</xsl:stylesheet>

よろしければ、アドバイスお願いします。

2 件の回答 2

1
match="div[contains(@class, 'case')][not(preceding-sibling::*[1][contains(@class, 'case')])]"

match="div[contains(@class, 'case')]"

とでは同じpriority 0.5なのでconflictし、その中でより後ろのほうにあるテンプレートが選ばれます。

なお、こうしたconflictについては、XSLTプロセッサが

Ambiguous rule match for /html/body[1]/div[1]/div[1] Matches both "div[fn:contains(...)]" on line 19 ... and "div[fn:contains(...)][fn:empty(...)]" on line 10 ...

のようなwarningを出してくれて、それによって気付けることが多いと思います。

conflictの解決法としては、優先したいテンプレートにより高い@priorityをつける、たとえば

<xsl:template match="div[contains(@class, 'case')][not(preceding-sibling::*[1][contains(@class, 'case')])]" priority="1">

とする、という方法がまず考えられます。しかし、@priorityを頻繁に使うと優先度地獄になりがちなので、あまりおすすめしません。

それより、2つのテンプレートをまとめて

<xsl:template match="div[contains(@class, 'case')]">
  <xsl:if test="not(preceding-sibling::*[1][contains(@class, 'case')])">
    <box>
      <xsl:apply-templates select="." mode="inbox" />
      <xsl:apply-templates select="following-sibling::div[contains(@class, 'case')][preceding-sibling::*[not(self::div[contains(@class, 'case')])][1]/following-sibling::div[contains(@class, 'case')][1] = current()]" mode="inbox" />
    </box>
  </xsl:if>
</xsl:template>

という1つのテンプレートにするほうが見通しがよいのではないかと思います。

1
  • ありがとうございます!なぜ上手くいかなかったのかを含めてよく理解できました。1つのテンプレートにまとめる方針で進めてみます
    – msnrhnd
    5月6日 14:42
0

xsl:for-eachp 要素を順次処理していくなど。

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

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

  <xsl:template match="div">
    <div>
    <xsl:for-each select="p">
      <xsl:copy-of select="."/>
      <xsl:variable name="box" select="following-sibling::div[@class='case'][count(preceding-sibling::p[1]|current())=1]"/>
      <xsl:if test="count($box) > 0">
        <box>
          <xsl:apply-templates select="$box"/>
        </box>
      </xsl:if>
    </xsl:for-each>
    </div>
  </xsl:template>

  <xsl:template match="div[@class='case']">
    <p><xsl:value-of select="."/></p>
  </xsl:template>

</xsl:stylesheet>
1
  • ありがとうございます。上記でうまく処理できました!
    – msnrhnd
    5月6日 0:31

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。