Re: [xsl] Transform selected attribute to element when element already has a value | XSLT 2.0

Subject: Re: [xsl] 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
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