Home > OS >  Select the parent node along with its properties in XSLT
Select the parent node along with its properties in XSLT

Time:12-20

i have this xslt script:

<xsl:template match="xsl:for-each[starts-with(@select, '(./ns0:CD')]">
     <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <!-- PULL PARENT AND SIBLING NODES -->
        <xsl:element name="{name(..)}">
            <xsl:apply-templates select="preceding-sibling::*[1]"/>
            <xsl:apply-templates select="*"/>
        </xsl:element>
     </xsl:copy>
    </xsl:template>

This will take the parent element of the selected node with all the children. But the problem is that the parent has also some properties which i want to keep. The "name" takes only the name of the parent node. How to extract and keep the properties? For example i have this xslt:

<xsl:template match="/">
        <CD123 xmls="hello">
            <xsl:attribute name="xsi:schemaLocation" namespace="http://www.w3.org/2001/XMLSchema-instance"/>
            <xsl:for-each select="(./ns0:CD538C)[fn:not(fn:exists(*:ExportOperation[fn:namespace-uri() eq '']/*:requestRejectionReasonCode[fn:namespace-uri() eq '']))]">
                <SynIde xmlns="">UN1OC</SynIde>
                <SynVer xmlns="">
                    <xsl:sequence select="xs:string(xs:integer('3'))"/>
                </SynVer>
            </xsl:for-each>
        </CD123>
    </xsl:template>

If i apply the starting script the node CD123 will lose its property xmls. How to keep the property?

CodePudding user response:

It is the namespace, I suppose, so you can use e.g.

<xsl:element name="{name(..)}" namespace="{namespace-uri(..)}">

Additionally you might want e.g. <xsl:copy-of select="../namespace::*"/>.

XSLT 3 allows e.g. <xsl:copy select="..">.

  • Related