Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
914 views
in Technique[技术] by (71.8m points)

xml - 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 Outcome:

<?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 getting the value of Name attribute of Field element and construct a new element and then get the value of "Value" attribute and using it as value of new element.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This 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>

Applied to well-formed 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>

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 <PONumber> is supposed to be generated from.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...