简体   繁体   中英

XSLT failed to transform message

Hello i am having problem with xslt i am working with. I am trying to transform message with following stylesheet:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
    <xsl:param name="ename">Test</xsl:param>
    <xsl:param name="evalue">Test1234</xsl:param>

    <xsl:param name="removeElementsNamed" select="'Test'"/>
    <!-- <xsl:param name="numberGenerated" select="following-sibling::metadata/metadata-element[key='sequence_no']"/> -->
    <xsl:param name="numberGenerated" select="following-sibling::metadata[metadata-element/key='messageuniqueid'][2]"/>
    <xsl:param name="id" select="0004"/>

    <xsl:output method="xml" encoding="utf-8"/>
    <xsl:template match="@*|node()" name="input">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>     
    <xsl:template match="Root">
        <xsl:copy>
            <xsl:apply-templates/>
                <xsl:if test="not(c)">
                    <xsl:element name="{$ename}"><xsl:value-of select="$evalue"/></xsl:element>
                </xsl:if> 
                <xsl:if test="Record/Composite/Field/@id = $id">
                    <xsl:value-of select="$numberGenerated"/>>
                </xsl:if> 
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Root">
        <xsl:if test="not(name() = $removeElementsNamed)">
            <xsl:call-template name="input"/>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

The following is my input message

<Message>
  <payload>
    <Root id="ORDERS">
      <Record id="UNB">
        <Composite id="S001">
          <Field id="0001">UNOA</Field>
          <Field id="0002">2</Field>
        </Composite>
        <Composite id="S002">
          <Field id="0004">8712423008458</Field>
          <Field id="0007">14</Field>
          <Field id="0008">VALKSOFTWARE</Field>
        </Composite>
        <Composite id="S003">
          <Field id="0007">14</Field>
          <Field id="0014">03</Field>
        </Composite>
        <Composite id="S004">
          <Field id="0017">20070123</Field>
          <Field id="0019">1420</Field>
        </Composite>
        <Field id="0020">87096</Field>
      </Record>
    </Root>
  </payload>
    <metadata>
        <metadata-element>
            <key>messageuniqueid</key>  
            <value>unique-id</value>
        </metadata-element>
    </metadata>
    <metadata>
        <metadata-element>
            <key>sequence_no</key>  
            <value>0123456789</value>
        </metadata-element>
    </metadata>
</Message>

What i want to achieve is basically get the value of metadata element ( sequence_no ) which in this case is "0123456789" and i want to paste this value in place of "id=0004" so at the end it will show "id=0123456789"

Thanks

There are several approaches to this. Below is one example to create a template which matches "Field":

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:param name="ename">Test</xsl:param>
    <xsl:param name="evalue">Test1234</xsl:param>
    <xsl:param name="removeElementsNamed" select="'Test'"/>
    <xsl:param name="id" select="0004"/>
    <xsl:output method="xml" encoding="utf-8"/>
    <xsl:template match="@*|node()" name="input">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Root">
        <xsl:copy>
            <xsl:apply-templates/>
            <xsl:if test="not(c)">
                <xsl:element name="{$ename}">
                    <xsl:value-of select="$evalue"/>
                </xsl:element>
            </xsl:if>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Root">
        <xsl:if test="not(name() = $removeElementsNamed)">
            <xsl:call-template name="input"/>
        </xsl:if>
    </xsl:template>
    <xsl:template match="Field">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:if test="@id = $id">
                <xsl:attribute name="id"><xsl:value-of select="ancestor::Message/metadata[2]/metadata-element/value"/></xsl:attribute>
            </xsl:if>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM