XSL split a string based on delimiter BUT leave al

2019-09-17 19:28发布

I am NOT familiar with XSL and need some help from you experts. Try to only convert <RiskType>Risk1, Risk2, Risk3</RiskType>

To <RiskType>Risk1</RiskType>

<RiskType>Risk2</RiskType>

<RiskType>Risk3</RiskType>

But I have to keep everything else intact. Based on other articles, I tried to use XSL, but it seems to do the above but destroy the structure and other tags.

<Policy>

<PolNumber>123456789</PolNumber>
<LineOfBusiness tc="1">Life</LineOfBusiness>
 <Life>
    <Coverage id="">
        <LifeCovTypeCode tc="123">Child Term Rider</LifeCovTypeCode>
        <NumChildren>2</NumChildren>
    </Coverage>
    <PlanName>MyPlan</PlanName>
 </Life>
  <ApplicationInfo>
    <OLifeEExtension>
        <RiskTypes>
            <RiskType>Risk1, Risk2, Risk3</RiskType>
        </RiskTypes>
    </OLifeEExtension>
   </ApplicationInfo>
</Policy>

To

<Policy>

<PolNumber>123456789</PolNumber>
<LineOfBusiness tc="1">Life</LineOfBusiness>
 <Life>
    <Coverage id="">
        <LifeCovTypeCode tc="123">Child Term Rider</LifeCovTypeCode>
        <NumChildren>2</NumChildren>
    </Coverage>
    <PlanName>MyPlan</PlanName>
 </Life>
  <ApplicationInfo>
    <OLifeEExtension>
        <RiskTypes>
            <RiskType>Risk1</RiskType>
            <RiskType>Risk2</RiskType>
            <RiskType>Risk3</RiskType>
        </RiskTypes>
    </OLifeEExtension>
   </ApplicationInfo>
</Policy>

XSL does not work

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

<!-- Split child nodes -->
<xsl:template match="*" mode="tokenize-children">
    <xsl:copy>
        <xsl:apply-templates select="@*" />
        <xsl:apply-templates select="*" mode="tokenize" />
    </xsl:copy>
</xsl:template>

<!-- Tokenize text node of child nodes -->
<xsl:template match="*/text()" name="tokenize" mode="tokenize">
    <xsl:param name="text" select="."/>
    <xsl:param name="separator" select="','"/>

    <xsl:variable name="item"   select="name(..)" />

    <xsl:choose>
        <xsl:when test="not(contains($text, $separator))">
            <xsl:element name="{$item}">
                <xsl:value-of select="normalize-space($text)"/>
            </xsl:element>
        </xsl:when>
        <xsl:otherwise>
            <xsl:element name="{$item}">
                <xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
            </xsl:element>
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="substring-after($text, $separator)"/>
            </xsl:call-template>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

标签: xml xslt
1条回答
别忘想泡老子
2楼-- · 2019-09-17 20:27

I'd suggest you try it this way:

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:strip-space elements="*"/>

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

<xsl:template match="RiskType">
    <xsl:call-template name="tokenize">
        <xsl:with-param name="text" select="."/>
    </xsl:call-template>
</xsl:template>

<xsl:template name="tokenize">
    <xsl:param name="text"/>
    <xsl:param name="delimiter" select="', '"/>
        <xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" />
        <xsl:if test="$token">
            <RiskType>
                <xsl:value-of select="$token"/>
            </RiskType>
        </xsl:if>
        <xsl:if test="contains($text, $delimiter)">
            <!-- recursive call -->
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
            </xsl:call-template>
        </xsl:if>
</xsl:template>

</xsl:stylesheet>

Or, if you prefer:

<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:strip-space elements="*"/>

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

<xsl:template match="RiskType" name="tokenize">
    <xsl:param name="text" select="."/>
    <xsl:param name="delimiter" select="', '"/>
        <xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" />
        <xsl:if test="$token">
            <RiskType>
                <xsl:value-of select="$token"/>
            </RiskType>
        </xsl:if>
        <xsl:if test="contains($text, $delimiter)">
            <!-- recursive call -->
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
            </xsl:call-template>
        </xsl:if>
</xsl:template>

</xsl:stylesheet>
查看更多
登录 后发表回答