2015-02-23 58 views
0

嗨我想爲以下示例編寫xslt 2.0解決方案。XSLT2.0:如果父項相同,則將一個標記中的所有節點都取出的解決方案

我想要把所有的子元素的一個字符內,直到該節點是其它字符本身

輸入XML

<para> 
<character> 
<formatting>format</formatting> 
</character> 
<character> 
<formatting>format1</formatting 
<formatting>format2</formatting 
</character> 
this is a text node 
<character> 
<formatting>format3</formatting> 
</character> 
<character> 
<formatting>format7</formatting> 
<formatting>format8</formatting> 
</character> 
</para> 

預計輸出

<para> 
<character> 

    <formatting>format</formatting> 
    <formatting>format1</formatting> 
    <formatting>format2</formatting> 

</character> 
this is a text node 
<character> 
<formatting>format3</formatting> 
<formatting>format7</formatting 
<formatting>format8</formatting 
</character> 
</para> 

回答

1

您應該能夠使用xsl:for-each-group和組名相鄰。

實施例...

XML輸入

<para> 
    <character> 
     <formatting>format</formatting> 
    </character> 
    <character> 
     <formatting>format1</formatting> 
     <formatting>format2</formatting> 
    </character> 
    this is a text node 
    <character> 
     <formatting>format3</formatting> 
    </character> 
    <character> 
     <formatting>format7</formatting> 
     <formatting>format8</formatting> 
    </character> 
</para> 

XSLT 2.0

<xsl:stylesheet version="2.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="para"> 
     <xsl:copy> 
      <xsl:for-each-group select="node()" group-adjacent="name()"> 
       <xsl:choose> 
        <xsl:when test="current-group()[1][self::character]"> 
         <character> 
          <xsl:apply-templates select="current-group()/node()"/>        
         </character> 
        </xsl:when> 
        <xsl:otherwise> 
         <xsl:apply-templates select="current-group()"/> 
        </xsl:otherwise> 
       </xsl:choose> 
      </xsl:for-each-group>    
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

XML輸出

<para> 
    <character> 
     <formatting>format</formatting> 
     <formatting>format1</formatting> 
     <formatting>format2</formatting> 
    </character> 
    this is a text node 
    <character> 
     <formatting>format3</formatting> 
     <formatting>format7</formatting> 
     <formatting>format8</formatting> 
    </character> 
</para> 
+0

非常感謝..解決方案正在工作 – Khushi 2015-02-24 13:04:37

相關問題