数据块中的 XSLT 转换以根据条件获取值
XSLT Transformation in databricks to get the value based on condition
我是 XSL 编程的新手,正在尝试执行以下转换,我必须根据其他 attribute/tag
中的文本值获取值
来源XML:
<ns1:specificationSectionDetail>
<ns1:specificationSectionFoodOtherLabellingCopySection>
<ns1:claimOrStatement>
<ns1:parameterisedEntity>
<ns2:code>NUTRITION_CLAIMS_104</ns2:code>
<ns2:localeData>
<ns1:description>
<![CDATA[Health Star Rating DI/Claims {l}]]>
</ns1:description>
<ns1:id>16601</ns1:id>
</ns2:localeData>
<ns1:packCopyText>Health Star Rating DI/Claims HSR - Test 72339</ns1:packCopyText>
<ns1:useOnPackText>Back - Follow Brand Style Guide</ns1:useOnPackText>
<ns1:statement>Health Star Rating DI/Claims HSR - Test 72339</ns1:statement>
</ns1:claimOrStatement>
<ns1:claimOrStatement>
<ns1:parameterisedEntity>
<ns2:code>NUTRITION_CLAIMS_35</ns2:code>
<ns2:localeData>
<ns1:description>
<![CDATA[Health Star Rating {s}]]>
</ns1:description>
<ns1:packCopyText>Health Star Rating Better - 72339</ns1:packCopyText>
<ns1:useOnPackText>Front - Follow Brand Style Guide</ns1:useOnPackText>
<ns1:statement>Health Star Rating Better - 72339</ns1:statement>
</ns1:claimOrStatement>
<ns1:claimOrStatement>
<ns1:parameterisedEntity>
<ns2:code>NUTRITION_CLAIMS_258</ns2:code>
<ns2:localeData>
<ns1:description>
<![CDATA[Potassium contributes to normal functioning of the nervous system]]>
</ns1:description>
<ns1:id>18193</ns1:id>
</ns2:localeData>
<ns1:packCopyText>Potassium contributes to normal functioning of the nervous system</ns1:packCopyText>
<ns1:useOnPackText>Back - Follow Brand Style Guide</ns1:useOnPackText>
<ns1:statement>Potassium contributes to normal functioning of the nervous system</ns1:statement>
</ns1:claimOrStatement>
XSLT 文件
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns1="http://www.micros.com/creations/core/domain/dto/v1p0/full" xmlns:ns2="http://www.micros.com/creations/core/domain/dto/v1p0/simple" exclude-result-prefixes="ns1 ns1">
<xsl:template match="health-star-rating">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text,'DI/Claims')">
<xsl:value-of select="$text" />
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="/">
<ItemDetails>
<SourceSystem>
<xsl:text>Fusion</xsl:text>
</SourceSystem>
<ActionType>
<xsl:text>Create</xsl:text>
</ActionType>
<CreateDateTime>
<xsl:text>2021-11-10T08:00:00</xsl:text>
</CreateDateTime>
<Items>
<Item>
<HealthStarRating>
<xsl:value-of select="ns1:productSpecificationFullDTO/ns1:specificationSectionDetail/ns1:specificationSectionFoodOtherLabellingCopySection/ns1:claimOrStatement/ns1:packCopyText"/>
</HealthStarRating>
<HealthStarRatingDIClaims>
<xsl:value-of select="ns1:productSpecificationFullDTO/ns1:specificationSectionDetail/ns1:specificationSectionFoodOtherLabellingCopySection/ns1:claimOrStatement/ns1:packCopyText"/>
</HealthStarRatingDIClaims>
</Item>
</Items>
</ItemDetails>
</xsl:template>
</xsl:stylesheet>
转换后的 XML 应如下所示
<HealthStarRating>Health Star Rating Better - 72339</HealthStarRating>
<HealthStarRatingDIClaims>Health Star Rating DI/Claims HSR - Test 72339</HealthStarRatingDIClaims>
<Nutritions>
<Instruction>
Potassium contributes to normal functioning of the nervous system
</Instruction>
</Nutritions>
基本上,XSLT 应该包含一个条件,该条件确定“描述”标签的值并获取“文本”标签的值并作为单独的属性输出
提前致谢
您可以使用以下 XSLT-1.0 模板。
第一步,它提取不带空格的“描述”并将其存储在变量中。
在第二步中,它检查描述是特定类型之一还是 <Others>
。如果它是特定类型,它会从描述中派生元素名称。
<xsl:template match="claims">
<xsl:variable name="elemName" select="translate(entity/description,' 

','')" />
<xsl:choose>
<xsl:when test="$elemName='HealthStar' or $elemName='DIClaims'">
<xsl:element name="{$elemName}">
<xsl:value-of select="normalize-space(Text)" />
</xsl:element>
</xsl:when>
<xsl:otherwise>
<Others><xsl:value-of select="normalize-space(Text)" /></Others>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
它的输出符合要求。
我是 XSL 编程的新手,正在尝试执行以下转换,我必须根据其他 attribute/tag
中的文本值获取值来源XML:
<ns1:specificationSectionDetail>
<ns1:specificationSectionFoodOtherLabellingCopySection>
<ns1:claimOrStatement>
<ns1:parameterisedEntity>
<ns2:code>NUTRITION_CLAIMS_104</ns2:code>
<ns2:localeData>
<ns1:description>
<![CDATA[Health Star Rating DI/Claims {l}]]>
</ns1:description>
<ns1:id>16601</ns1:id>
</ns2:localeData>
<ns1:packCopyText>Health Star Rating DI/Claims HSR - Test 72339</ns1:packCopyText>
<ns1:useOnPackText>Back - Follow Brand Style Guide</ns1:useOnPackText>
<ns1:statement>Health Star Rating DI/Claims HSR - Test 72339</ns1:statement>
</ns1:claimOrStatement>
<ns1:claimOrStatement>
<ns1:parameterisedEntity>
<ns2:code>NUTRITION_CLAIMS_35</ns2:code>
<ns2:localeData>
<ns1:description>
<![CDATA[Health Star Rating {s}]]>
</ns1:description>
<ns1:packCopyText>Health Star Rating Better - 72339</ns1:packCopyText>
<ns1:useOnPackText>Front - Follow Brand Style Guide</ns1:useOnPackText>
<ns1:statement>Health Star Rating Better - 72339</ns1:statement>
</ns1:claimOrStatement>
<ns1:claimOrStatement>
<ns1:parameterisedEntity>
<ns2:code>NUTRITION_CLAIMS_258</ns2:code>
<ns2:localeData>
<ns1:description>
<![CDATA[Potassium contributes to normal functioning of the nervous system]]>
</ns1:description>
<ns1:id>18193</ns1:id>
</ns2:localeData>
<ns1:packCopyText>Potassium contributes to normal functioning of the nervous system</ns1:packCopyText>
<ns1:useOnPackText>Back - Follow Brand Style Guide</ns1:useOnPackText>
<ns1:statement>Potassium contributes to normal functioning of the nervous system</ns1:statement>
</ns1:claimOrStatement>
XSLT 文件
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns1="http://www.micros.com/creations/core/domain/dto/v1p0/full" xmlns:ns2="http://www.micros.com/creations/core/domain/dto/v1p0/simple" exclude-result-prefixes="ns1 ns1">
<xsl:template match="health-star-rating">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text,'DI/Claims')">
<xsl:value-of select="$text" />
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="/">
<ItemDetails>
<SourceSystem>
<xsl:text>Fusion</xsl:text>
</SourceSystem>
<ActionType>
<xsl:text>Create</xsl:text>
</ActionType>
<CreateDateTime>
<xsl:text>2021-11-10T08:00:00</xsl:text>
</CreateDateTime>
<Items>
<Item>
<HealthStarRating>
<xsl:value-of select="ns1:productSpecificationFullDTO/ns1:specificationSectionDetail/ns1:specificationSectionFoodOtherLabellingCopySection/ns1:claimOrStatement/ns1:packCopyText"/>
</HealthStarRating>
<HealthStarRatingDIClaims>
<xsl:value-of select="ns1:productSpecificationFullDTO/ns1:specificationSectionDetail/ns1:specificationSectionFoodOtherLabellingCopySection/ns1:claimOrStatement/ns1:packCopyText"/>
</HealthStarRatingDIClaims>
</Item>
</Items>
</ItemDetails>
</xsl:template>
</xsl:stylesheet>
转换后的 XML 应如下所示
<HealthStarRating>Health Star Rating Better - 72339</HealthStarRating>
<HealthStarRatingDIClaims>Health Star Rating DI/Claims HSR - Test 72339</HealthStarRatingDIClaims>
<Nutritions>
<Instruction>
Potassium contributes to normal functioning of the nervous system
</Instruction>
</Nutritions>
基本上,XSLT 应该包含一个条件,该条件确定“描述”标签的值并获取“文本”标签的值并作为单独的属性输出
提前致谢
您可以使用以下 XSLT-1.0 模板。
第一步,它提取不带空格的“描述”并将其存储在变量中。
在第二步中,它检查描述是特定类型之一还是 <Others>
。如果它是特定类型,它会从描述中派生元素名称。
<xsl:template match="claims">
<xsl:variable name="elemName" select="translate(entity/description,' 

','')" />
<xsl:choose>
<xsl:when test="$elemName='HealthStar' or $elemName='DIClaims'">
<xsl:element name="{$elemName}">
<xsl:value-of select="normalize-space(Text)" />
</xsl:element>
</xsl:when>
<xsl:otherwise>
<Others><xsl:value-of select="normalize-space(Text)" /></Others>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
它的输出符合要求。