开发者

string-join for XSLT1

开发者 https://www.devze.com 2023-03-04 02:52 出处:网络
I am a newbie in XSLT and I need String-join function in XLT1. I know that there is not such function there b开发者_JS百科ut I have to stick to XLST1.

I am a newbie in XSLT and I need String-join function in XLT1. I know that there is not such function there b开发者_JS百科ut I have to stick to XLST1.

I have a xml file that has a stream like this:


<?xml version="1.0" encoding="UTF-8" ?> 


<CgPoints>
  <CgPoint name="A">315.4 58.1 0</CgPoint> 
  <CgPoint name="B">315.4 58.2 0</CgPoint> 
  <CgPoint name="C">315.9 58.2 0</CgPoint> 
  <CgPoint name="D">315.9 58.1 0</CgPoint> 
  <CgPoint name="E">315.4 58.1 6</CgPoint> 
  <CgPoint name="F">315.4 58.2 6</CgPoint> 
</CgPoints>

I need a string-join function in xslt1 to create an output like this:


<?xml version="1.0" encoding="UTF-8"?>

    <Placemark>
        <Point>
            <coordinates>315.4,58.1,0 
                         315.4,58.2,0
                      315.9,58.2,0
                         315.9,58.1,0
                       315.4,58.1,6
                         315.4,58.2,6
            </coordinates>
        </Point>
    </Placemark>
</kml>

Could you please write a XSLT1 code that I can add it in Mapforce Altova as a library. Thanking you in advance for your help.


I think we can have a recursive template like below:

<xsl:template match="/">
    <Placemark>
        <Point>
            <coordinates>
                <xsl:apply-templates select="CgPoints/CgPoint"/>
            </coordinates>
        </Point>
    </Placemark>
</xsl:template>

<xsl:template match="CgPoint">
    <xsl:call-template name="replaceSpaceWithComma">
        <xsl:with-param name="s" select="."/>
    </xsl:call-template>
</xsl:template>

<xsl:template name="replaceSpaceWithComma">
    <xsl:param name="s" />
    <xsl:choose>
        <xsl:when test="string-length( substring-after( $s, ' ') )">
            <xsl:call-template name="replaceSpaceWithComma">
                <xsl:with-param name="s" select="concat(substring-before($s, ' '), ',',substring-after($s , ' ') )" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$s"/>
        </xsl:otherwise>
    </xsl:choose>


</xsl:template>
0

精彩评论

暂无评论...
验证码 换一张
取 消