Transform attribute to element when element already has a value
Here should go questions about transforming XML with XSLT and FOP.
-
- Posts: 17
- Joined: Fri Aug 21, 2020 5:40 pm
Transform attribute to element when element already has a value
Post by Fiona Chen »
Hello, I need some help to transform below XML:
The desired result:
There are three criteria for the transformation:
1) new namespace
2) new root element
3) targeted transformed Attribute(s)
My XSLT module is:
But the result is undesirable when the original element (in this case, it is the <currency> element) already has a value: USD.
My module produces unintended result:
What is the solution to fix the problem?
Code: Select all
<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>
Code: Select all
<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>
1) new namespace
2) new root element
3) targeted transformed Attribute(s)
My XSLT module is:
Code: Select all
<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>
My module produces unintended result:
Code: Select all
<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>
-
- Posts: 388
- Joined: Thu Jul 01, 2004 12:29 pm
Re: Transform attribute to element when element already has a value
Hello,
Probably you can add a template special for the "currency" element, something like this:
Maybe you can try to ask this question on the XSLT list (xsl-list@lists.mulberrytech.com), there are more XSLT users to respond there.
Best Regards,
Octavian
Probably you can add a template special for the "currency" element, something like this:
Code: Select all
<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="*:currency">
<xsl:element name="{local-name()}" namespace="{$namespace}">
<xsl:value-of select="text()"/>
</xsl:element>
<xsl:apply-templates select="@*"/>
</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>
Best Regards,
Octavian
Octavian Nadolu
<oXygen/> XML Editor
http://www.oxygenxml.com
<oXygen/> XML Editor
http://www.oxygenxml.com
-
- Posts: 17
- Joined: Fri Aug 21, 2020 5:40 pm
Re: Transform attribute to element when element already has a value
Post by Fiona Chen »
Thank you, Octavian! This is very helpful!
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service