0
我想的XSLT代码下面输入文件仅某些元素:XSLT:分割基于条件
<?xml version="1.0" encoding="UTF-8"?>
<result>
<records>
<dataProcessed>
<FieldName>Tesco</FieldName>
<Mode>As Is</Mode>
</dataProcessed>
<dataProcessed>
<FieldName>ASDA|Tesco|Walmart</FieldName>
<Mode>Split</Mode>
</dataProcessed>
</records>
<records>
<dataProcessed>
<FieldName>Orange|MTS</FieldName>
<Mode>Split</Mode>
</dataProcessed>
</records>
</result>
当场模式的值“分割”,然后我需要使用管分割段分界符,我需要根据分割将字段Mode的值更改为1,2等。 所需的输出是:
<?xml version="1.0" encoding="UTF-8"?>
<result>
<records>
<dataProcessed>
<FieldName>Tesco</FieldName>
<Mode>As Is</Mode>
</dataProcessed>
<dataProcessed>
<FieldName>ASDA</FieldName>
<Mode>1</Mode>
</dataProcessed>
<dataProcessed>
<FieldName>Tesco</FieldName>
<Mode>2</Mode>
</dataProcessed>
<dataProcessed>
<FieldName>Walmart</FieldName>
<Mode>3</Mode>
</dataProcessed>
</records>
<records>
<dataProcessed>
<FieldName>Orange</FieldName>
<Mode>1</Mode>
</dataProcessed>
<dataProcessed>
<FieldName>MTS</FieldName>
<Mode>2</Mode>
</dataProcessed>
</records>
</result>
这里是我想要的代码:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="result/records/dataProcessed[Mode = 'Split']">
<xsl:copy>
<xsl:call-template name="parse-comma-separated">
<xsl:with-param name="elements" select="'FieldName,Mode'"/>
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="parse-comma-separated">
<xsl:param name="elements"/>
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($elements, '|')">
<xsl:element name="{normalize-space(substring-before($elements, ','))}">
<xsl:value-of select="normalize-space(substring-before($text, ','))"/>
</xsl:element>
<xsl:call-template name="parse-comma-separated">
<xsl:with-param name="elements" select="substring-after($elements, ',')"/>
<xsl:with-param name="text" select="substring-after($text, ',')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{normalize-space($elements)}">
<xsl:value-of select="normalize-space($text)"/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
你命名的模板是没有任何意义。尝试从这里调整一个:https://stackoverflow.com/a/31593480/3016153请注意,您必须将分隔符更改为'|'。 –
我试过使用这段代码,但它仍然不能正常工作。我也将分隔符更改为| – Tpi