xslt - Insert node between chuck of xml based on xpath expression -
variable xml_node contains 2 nodes.
<parameter name="fsc-a" type="30"> <is_log>false</is_log> <is_quantitated>false</is_quantitated> <min>0.0</min> <max>262143.0</max> <raw_index>1</raw_index> <linear_index>1</linear_index> <log_index>19</log_index> <final_index>1</final_index> <fl>fsc</fl> <voltage>302</voltage> <target>3</target> <brightness>1.0</brightness> <threshold>30000</threshold> <trigger>true</trigger> <labels_only>false</labels_only> <biexp_scale>-1</biexp_scale> <comp_biexp_scale>-1</comp_biexp_scale> <manual_biexp_scale>0</manual_biexp_scale> <can_be_compensated>false</can_be_compensated> </parameter> <parameter name="ssc-a" type="30"> <is_log>false</is_log> <is_quantitated>false</is_quantitated> <min>0.0</min> <max>262143.0</max> <raw_index>2</raw_index> <linear_index>2</linear_index> <log_index>20</log_index> <final_index>2</final_index> <fl>ssc</fl> <voltage>307</voltage> <target>3</target> <brightness>1.0</brightness> <threshold>5000</threshold> <trigger>false</trigger> <labels_only>false</labels_only> <biexp_scale>-1</biexp_scale> <comp_biexp_scale>-1</comp_biexp_scale> <manual_biexp_scale>0</manual_biexp_scale> <can_be_compensated>false</can_be_compensated> </parameter>
my xpath //parameter[@name = 'fsc-a']
i insert $extra_node after xpath
variable extra_node contain node.
<parameter name="ssc-h" type="30"> <is_log>false</is_log> <is_quantitated>false</is_quantitated> <min>0.0</min> <max>262143.0</max> <raw_index>2</raw_index> <linear_index>2</linear_index> <log_index>20</log_index> <final_index>2</final_index> <fl>ssc</fl> <voltage>307</voltage> <target>3</target> <brightness>1.0</brightness> <threshold>5000</threshold> <trigger>false</trigger> <labels_only>false</labels_only> <biexp_scale>-1</biexp_scale> <comp_biexp_scale>-1</comp_biexp_scale> <manual_biexp_scale>0</manual_biexp_scale> <can_be_compensated>false</can_be_compensated> </parameter>
so final
<parameter name="fsc-a" type="30"> <is_log>false</is_log> <is_quantitated>false</is_quantitated> <min>0.0</min> <max>262143.0</max> <raw_index>1</raw_index> <linear_index>1</linear_index> <log_index>19</log_index> <final_index>1</final_index> <fl>fsc</fl> <voltage>302</voltage> <target>3</target> <brightness>1.0</brightness> <threshold>30000</threshold> <trigger>true</trigger> <labels_only>false</labels_only> <biexp_scale>-1</biexp_scale> <comp_biexp_scale>-1</comp_biexp_scale> <manual_biexp_scale>0</manual_biexp_scale> <can_be_compensated>false</can_be_compensated> </parameter> <parameter name="ssc-h" type="30"> <is_log>false</is_log> <is_quantitated>false</is_quantitated> <min>0.0</min> <max>262143.0</max> <raw_index>2</raw_index> <linear_index>2</linear_index> <log_index>20</log_index> <final_index>2</final_index> <fl>ssc</fl> <voltage>307</voltage> <target>3</target> <brightness>1.0</brightness> <threshold>5000</threshold> <trigger>false</trigger> <labels_only>false</labels_only> <biexp_scale>-1</biexp_scale> <comp_biexp_scale>-1</comp_biexp_scale> <manual_biexp_scale>0</manual_biexp_scale> <can_be_compensated>false</can_be_compensated> </parameter> <parameter name="ssc-a" type="30"> <is_log>false</is_log> <is_quantitated>false</is_quantitated> <min>0.0</min> <max>262143.0</max> <raw_index>2</raw_index> <linear_index>2</linear_index> <log_index>20</log_index> <final_index>2</final_index> <fl>ssc</fl> <voltage>307</voltage> <target>3</target> <brightness>1.0</brightness> <threshold>5000</threshold> <trigger>false</trigger> <labels_only>false</labels_only> <biexp_scale>-1</biexp_scale> <comp_biexp_scale>-1</comp_biexp_scale> <manual_biexp_scale>0</manual_biexp_scale> <can_be_compensated>false</can_be_compensated> </parameter>
i tried this, nut doesn't work, because don't know doing. can please help?
<xsl:variable name="xpath" select="//root/parameter[@name = 'fsc-a']"/> <xsl:copy-of select="custom:insertafter($xml_node,$xpath,saxon:parse($extra_node))"/> <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ insertafter function~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <xsl:function name="custom:insertafter"> <xsl:param name="doc" as="node()"/> <!--one node: root node of xml chunk insert should done --> <xsl:param name="xpath" as="xs:string"/> <!--string: xpath of node after new node placed --> <xsl:param name="insert_nodes" as="node()*"/> <!--one or more nodes: node(s) inserted after specified node --> <xsl:variable name="insert_after_nodes" select="custom:evaluatexpath($xpath,$doc)" as="node()*"/> <xsl:variable name="result" as="node()*"> <xsl:apply-templates mode="insert_node" select="$doc"> <xsl:with-param name="insert_after_nodes" select="$insert_after_nodes" tunnel="yes"/> <xsl:with-param name="insert_nodes" select="$insert_nodes" tunnel="yes"/> </xsl:apply-templates> </xsl:variable> <xsl:copy-of select="ext:node-set($result)"/> </xsl:function> <xsl:function name="custom:evaluatexpath"> <xsl:param name="path" as="xs:string"/> <xsl:param name="context"/> <xsl:for-each select="$context"> <!-- change context document --> <xsl:sequence select="saxon:evaluate($path)"/> </xsl:for-each> </xsl:function>
Comments
Post a Comment