0
votes

I have an xml i want to modify as follows

I want to insert text into a specify node

XML

<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
         <wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
                             wsu:Id="UsernameToken-80842">
            <wsse:Username>test</wsse:Username>
            <wsse:Password>password</wsse:Password>
         </wsse:UsernameToken>
      </wsse:Security>
   </soap:Header>

what XSLT can i use to insert values into the wsse:Password node eg. so that it is

 <wsse:Password Type="blabla">password</wsse:Password>

How can i do this with xslt?

1
Question is unclear. Can you please clarify what value & how you want to process or use this, what is the input?Rao

1 Answers

0
votes

XSLT 1.0 on your example (Take care of your namespaces):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
    version="1.0">

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

    <xsl:template match="wsse:Password">
        <xsl:copy>
            <xsl:text>abc</xsl:text>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

Result

<?xml version="1.0" encoding="UTF-8"?><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    <wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="UsernameToken-80842">
        <wsse:Username>test</wsse:Username>
        <wsse:Password>abc</wsse:Password>
    </wsse:UsernameToken>
</wsse:Security>

EDIT 1:

<wsse:Password Type="blabla">password</wsse:Password>

There some different approaches to create an attribute:

I.

<xsl:template match="wsse:Password">
    <xsl:copy>
        <xsl:attribute name="Type">
            <!--
                <xsl:text></xsl:text>
                <xsl:value-of select="..."/>
                <xsl:apply-templates select="..."/>
            -->
        </xsl:attribute>
        <xsl:value-of select="."/>
    </xsl:copy>
</xsl:template>

II.

<xsl:template match="wsse:Password">
    <wsse:Password Type="{concat('ab', 'cd')}">
        <xsl:apply-templates/>
    </wsse:Password>
</xsl:template>

Part II: If you want to use an xpath for your attribute "type", you have to work with {}, otherwise you have a consant string value, you can write it like Type="world".