Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string value as XML tag name

Tags:

xslt

Below is my requirement. Can we do this using XSLT? I want to convert value of AttributeName as tag under policy and corresponding AttributeValue as value.

Input :

<Policy>
    <Attributes>
        <AttributeName>is_policy_loan</AttributeName>
        <AttributeValue>Yes</AttributeValue>
    </Attributes>
    <Attributes>
        <AttributeName>is_policy_owners</AttributeName>
        <AttributeValue>Yes</AttributeValue>
    </Attributes>       
    <Attributes>
        <AttributeName>is_policy_twoyears</AttributeName>
        <AttributeValue>Yes</AttributeValue>
    </Attributes>       
</Policy>

Output :

<Policy>
    <is_policy_loan>Yes</is_policy_loan>
    <is_policy_owners>Yes</is_policy_owners>
    <is_policy_twoyears>Yes</is_policy_twoyears>
</Policy>
like image 826
user2954062 Avatar asked Jun 19 '26 16:06

user2954062


1 Answers

The following xsl file will do the job:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!-- create the <AttributeName>AttributeValue</..> nodes -->
  <xsl:template match="//Attributes">
    <xsl:variable name="name" select="AttributeName" />
    <xsl:element name="{$name}">
      <xsl:value-of select="AttributeValue" />
    </xsl:element>
  </xsl:template>

  <!-- wrap nodes in a `Policy` node -->
  <xsl:template match="/">
    <Policy>
      <xsl:apply-templates/>
    </Policy>
  </xsl:template>
</xsl:stylesheet>
like image 93
hek2mgl Avatar answered Jun 23 '26 17:06

hek2mgl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!