XSLT: Add content to an XML depending on conditions -


i trying transform xml-file xml-file, content added in case not provided.

my xml looks this:

<bookstore>    <book>         <title>book1</title>         <author>author1</author>         <chapters>              <chapter>                   <ch-name>blabla</ch-name>                   <ch-number>1</ch-number>              </chapter>         </chapters>    </book>    <book>         <title>book2</title>         <author>author2</author>         <chapters>              <chapter>                   <ch-name>test</ch-name>                   <ch-number>1</ch-number>              </chapter>         </chapters>    </book>    <book>         <title>book3</title>         <author>author3</author>    </book> </bookstore> 

now, want add chapters element (and child elements) books, doesn't exist already, create sort of default.

my xslt looks this:

<dns:template match="book[not(exists(chapters))]">     <xsl:copy>       <xsl:apply-templates select="@*|*"/>     </xsl:copy>     <chapters>            <chapter>                 <ch-name>default</ch-name>                 <ch-number>default</ch-number>             </chapter>     </chapters>    </dns:template>  <xsl:template match="@* | *">     <xsl:copy>         <xsl:apply-templates select="@* | * | text()"/>     </xsl:copy> </xsl:template> 

however, when try in online editor, there no changes. dns namespace should support not(exists) part.

is there wrong xslt?

thank given!

change "dns" prefix "xsl"(of course can have prefix defined again same namespace, not recommended). if using xslt1.0, there no "exists()" function present. also, "chapters" needs inside . can use xslt:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="book[not(chapters)]">     <xsl:copy>         <xsl:apply-templates select="@*|*"/>         <chapters>             <chapter>                 <ch-name>default</ch-name>                 <ch-number>default</ch-number>             </chapter>         </chapters>     </xsl:copy>  </xsl:template>  <xsl:template match="@* | *">     <xsl:copy>         <xsl:apply-templates select="@* | * | text()"/>     </xsl:copy> </xsl:template> </xsl:stylesheet> 

Comments

Popular posts from this blog

How to access named pipes using JavaScript in Firefox add-on? -

multithreading - OPAL (Open Phone Abstraction Library) Transport not terminated when reattaching thread? -

node.js - req param returns an empty array -