Subject: Re: [xsl] clever ways to dynamically copy/create element structure as needed? From: "Chris Papademetrious christopher.papademetrious@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> Date: Sun, 2 Oct 2022 14:58:21 -0000 |
Hi everyone, The moded templates can be simplified a bit to create the empty elements using parse-xml() instead of a variable: <xsl:mode name="add-data" on-no-match="shallow-copy"/> <xsl:template match="topic" mode="add-data"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:apply-templates select="title,titlealts,shortdesc,abstract"/> <xsl:apply-templates select="(prolog, parse-xml('<prolog/>'))[1]" mode="add-data"/> <xsl:apply-templates select="body,related-links,topic"/> </xsl:copy> </xsl:template> <xsl:template match="prolog" mode="add-data"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:apply-templates select="author,source,publisher,copyright,critdates,permissions"/> <xsl:apply-templates select="(metadata,parse-xml('<metadata/>'))[1]" mode="add-data"/> <xsl:apply-templates select="change-historylist,resourceid,data,sort-as,data-about,foreign,mathml, svg-container,unknown"/> </xsl:copy> </xsl:template> <xsl:template match="metadata" mode="add-data"> <xsl:apply-templates select="audience,category,keywords,prodinfo,othermeta"/> <xsl:apply-templates select="data[not(@name = 'prefix')]"/> <data name="prefix">my-prefix</data> <xsl:apply-templates select="sort-as,data-about,foreign,mathml,svg-container,unknown"/> </xsl:template> Note that only the processing down the desired element path (topic/prolog/metadata) uses the moded template. For siblings along the path, we shunt back to the default template (a simple copy in this case) so that we play well with other processing that might be in the stylesheet. * Chris From: Chris Papademetrious christopher.papademetrious@xxxxxxxxxxxx <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> Sent: Saturday, October 1, 2022 5:05 PM To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx Subject: Re: [xsl] clever ways to dynamically copy/create element structure as needed? I tried implementing your suggestion using a moded template. From the default stylesheet mode, I match the topmost element of the element path I know will exist, then call an badd-datab moded template on it: <xsl:mode on-no-match="shallow-copy"/> <xsl:template match="topic"> <xsl:apply-templates select="." mode="add-data"/> </xsl:template> The moded template successively call itself for each lower level, inserting an empty element of the next level if it doesnbt already exist: <xsl:mode name="add-data" on-no-match="shallow-copy"/> <xsl:template match="topic" mode="add-data"> <xsl:variable name="empty-prolog"> <prolog/> </xsl:variable> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:apply-templates select="title,titlealts,shortdesc,abstract"/> <xsl:apply-templates select="(prolog,$empty-prolog)[1]" mode="add-data"/> <xsl:apply-templates select="body,related-links,topic"/> </xsl:copy> </xsl:template> <xsl:template match="prolog" mode="add-data"> <xsl:variable name="empty-metadata"> <metadata/> </xsl:variable> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:apply-templates select="author,source,publisher,copyright,critdates,permissions"/> <xsl:apply-templates select="(metadata,$empty-metadata)[1]" mode="add-data"/> <xsl:apply-templates select="change-historylist,resourceid,data,sort-as,data-about,foreign,mathml, svg-container,unknown"/> </xsl:copy> </xsl:template> <xsl:template match="metadata" mode="add-data"> <xsl:apply-templates select="audience,category,keywords,prodinfo,othermeta"/> <xsl:apply-templates select="data[not(@name = 'prefix')]"/> <data name="prefix">my-prefix</data> <xsl:apply-templates select="sort-as,data-about,foreign,mathml,svg-container,unknown"/> </xsl:template> This is not too bad, but I wonder if there is still a more elegant way to do it. - Chris
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] clever ways to dynamicall, Chris Papademetrious | Thread | Re: [xsl] clever ways to dynamicall, Chris Papademetrious |
Re: [xsl] clever ways to dynamicall, Chris Papademetrious | Date | Re: [xsl] clever ways to dynamicall, Chris Papademetrious |
Month |