0

The task I need to accomplish is to get the value of the element attribute (ws:PriorValue="PriorValue) and map it to a field in a C# class. The way I have it now I get null. I don't work much with XML and have never used XSLT before but from what I've seen I would need to use <xsl:value-of select="" /> but I'm not quite sure how to accomplish that with the what we already have. I was able to map the value of the element (TestValue) using <xsl:with-param name="targetNode" select="ws:Additional_Information/ws:User_Name/[@Pws:PriorValue] so I know that works but obviously it's not the correct value.

Example XML

<ws:Additional_Information>
    <ws:User_Name ws:PriorValue="PriorValue">TestValue</ws:User_Name>
<ws:Additional_Information>

XSLT Template:

<xsl:template name="MapElement">
    <xsl:param name="targetNode" />
    <xsl:param name="elementName" />
    <xsl:if test="$targetNode and string-length($targetNode/text())>0">
      <xsl:element name="{$elementName}" >
        <xsl:value-of select="$targetNode/text()"/>
      </xsl:element>
    </xsl:if>
 </xsl:template>

Map value to C#:

<xsl:template name="MapWorker" match="ws:Worker" mode="TransformWorker">
    <Worker>
        <AdditionaInformation>
            <xsl:call-template name="MapElement">
                <xsl:with-param name="targetNode" select="ws:Additional_Information/ws:User_Name/@ws:PriorValue" />
                <xsl:with-param name="elementName" select="'PriorUserName'" />
            </xsl:call-template>
            <xsl:call-template name="MapElement">
                <xsl:with-param name="targetNode" select="ws:Personal/ws:Name_Data[ws:Name_Type='Legal']/ws:Last_Name" />
                <xsl:with-param name="elementName" select="'LastName'" />
            </xsl:call-template>
            <xsl:call-template name="MapElement">
                <xsl:with-param name="targetNode" select="ws:Personal/ws:Name_Data[ws:Name_Type='Preferred']/ws:First_Name" />
                <xsl:with-param name="elementName" select="'PreferredFirstName'" />
           </xsl:call-template>
       </AdditionalInformation>
   </Worker>
</xsl:template>

1 Answer 1

0

Remove the /text() from your XPath expressions, because attributes have no text() children (because they are already an attribute child that cannot have any children itself) - hence the attempt to get the text() of an attribute will be null.
Changing your template to the following will handle both elements and attributes:

<xsl:template name="MapElement">
    <xsl:param name="targetNode" />
    <xsl:param name="elementName" />
    <xsl:if test="$targetNode and string-length($targetNode)>0">
      <xsl:element name="{$elementName}" ><xsl:value-of select="$targetNode"/></xsl:element>
    </xsl:if>
</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.