[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Transform selected attribute to element when element

Subject: Transform selected attribute to element when element already has a value | XSLT 2.0
From: "Sam Spade anonymousjuly1@xxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 21 May 2021 14:30:07 -0000
 Transform selected attribute to element when element
Input:
B <requestConfirmation xmlns="http://example/confirmation">    <trade>
        <cal>
            <c>PRECEDING</c>
            <bcs id="businessCenters">
                <bc>USNY</bc>
                <bc>GBLO</bc>
            </bcs>
        </cal>
        <amount>
            <currency id="settlementCurrency"
currencyScheme="http://example/iso4">USD</currency>
            <referenceAmount>StandardISDA</referenceAmount>
            <cashSettlement>true</cashSettlement>
        </amount>
    </trade>
</requestConfirmation>
My module: <xsl:param name="namespace"
as="xs:string">http://fc.fasset/product</xsl:param>
    <xsl:param name="root" as="xs:string">requestProduct</xsl:param>
    <xsl:param name="keepAttr" static="yes" as="xs:string*" select="'href',
'id'"/>

    <xsl:template match="/*">
        <xsl:element name="{$root}" namespace="{$namespace}">
            <xsl:apply-templates select="@* , node()"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="*">
        <xsl:element name="{local-name()}" namespace="{$namespace}">
            <xsl:apply-templates select="@* , node()"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="@*">
        <xsl:choose>
            <xsl:when test="local-name() = $keepAttr">
                <xsl:element name="{local-name()}" namespace="{$namespace}">
                    <xsl:value-of select="."/>
                </xsl:element>
            </xsl:when>
        </xsl:choose>
    </xsl:template>
Undesirable result:
<requestProduct xmlns="http://fc.fasset/product">
   <trade>
      <cal>
         <c>PRECEDING</c>
         <bcs>
            <id>businessCenters</id>
            <bc>USNY</bc>
            <bc>GBLO</bc>
         </bcs>
      </cal>
      <amount>
         <currency>
            <id>settlementCurrency</id>USD</currency>
         <referenceAmount>StandardISDA</referenceAmount>
         <cashSettlement>true</cashSettlement>
      </amount>
   </trade>
</requestProduct>The transformation logic:
1) transform namespace
2) transform root element
3) transform the selected attribute(s) to element (the attribute name is
parameterised not hard-coded)
4) the transformed element shouldn't contain both other element (transformed
from the attribute) and its original data
The desired result:
<requestProduct xmlns="http://fc.fasset/product">
   <trade>
      <cal>
         <c>PRECEDING</c>
         <bcs>
            <id>businessCenters</id>
            <bc>USNY</bc>
            <bc>GBLO</bc>
         </bcs>
      </cal>
      <amount>
         <currency>USD</currency>
         <id>settlementCurrency</id>
         <referenceAmount>StandardISDA</referenceAmount>
         <cashSettlement>true</cashSettlement>
      </amount>
   </trade>
</requestProduct>What is wrong with my module? How can I transform the input
to the desired result?
Thank you!

B B B 

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.