Luuk Krijnen
Luuk Krijnen

Reputation: 1192

Create new element with fixed value and dynamic attribute using xslt

if i'm trying to understand xslt where i'm stuck at a place where i want to add an element with a fixed value and a dynamic attribute

input:

<newsItem parentGUID="fakeGuid">
</newsItem>

desired output:

<newsItem>
  <parent Key="fakeGuid">News</parent>
<newsItem>

Current Xslt (value isn't in the actual output)

<xsl:template match="NewsItem">
  <xsl:element name="Parent">
      <xsl:attribute name="Key">
        <xsl:value-of select="@parentGUID"/>
      </xsl:attribute>
      <xsl:value-of select="News"/>
  </xsl:element>
</xsl:template>

Can someone point me out what i'm doing wrong?

Kind regards

Upvotes: 0

Views: 965

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117165

The instruction:

<xsl:value-of select="News"/>

looks for a child element named News to extract its string-value. To output the literal text "News", try:

<xsl:text>News</xsl:text>

Note also that you don't need to use xsl:element to create a literal result element. To get the result you show, you could do:

<xsl:template match="newsItem">
    <xsl:copy>
        <Parent key="{@parentGUID}">News</Parent>
    </xsl:copy>
</xsl:template>

Read about attribute value templates to understand how this works.

Upvotes: 1

Related Questions