Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 7th, 2007, 01:20 PM
Registered User
 
Join Date: Mar 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Attribute to Child element Conversion

Hi,

I am working with XSLT.
My job will be to convert the value of each element as a child element.Also to convert each Attribute of Each element as child elemnets.

e.g.

source xml:

<Policy st="1">Active</Policy>

target xml:

<Policyst>1</Policyst>
<PolicyValue>Active</PolicyValue>

For the entire xml file I need to convert like that.

Please Help me.Thanks in advance.

Bye
kudzuconf

 
Old July 7th, 2007, 02:21 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Perhaps you can show a more complete example. I can't tell from your description what you would want to do with elements that have element children, elements with mixed content, elements whose name is in a namespace, etc.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 9th, 2007, 06:07 AM
Registered User
 
Join Date: Mar 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have an XML file :

<?xml version="1.0" encoding="ISO-8859-1"?>

<PolicyInfo>
    <Policy Status="1">Active</Policy>
    <Annuitant Status="-1">Active</Annuitant>
    <Role Status="0">Not Active</Role>
    <Agent Status="1">Dead</Agent>
</PolicyInfo>

XSL file I am using :

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   version="1.0">
  <xsl:output indent="yes" />
  <xsl:strip-space elements="*" />
  <xsl:template match="*">
    <xsl:copy>

      <xsl:if test="@*">
        <xsl:element name="{current()}">
          <xsl:value-of select="current()" />
        </xsl:element>
        <xsl:for-each select="@*">
          <xsl:element name="{name()}">
            <xsl:value-of select="." />
          </xsl:element>
        </xsl:for-each>
    </xsl:if>
      <xsl:apply-templates />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

O/P generated:

<?xml version="1.0" encoding="utf-8"?>
<PolicyInfo>
  <Policy>
    <Status>1</Status>Active</Policy>
  <Annuitant>
    <Status>-1</Status>Active</Annuitant>
  <Role>
    <Status>0</Status>Not Active</Role>
  <Agent>
    <Status>1</Status>Dead</Agent>
</PolicyInfo>

Requird O/P:

<?xml version="1.0" encoding="utf-8"?>
<PolicyInfo>
    <Policy>
            <PolicyStatus>1</PolicyStatus>
        <StstusValue>Active</StstusValue>
    </Policy>
    <Annuitant>
            <AnnuitantStatus>-1</AnnuitantStatus>
        <AnnuitantValue>Active</AnnuitantValue>
    </Annuitant>
      <Role>
            <RoleStatus>0</RoleStatus>
        <RoleValue>Not Active</RoleValue>
    </Role>
     <Agent>
            <AgentStatus>1</AgentStatus>
        <AgentValue>Dead</AgentValue>
    </Agent>
</PolicyInfo>

I need dynamically transforming the xml file.Node names should not be hard coded.Because the actual xml I will be using is a big one.

Thanks,
kudzuconf

 
Old July 9th, 2007, 06:25 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Add a template rule for text nodes:

<xsl:template match="text()">
  <xsl:element name="{name(..)}Value">
    <xsl:value-of select="."/>
  </xsl:element>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 9th, 2007, 08:18 AM
Registered User
 
Join Date: Mar 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It seems You are a friend indeed...Thanks for the guidance.I am your FAN

 
Old July 13th, 2007, 01:06 AM
Registered User
 
Join Date: Mar 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I need to add a restriction in the previous
.xsl file.

Example:

XML File:

<?xml version="1.0" encoding="utf-8"?>

<PolicyInfo>
    <Policy Status="1">Active</Policy>
    <Annuitant Code="-1">Active</Annuitant>
    <Role Number="0">Not Active</Role>
    <Agent Status="1">Dead</Agent>
    <Dealer>NewDealer</Dealer>
</PolicyInfo>

XSL File:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   version="1.0">
  <xsl:output indent="yes" />
  <xsl:strip-space elements="*" />

  <xsl:template match="*">
    <xsl:copy>
      <xsl:if test="@*">
        <xsl:for-each select="@*">
          <xsl:element name="{concat(name(..),name())}">
            <xsl:value-of select="." />
          </xsl:element>
         </xsl:for-each>
     </xsl:if>
      <xsl:apply-templates />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="text()">
     <xsl:element name="{name(..)}Value">
             <xsl:value-of select="."/>
     </xsl:element>
  </xsl:template>
</xsl:stylesheet>


O/P:

<?xml version="1.0" encoding="utf-8"?>
<PolicyInfo>
    <Policy>
            <PolicyStatus>1</PolicyStatus>
        <StstusValue>Active</StstusValue>
    </Policy>
    <Annuitant>
            <AnnuitantStatus>-1</AnnuitantStatus>
        <AnnuitantValue>Active</AnnuitantValue>
    </Annuitant>
      <Role>
            <RoleStatus>0</RoleStatus>
        <RoleValue>Not Active</RoleValue>
    </Role>
     <Agent>
            <AgentStatus>1</AgentStatus>
        <AgentValue>Dead</AgentValue>
    </Agent>
     <Dealer>
    <DealerValue>NewDealer</Dealer>
     </Dealer>
</PolicyInfo>

I need the text nodes having attribute only be formatted,
the text nodes without any attribute must remain same.

Required O/P:

<?xml version="1.0" encoding="utf-8"?>
<PolicyInfo>
    <Policy>
        <PolicyStatus>1</PolicyStatus>
        <StstusValue>Active</StstusValue>
    </Policy>
    <Annuitant>
        <AnnuitantStatus>-1</AnnuitantStatus>
        <AnnuitantValue>Active</AnnuitantValue>
    </Annuitant>
      <Role>
        <RoleStatus>0</RoleStatus>
        <RoleValue>Not Active</RoleValue>
    </Role>
     <Agent>
        <AgentStatus>1</AgentStatus>
        <AgentValue>Dead</AgentValue>
    </Agent>
     <Dealer>NewDealer</Dealer>
</PolicyInfo>


Thanks in advance for any advice.

Bye
Kudzuconf


 
Old July 13th, 2007, 04:02 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Add the rule

<xsl:template match="*[not(@*|*)]">
  <xsl:copy-of select="."/>
</xsl:template>

ie if an element has no attributes or element children, copy it unchanged.

Looking at this code:

      <xsl:if test="@*">
        <xsl:for-each select="@*">
          <xsl:element name="{concat(name(..),name())}">
            <xsl:value-of select="." />
          </xsl:element>
         </xsl:for-each>
     </xsl:if>

the xsl:if is unnecessary since if there are no attributes the for-each will do nothing.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding the first child element humansky XSLT 5 April 17th, 2008 08:46 AM
How to add an attribute to each element bartnowa XSLT 3 November 8th, 2007 12:12 PM
child element kgoldvas XML 4 April 27th, 2006 01:51 AM
Attribute for first element only mehdi62b XML 1 January 2nd, 2006 06:26 AM
Match element based on attribute of child? transom XSLT 1 September 11th, 2005 03:26 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.