XSLT Transformation - Dynamic Element Names

Source input

<SiebelMessage MessageId="1-18J35" IntObjectName="XRX R5 Letter Instance" MessageType="Integration Object" IntObjectFormat="Siebel Hierarchical"> <LetterInstance Id="1-1RUYIF" Language="ENU" TemplateType="SA"> <Field Value="CO Last Name" Datatype="String" Name="ContractingOfficerLastName"> </LetterInstance> </SiebelMessage> 

Expected Result:

 <?xml version="1.0" encoding="UTF-8"?> <SiebelMessage MessageId="1-18J35" IntObjectName="XRX R5 Letter Instance" MessageType="Integration Object" IntObjectFormat="Siebel Hierarchical"> <LetterInstance Id="1-1RUYIF" Language="ENU" TemplateType="SA"> <ContractingOfficerLastName>CO Last Name</ContractingOfficerLastName> <PONumber>POTest000001</PONumber> </LetterInstance> </SiebelMessage> 

Basically, get the value of the Name attribute of the Field element and build a new element, and then get the value of the "Value" attribute and use it as the value of the new element.

+6
source share
2 answers

This stylish XSL stylesheet:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="Field"> <xsl:element name="{@Name}"> <xsl:value-of select="@Value"/> </xsl:element> </xsl:template> </xsl:stylesheet> 

Used for correct input:

 <SiebelMessage MessageId="1-18J35" IntObjectName="XRX R5 Letter Instance" MessageType="Integration Object" IntObjectFormat="Siebel Hierarchical"> <LetterInstance Id="1-1RUYIF" Language="ENU" TemplateType="SA"> <Field Value="CO Last Name" Datatype="String" Name="ContractingOfficerLastName"/> </LetterInstance> </SiebelMessage> 

It produces:

 <SiebelMessage MessageId="1-18J35" IntObjectName="XRX R5 Letter Instance" MessageType="Integration Object" IntObjectFormat="Siebel Hierarchical"> <LetterInstance Id="1-1RUYIF" Language="ENU" TemplateType="SA"> <ContractingOfficerLastName>CO Last Name</ContractingOfficerLastName> </LetterInstance> </SiebelMessage> 

I'm not sure where the <PONumber> should be generated <PONumber> .

+13
source

Something in this direction, if I understand you correctly:

 <xsl:element name="name()"> <xsl:value-of select="./text()" /> </xsl:element> 
0
source

Source: https://habr.com/ru/post/900241/


All Articles