【问题标题】:XML to XML Transformation through XSL通过 XSL 实现 XML 到 XML 的转换
【发布时间】:2015-01-22 11:33:21
【问题描述】:

输入 XML 是

                                    <CELL col="F">
                                  <Para apprevbar="F">s281w0-602
                                   <a>AB</a>
                                     <b>CD <d> RT </d> 78</b>
                                      <c>EF</c>....etc
                                    </Para>
                                  </CELL>

我希望输出为

                                <CELL col="F">
                         <Para apprevbar="F">s281w0-602
                               AB
                               CD
                               RT
                               78
                               EF...etc
                              </Para>
                              </CELL>

您能否告诉我任何 xsl 想法来转换上述输入 xml 以获得上述输出。

【问题讨论】:

  • 请向我们展示您迄今为止尝试过的 XSLT。
  • w3.org/1999/XSL/Transform">
  • 您能否将其编辑到问题中,而不是将其作为评论发布 - 代码格式在 cmets 中不能很好地实现。
  • 现在我更新了这个问题。请你看看并告诉我。

标签: xml xslt


【解决方案1】:

使用身份转换模板

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

<xsl:template match="a">
  <xsl:apply-templates/>
</xsl:template>

根据您的 cmets 和您的编辑,您似乎想从 Para 元素中删除所有元素内容,以便您可以使用

<xsl:template match="Para//*">
  <xsl:apply-templates/>
</xsl:template>

继续沿用目前采用的方法,但您也可以使用

<xsl:template match="Para">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:value-of select="."/>
  </xsl:copy>
</xsl:template>

简单地将Para元素的字符串值作为新内容输出。

【讨论】:

  • 我发布的两个模板将复制a元素的任何内容。
  • 如果您有其他元素需要与a 元素进行相同的处理,请使用&lt;xsl:template match="a | b | c"&gt;&lt;xsl:apply-templates/&gt;&lt;/xsl:template&gt;
  • 但是 a ,b,c 标签不是恒定的。它可以随任何标签名称而变化。是否有任何通用匹配可以删除所有标签值并单独获取值?
  • 如果您的意思是“ 标签的任何子标签”,则使用“*[parent::Para]”作为匹配属性的 Xpath。
  • 如果你使用&lt;xsl:template match="Para/*"&gt;&lt;xsl:apply-templates/&gt;&lt;/xsl:template&gt;,那么Para元素的所有子元素都会得到同样的处理。
猜你喜欢
  • 1970-01-01
  • 2016-03-13
  • 2015-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-12
  • 1970-01-01
  • 2016-04-15
相关资源
最近更新 更多