0

I am new to XSL and I am stuck with a problem.

I have an xml of the following format:

<Destinations>
    <conf:Destination id="12">
        <conf:attributes>
            <conf:attribute1>1212</conf:attribute1>
        </conf:attributes>
    </conf:Destination>
    <conf:Destination id="31">
        <conf:attributes>
            <conf:attribute1>3131</conf:attribute1>
        </conf:attributes>
    </conf:Destination>
</Destinations>

And say, I have an xsl with following 2 params:

<xsl:param name="attribute12" select="'21'" />
<xsl:param name="attribute31" select="'5'" />

I want to have a xsl template in XSLT 1 that changes my xml as follows: 1) For destination id=12 in xml, value inside 'conf:attribute1' tag to be set as 21 2) For destination id=31 in xml, value inside 'conf:attribute1' tag to be set as 5

This way I will have final xml as:

<Destinations>
    <conf:Destination id="12">
        <conf:attributes>
            <conf:attribute1>21</conf:attribute1>
        </conf:attributes>
    </conf:Destination>
    <conf:Destination id="31">
        <conf:attributes>
            <conf:attribute1>5</conf:attribute1>
        </conf:attributes>
    </conf:Destination>
</Destinations>

Can anybody please help.

1
  • Why is the question tagged as xslt-2.0 when asking for XSLT 1? Commented Jan 10, 2015 at 13:08

1 Answer 1

1

Use the identity transformation template

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

and then two templates

<xsl:template match="conf:Destination[@id='12']/conf:attributes/conf:attribute1">
  <xsl:copy>
    <xsl:value-of select="$attribute12"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="conf:Destination[@id='31']/conf:attributes/conf:attribute1">
  <xsl:copy>
    <xsl:value-of select="$attribute31"/>
  </xsl:copy>
</xsl:template>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.