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 August 7th, 2006, 09:52 AM
Registered User
 
Join Date: Aug 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML to XML using XSLT

I have a repeting XMl input
                <ServiceAttributeList>
                    <ServiceAttribute>
                        <Name> xyz </Name>
                        <Value>123</Value>
                    </ServiceAttribute>
                    <ServiceAttribute>
                        <Name>abc</Name>
                        <Value>4566</Value>
                    </ServiceAttribute>
                </ServiceAttributeList>
                <ServiceAttributeList>
                .....
                </ServiceAttributeList>

I want to transorm it into

                <ServiceAttributeList>
                    <ServiceAttribute>
                        <xyz>
                        <Value>123</Value>
                        </xyz>
                    </ServiceAttribute>
                    <ServiceAttribute>
                        <abc>
                        <Value>4566</Value>
                        </abc>
                    </ServiceAttribute>
                </ServiceAttributeList>

                <ServiceAttributeList>
                ................
                </ServiceAttributeList>

what XSLT i have to write? any helps?


 
Old August 7th, 2006, 11:57 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Something like

<xsl:template match="ServiceAttribute">
  <xsl:element name="{Name}">
    <xsl:copy-of select="Value"/>


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old August 7th, 2006, 10:54 PM
Registered User
 
Join Date: Aug 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Buddy..But i need the 'copy of' input xml to out put with the changes I mentioned.
 
Old August 8th, 2006, 08:13 AM
Registered User
 
Join Date: Aug 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Output and input should look similar except for all Name value attributes.

Input
                <ServiceAttributeList>
                    <ServiceAttribute>
                        [u]<Name> xyz </Name></u>
                        [u]<Value>123</Value></u>
                    </ServiceAttribute>
                    <ServiceAttribute>
                        [u]<Name>abc</Name></u>
                        [u]<Value>4566</Value></u>
                    </ServiceAttribute>
                </ServiceAttributeList>
                <ServiceAttributeList>
                .....
                </ServiceAttributeList>


output
                <ServiceAttributeList>
                    <ServiceAttribute>
                        [u]<xyz></u>
                        [u]<Value>123</Value></u>
                        [u]</xyz></u>
                    </ServiceAttribute>
                    <ServiceAttribute>
                        [u]<abc></u>
                        [u]<Value>4566</Value></u>
                        [u]</abc></u>
                    </ServiceAttribute>
                </ServiceAttributeList>

                <ServiceAttributeList>
                ................
                </ServiceAttributeList>

Thanks
 
Old August 9th, 2006, 12:16 AM
Registered User
 
Join Date: Aug 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Any Help on this?

 
Old August 9th, 2006, 02:47 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I think Michael showed the solution but if you need more help you need to give us an accurate representation of your input XML. The example you gave is not well-formed as it has no document element.

Personally I think the first XML is a better representation. It's easier to manipulate and also can you guarantee that the text of the Name element will form a legitimate XML element name?

--

Joe (Microsoft MVP - XML)
 
Old August 9th, 2006, 04:09 AM
Registered User
 
Join Date: Aug 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It will alomost look like one below

<?xml version="1.0" encoding="UTF-8"?>
<SPRequestMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <SPRequestSequence>
        <SPRequest>
            <SPRequestID>1</SPRequestID>
            <Action>Create</Action>
            <ServiceType>Create-VoIP-Service</ServiceType>
            <LocationID/>
            <ServiceAttributeList>
                <ServiceAttribute>
                    <Name>voipId</Name>
                    <Value>123</Value>
                </ServiceAttribute>
                <ServiceAttribute>
                    <Name>subsId</Name>
                    <Value>1235</Value>
                </ServiceAttribute>
            </ServiceAttributeList>
        </SPRequest>
    </SPRequestSequence>
</SPRequestMessage>

and the output will be like

<?xml version="1.0" encoding="UTF-8"?>
<SPRequestMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <SPRequestSequence>
        <SPRequest>
            <SPRequestID>1</SPRequestID>
            <Action>Create</Action>
            <ServiceType>Create-VoIP-Service</ServiceType>
            <LocationID/>
            <ServiceAttributeList>
                <ServiceAttribute>
                    <voipId>
                    <Value>123</Value>
                    </voipId>
                </ServiceAttribute>
                <ServiceAttribute>
                    <subsId>
                    <Value>1235</Value>
                    </subsId>
                </ServiceAttribute>
            </ServiceAttributeList>
        </SPRequest>
    </SPRequestSequence>
</SPRequestMessage>


 
Old August 9th, 2006, 07:59 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Okay, you start with the identity template:
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
and then add the template Michael showed to form the new style ServiceAttribute element:
Code:
  <xsl:template match="ServiceAttribute">
    <ServiceAttribute>
      <xsl:element name="{Name}">
        <xsl:copy-of select="Value"/>
      </xsl:element>
    </ServiceAttribute>
  </xsl:template>

This gives the full stylesheet as:
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="ServiceAttribute">
    <ServiceAttribute>
      <xsl:element name="{Name}">
        <xsl:copy-of select="Value"/>
      </xsl:element>
    </ServiceAttribute>
  </xsl:template>
</xsl:stylesheet>
--

Joe (Microsoft MVP - XML)
 
Old August 9th, 2006, 08:44 AM
Registered User
 
Join Date: Aug 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks very much
It is working well.
Merci encore.
 
Old July 17th, 2013, 09:11 AM
Registered User
 
Join Date: Jun 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Substring value Processing in recursion

This is all about reverse a string. It works properly for the given Value 'ABCDEF'. The output is also correct 'FEDCBA'. But I want to know how this is printing letters 'A' and 'D' in this string. Could anyone help me to understand this? please. Elaborate this working method.

Code:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
<xsl:output method="html"/>
<xsl:template name="reverse">
<xsl:param name="input" select="'ABCDEF'"/>
<xsl:variable name="len" select="string-length($input)"/>
<xsl:choose>
<xsl:when test="$len &lt; 2">
<xsl:value-of select="$input"/>
</xsl:when>
<xsl:when test="$len = 2">
<xsl:value-of select="substring($input,2,1)"/>
<xsl:value-of select="substring($input,1,1)"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="mid" select="floor($len div 2)"/>
<xsl:call-template name="reverse">
<xsl:with-param name="input" select="substring($input,$mid+1,$mid+1)"/>
</xsl:call-template>
<xsl:call-template name="reverse">
<xsl:with-param name="input" select="substring($input,1,$mid)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="/">
<xsl:call-template name="reverse">
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>





Similar Threads
Thread Thread Starter Forum Replies Last Post
xml and xsl templates as input to xslt gives xml rameshnarayan XSLT 5 August 3rd, 2005 01:58 AM
XSLT read through XML to transform another XML dendenx2 XSLT 8 July 7th, 2005 08:18 PM
XSLT for complicated xml to xml transf. required doug@sirvisetti XSLT 3 June 17th, 2005 04:26 PM
merge two xml file and make new xml using xslt ketan XSLT 0 September 21st, 2004 08:48 AM
Merge XML files into a xml file using xslt lxu XML 4 November 6th, 2003 06:01 PM





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