XSLT : How do i assign Constant value using XML element -


we have input xml. in that,we tried put constant value using <lineactiontype> element.

we want add <lineactiontype >a<lineactiontype /> element constant value on each comment line tag.

 <commentline>                     <comment>comment#1</comment>                     <orderlineid>or-1810143</orderlineid>                     <userdefined>3</userdefined>                     <lineactiontype >a<lineactiontype />                  </commentline> 

input xml

<?xml version="1.0" encoding="utf-8"?> <salesorders xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:xsd="http://www.w3.org/2001/xmlschema-instance" xsd:nonamespaceschemalocation="sortoidoc.xsd">    <orders>       <orderheader>          <customerponumber>manual order 1</customerponumber>          <orderactiontype>a</orderactiontype>       </orderheader>       <orderdetails>          <stockline>             <customerpoline>9999</customerpoline>             <stockcode>abc-1</stockcode>             <stockdescription>assembled</stockdescription>             <orderqty>2.0</orderqty>             <orderuom>ea</orderuom>             <priceuom>ea</priceuom>             <alwaysusepriceentered>y</alwaysusepriceentered>             <userdefined>1</userdefined>             <orderlineid>or-1810141</orderlineid>          </stockline>          <stockline>             <customerpoline>9999</customerpoline>             <stockcode>fbx-smo30029-8</stockcode>             <stockdescription>target soap pdq 2014 assembled</stockdescription>             <orderqty>3.0</orderqty>             <orderuom>ea</orderuom>             <priceuom>ea</priceuom>             <alwaysusepriceentered>y</alwaysusepriceentered>             <userdefined>2</userdefined>             <orderlineid>or-1810142</orderlineid>          </stockline>          <commentline>             <comment>comment#1</comment>             <orderlineid>or-1810143</orderlineid>             <userdefined>3</userdefined>             <lineactiontype />          </commentline>          <commentline>             <comment>comment#2</comment>             <orderlineid>or-1810144</orderlineid>             <userdefined>4</userdefined>             <lineactiontype />          </commentline>       </orderdetails>    </orders> </salesorders> 

we tried following xslt 2.0:

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:xs="http://www.w3.org/2001/xmlschema" version="2.0">    <xsl:output method="xml" encoding="windows-1252" indent="yes" />    <xsl:template match="@*|node()">       <xsl:copy>          <xsl:apply-templates select="@*|node()" />       </xsl:copy>    </xsl:template>    <xsl:template match="orderdetails">       <xsl:copy>          <xsl:apply-templates select="*">             <xsl:sort select="xs:integer(userdefined)" />          </xsl:apply-templates>       </xsl:copy>    </xsl:template>    <xsl:template match="stockline[not(stockcodedescription) , not (orderqty) , not(price)]">       <commentline>          <comment>             <xsl:value-of select="stockcode" />          </comment>          <xsl:copy-of select="orderlineid" />          <xsl:copy-of select="userdefined" />         </commentline>    </xsl:template>    <xsl:template match="commentline[orderlineid = preceding-sibling::stockline/orderlineid , not(comment)]" />    <xsl:template match="commentline[some $sib in preceding-sibling::commentline satisfies deep-equal(., $sib)]" /> </xsl:stylesheet> 

expected output :

<?xml version="1.0" encoding="utf-8"?> <salesorders xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:xsd="http://www.w3.org/2001/xmlschema-instance" xsd:nonamespaceschemalocation="sortoidoc.xsd">    <orders>       <orderheader>          <customerponumber>manual order 1</customerponumber>          <orderactiontype>a</orderactiontype>       </orderheader>       <orderdetails>          <stockline>             <customerpoline>9999</customerpoline>             <stockcode>abc-1</stockcode>             <stockdescription>assembled</stockdescription>             <orderqty>2.0</orderqty>             <orderuom>ea</orderuom>             <priceuom>ea</priceuom>             <alwaysusepriceentered>y</alwaysusepriceentered>             <userdefined>1</userdefined>             <orderlineid>or-1810141</orderlineid>          </stockline>          <stockline>             <customerpoline>9999</customerpoline>             <stockcode>fbx-smo30029-8</stockcode>             <stockdescription>target soap pdq 2014 assembled</stockdescription>             <orderqty>3.0</orderqty>             <orderuom>ea</orderuom>             <priceuom>ea</priceuom>             <alwaysusepriceentered>y</alwaysusepriceentered>             <userdefined>2</userdefined>             <orderlineid>or-1810142</orderlineid>          </stockline>          <commentline>             <comment>comment#1</comment>             <orderlineid>or-1810143</orderlineid>             <userdefined>3</userdefined>             <lineactiontype >a<lineactiontype />          </commentline>          <commentline>             <comment>comment#2</comment>             <orderlineid>or-1810144</orderlineid>             <userdefined>4</userdefined>            <lineactiontype >a<lineactiontype />          </commentline>       </orderdetails>    </orders> </salesorders> 

any on appreciated ?

firstly note <lineactiontype >a<lineactiontype /> not valid xml, assuming mean <lineactiontype>a</lineactiontype>.

anyway, if commentline elements had lineactiontype , wanted set them a, add template along identity template

<xsl:template match="commentline/lineactiontype">    <xsl:copy>a</xsl:copy> </xsl:template> 

alternatively, if lineactiontype not present under commentline, try adding these 2 templates instead. 1 add under commentline, , prevent existing 1 being output (so doesn't output twice)

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

Comments