[英]XSL split a string based on delimiter BUT leave all other tags the same
I am NOT familiar with XSL and need some help from you experts. 我不熟悉XSL,需要专家的帮助。 Try to only convert
<RiskType>Risk1, Risk2, Risk3</RiskType>
尝试仅转换
<RiskType>Risk1, Risk2, Risk3</RiskType>
To <RiskType>Risk1</RiskType>
要
<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.
在其他文章的基础上,我尝试使用XSL,但似乎可以完成上述操作,但破坏了结构和其他标签。
<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不起作用
<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>
I'd suggest you try it this way: 我建议您这样尝试:
XSLT 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: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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.