0

要素の親である <bold> 要素を終了する方法を XSL で理解しようとしています。

EDITまた、要素 <paragraph> と <bold> のコンテンツは <style name ="bold"> を取得します。<link> 要素の内容を除く。<style> は <paragraph> と <bold> のコンテンツ内にラップされます。また、要素 <paragraph> のコンテンツには、1 つ以上の <bold> および <link> を含めることができます



入力 XML

<paragraph>
     This is some text that  has no style
 </paragraph> 

 <paragraph>
     This is some text that is  <bold>correct way</bold> <link>need    
     to be linked </link> to a document
 </paragraph>

 <paragraph>
     This is some text that is  <bold>incorrect <link>need
     to be linked </link> way </bold> to a document
 </paragraph>


出力 Xml

<paragraph>
     This is some text that  has no style
 </paragraph> 

 <paragraph>
     <style name="bold">This is some text that is  <bold>correct way</bold></style>
     <link>need to be linked </link>
     <style name="bold"> to a document</style>
 </paragraph>

 <paragraph>
     <style name="bold">This is some text that is  <bold>incorrect</bold></style> 
     <link>need to be linked </link>
     <style name="bold"><bold> way </bold> to a document</style>
 </paragraph>

どんな助けでも大歓迎です。

4

1 に答える 1

0

この変換:

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

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

 <xsl:template match="bold[link]"><xsl:apply-templates/></xsl:template>

 <xsl:template match="bold[link]/text()">
  <bold><xsl:value-of select="."/></bold>
 </xsl:template>
</xsl:stylesheet>

提供された XML ドキュメント(単一のトップ要素にラップされた提供されたフラグメント) に適用された場合:

<t>
 <paragraph>
     This is some text that is  <bold>correct way</bold> <link>need
     to be linked </link> to a document
 </paragraph>

 <paragraph>
     This is some text that is  <bold>incorrect <link>need
     to be linked </link> way </bold> to a document
 </paragraph>
</t>

必要な正しい結果が生成されます。

<t>
   <paragraph>
     This is some text that is  <bold>correct way</bold>
      <link>need
     to be linked </link> to a document
 </paragraph>
   <paragraph>
     This is some text that is  <bold>incorrect </bold>
      <link>need
     to be linked </link>
      <bold> way </bold> to a document
 </paragraph>
</t>

OP は質問を更新しました。これは、最初の + 新しい要件を実装するわずかに変更された変換です

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

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

 <xsl:template match="paragraph[bold]">
  <paragraph>
    <style name="bold"><xsl:apply-templates/></style>
  </paragraph>
 </xsl:template>

 <xsl:template match="bold[link]"><xsl:apply-templates/></xsl:template>

 <xsl:template match="bold[link]/text()">
  <bold><xsl:value-of select="."/></bold>
 </xsl:template>
</xsl:stylesheet>

この変換が次のドキュメントに適用された場合:

<t>
<paragraph>
     This is some text that  has no style
 </paragraph>

 <paragraph>
     This is some text that is  <bold>correct way</bold> <link>need
     to be linked </link> to a document
 </paragraph>

 <paragraph>
     This is some text that is  <bold>incorrect <link>need
     to be linked </link> way </bold> to a document
 </paragraph>
 </t>

必要な正しい結果が生成されます。

<t>
   <paragraph>
     This is some text that  has no style
 </paragraph>
   <paragraph>
      <style name="bold">
     This is some text that is  <bold>correct way</bold>
         <link>need
     to be linked </link> to a document
 </style>
   </paragraph>
   <paragraph>
      <style name="bold">
     This is some text that is  <bold>incorrect </bold>
         <link>need
     to be linked </link>
         <bold> way </bold> to a document
 </style>
   </paragraph>
</t>
于 2013-03-24T15:59:04.907 に答える