XSLT转换为XML

时间:2015-03-04 09:05:28

标签: xslt

我有以下格式的xml:

  <top>
        <topValue Value="1#1#5" />
        <topValue Value="2#2#10" />
        <topValue Value="1#1#3" />
        <topValue Value="2#2#30" /> 
   </top>

并且输出应该如下:

  <boo>
       <booEnrty>
            <v>5</v>
            <v>10</v>
        </booEnrty>

        <booEnrty>
            <v>3</v>
            <v>30</v>
        </booEnrty>
     </boo>

要转换的XSLT

<boo>
        <xsl:for-each select="top/topValue">        
            <xsl:if test="position() mod 2 = 0">
                <booEnrty> 
                    <v><xsl:value-of select="substring-after(substring-after(@Value,'#'),'#')"/></v>                   
                </booEnrty> 
          </xsl:if>
     </xsl:for-each> 
    </boo>

XSLT文档应该如何进行此转换? 有什么想法吗?

由于

2 个答案:

答案 0 :(得分:0)

也许某人有更好的方法,但下面的XSLT适合您的情况。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

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

<xsl:template match="top">
    <boo>
        <xsl:apply-templates select="topValue[position() mod 2 = 1]"/>
    </boo>
</xsl:template>

<xsl:template match="topValue[position() mod 2 = 1]">
    <booEntry>
        <v>
            <xsl:call-template name="substring-after-last">
                <xsl:with-param name="string" select="@Value" />
                <xsl:with-param name="delimiter" select="'#'" />
            </xsl:call-template>
        </v>
        <xsl:apply-templates select="following-sibling::*[1]"/>
    </booEntry>
</xsl:template>

<xsl:template match="topValue">
    <v>
        <xsl:call-template name="substring-after-last">
            <xsl:with-param name="string" select="@Value" />
            <xsl:with-param name="delimiter" select="'#'" />
        </xsl:call-template>
    </v>
</xsl:template>

<xsl:template name="substring-after-last">
    <xsl:param name="string" />
    <xsl:param name="delimiter" />
    <xsl:choose>
        <xsl:when test="contains($string, $delimiter)">
            <xsl:call-template name="substring-after-last">
                <xsl:with-param name="string"
                    select="substring-after($string, $delimiter)" />
                <xsl:with-param name="delimiter" select="$delimiter" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise><xsl:value-of select="$string"/></xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

答案 1 :(得分:0)

简短而简单的事情怎么样?

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/top">
    <boo>
        <xsl:for-each select="topValue[position() mod 2 = 1]">        
            <booEnrty> 
                <xsl:for-each select=". | following-sibling::topValue[1]">
                    <v>
                        <xsl:value-of select="substring-after(substring-after(@Value,'#'),'#')"/>
                    </v> 
                </xsl:for-each> 
            </booEnrty>
        </xsl:for-each> 
    </boo>
</xsl:template> 

</xsl:stylesheet>
相关问题