0

我正在尝试在 XSL 中弄清楚如何结束元素 <bold> 它是元素的父级

编辑此外,元素 <paragraph> 和 <bold> 的内容获得 <style name ="bold">。不包括 <link> 元素的内容。<style> 将包裹在 <paragraph> 和 <bold> 的内容中。元素 <paragraph> 的内容也可以有一个或多个 <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 回答