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

Re: Transform selected attribute to element when eleme

Subject: Re: Transform selected attribute to element when element already has a value | XSLT 2.0
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 22 May 2021 08:12:38 -0000
Re:  Transform selected attribute to element when eleme
Am 22.05.2021 um 02:02 schrieb Sam Spade anonymousjuly1@xxxxxxxx:
>
> 2.As I point out in the post,
>
>
https://stackoverflow.com/questions/67575484/what-attribute-node-cannot-follo
w-non-attribute-node-in-element-content-tells
>
<https://stackoverflow.com/questions/67575484/what-attribute-node-cannot-foll
ow-non-attribute-node-in-element-content-tells>
>
> it is not only very difficult to search value if an element
> (<currency>) contains both element (<id>) and text value(USD) but also
> returns false result.
>
> <amount>
> <currency>
> <id>settlementCurrency</id>USD</currency>
> <referenceAmount>StandardISDA</referenceAmount>
> <cashSettlement>true</cashSettlement>
> </amount>
>
> That USDreally is the value of<currency>.In this case, it is
> legitimate and sensible to transform idas sibling element of its
> original element <currency>(its parent is still <amount>) to:
>
> <amount>
> <currency>USD</currency>
> <id>settlementCurrency</id>
> <referenceAmount>StandardISDA</referenceAmount>
> <cashSettlement>true</cashSettlement>
> </amount>
>
>
> The flattened XML is very searchable (an index on 'currency' is good
> enough) and can be transformed to JSON.
>
>
>

I am not sure an XSLT program can easily capture your rules but for the
given sample, if you insert a check on elements whether they both have
attributes and non-whitespace text content and in that case transform
any attributes to sibling following the element, the result is as you
wanted:


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 B  version="3.0"
 B  xmlns:xs="http://www.w3.org/2001/XMLSchema"
 B  exclude-result-prefixes="#all">

 B  <xsl:param name="namespace"
as="xs:string">http://fc.fasset/product</xsl:param>

 B  <xsl:param name="root" as="xs:string">requestProduct</xsl:param>
 B  <xsl:param name="keepAttr" static="yes" as="xs:string*"
select="'href', 'id'"/>

 B  <xsl:strip-space elements="*"/>
 B  <xsl:output indent="yes"/>

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

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

 B  <xsl:template match="*[@* and text()[normalize-space()]]">
 B B B B B  <xsl:element name="{local-name()}" namespace="{$namespace}">
 B B B B B B B B B  <xsl:apply-templates/>
 B B B B B  </xsl:element>
 B B B B B  <xsl:apply-templates select="@*"/>
 B  </xsl:template>

 B  <xsl:template match="@*[local-name() = $keepAttr]">
 B B B B B  <xsl:element name="{local-name()}" namespace="{$namespace}">
 B B B B B B B  <xsl:value-of select="."/>
 B B B B B  </xsl:element>
 B  </xsl:template>

</xsl:stylesheet>

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.