xslt - Issue with Multiple Nodes in XML using XSL -


i want add new keyword if node "name1" exist , if node "name1" not exist , if whole data node not exist add whole predefine section below:

<data> <keyword>keyword1</keyword> <name>name1</name> <data> 

for above use below logic in xsl:

<xsl:param name="addkeywords">     <!-- predefine structure add keyword --> </xsl:param>      <xsl:template         match="data/[name='name1']/keyword[position()=last()]">         <xsl:copy-of select="$addkeywords"/>`     </xsl:template>      <xsl:param name="adddatasection">         <!-- predefine structure add whole new data section -->     </xsl:param>      <xsl:template         match="data/[name!='name1'][position()=last()]">         <xsl:copy-of select="$adddatasection"/>     </xsl:template> 

now problem there multiple data section in document, if in first section name1 found new keyword add after there multiple data node exist without name1 data add keyword in first data section create whole new data section, duplication issue arrise. both template execute because of multiple data section.

so problem??

i not 100% clear asking, might after.

  • "if data element has child name element text name1 append constant keywords existing keywords child.

  • "if data element not have child name element text name1 remove contents of data , replace default, populated keyword , name child elements.

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">    <xsl:output method="xml"/>     <xsl:template match="@*|node()">       <xsl:copy>          <xsl:apply-templates select="@*|node()"/>       </xsl:copy>    </xsl:template>     <xsl:variable name="extrakeywords">,hello,world</xsl:variable>    <xsl:variable name="missingdatasection">       <keyword>somedefaultkeywords</keyword>       <name>somedefaultname</name>    </xsl:variable>     <xsl:template match="keyword[ancestor::data[name='name1']]">       <keyword>          <xsl:value-of select="."/>          <xsl:value-of select="$extrakeywords"/>       </keyword>    </xsl:template>     <xsl:template match="data[name!='name1']">       <data>          <xsl:copy-of select="$missingdatasection"></xsl:copy-of>       </data>    </xsl:template>  </xsl:stylesheet> 

when applied xml:

<xml>    <data>       <keyword>keyword1</keyword>       <name>name1</name>    </data>    <data>       <keyword>anotherkeyword</keyword>       <name>notname1</name>    </data> </xml> 

produces result:

<?xml version="1.0" encoding="utf-8"?> <xml>    <data>       <keyword>keyword1,hello,world</keyword>       <name>name1</name>    </data>    <data>       <keyword>somedefaultkeywords</keyword>       <name>somedefaultname</name>    </data> </xml> 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -