1

ドキュメントに不要なネストされた<bo>タグがいくつかあります。それらをxsltする最も簡単な方法は何ですか?

ソースの例:

<body>
    <bo>
        <bo>some text</bo>
        <bo>
            <bo>some other text</bo>
        </bo>
        <bo>more text</bo>
    </bo>
  
    <bo>
        <fig/>
    <bo/>
</body>

結果の例:

<body>
    <p>some text</p>
    <p>some other text</p>
    <p>more text</p>

    <p>
        <fig/>
    <p>
</body>

事前にサンクス!

4

2 に答える 2

3

構築するためのアプローチとして、次のことを行います。

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


<xsl:template match="bo[.//bo]">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="boo[not(boo)]">
  <p>
    <xsl:apply-templates/>
  </p>
</xsl:template>

これで十分でない場合は、使用できる入力バリアントと、それらをどのように変換するかをより詳細に説明する必要があります。

上記のテンプレートを使用した完全なスタイルシートは

<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>

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


<xsl:template match="bo[.//bo]">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="boo[not(boo)]">
  <p>
    <xsl:apply-templates/>
  </p>
</xsl:template>

</xsl:stylesheet>

と変換

<body>
    <bo>
            <bo>some text</bo>
            <bo>
                <bo>some other text</bo>
            </bo>
            <bo>more text</bo>
    </bo>
    <bo>
        <fig/>
    </bo>
</body>

の中へ

<body>
   <bo>some text</bo>
   <bo>some other text</bo>
   <bo>more text</bo>
   <bo>
      <fig/>
   </bo>
</body>
于 2013-07-09T09:56:06.707 に答える