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 February 8th, 2011, 12:50 PM
Authorized User
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default Swapping values between Element Name and documentation

Here is an XML
INPUT
------------------
<xsd:element name="SAPIdocRelease" minOccurs="0">
<xsd:annotation>
<xsd:documentation>DOCREL</xsd:documentation>
</xsd:annotation>
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="4"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>

OUTPUT
--------------

<xsd:element name="DOCREL" minOccurs="0">
<xsd:annotation>
<xsd:documentation>SAPIdocRelease</xsd:documentation>
</xsd:annotation>
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="4"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>

"DOCREL" needs to swapped wth "SAPIdocRelease" in generic way.

I have so many fields like that in one file. Doing manually taking longer than expected with cut & paste. Is there a generic way to swap those 2 fields with the exact same structure?

Appreciated in advance
 
Old February 8th, 2011, 12:53 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Generic way?

What have you tried so far?
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old February 8th, 2011, 12:56 PM
Authorized User
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I need to work on different names in file. So i like to have something with standard xslt conversion with out names of the values mentioned.

I just started working on it with no idea on how to.
 
Old February 8th, 2011, 01:00 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  version="1.0">

  <xsl:template match="@* | node()">
    <xsl:copy>
       <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="xsd:element/@name">
    <xsl:attribute name="{name()}">
       <xsl:value-of select="../xsd:annotation/xsd:documentation"/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="xsd:element/xsd:annotation/xsd:documentation">
    <xsl:copy>
       <xsl:value-of select="../../@name"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
Untested but should give you an idea.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old February 8th, 2011, 01:09 PM
Authorized User
 
Join Date: Apr 2010
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You are genius Martin. Works like a charm. Thanks a bunch





Similar Threads
Thread Thread Starter Forum Replies Last Post
Defining restrictions based on element/attribute values aldwinenriquez XML 3 May 13th, 2009 03:55 AM
XML element with delimited values and XSLT Clark Kent XSLT 15 May 7th, 2008 10:10 AM
xml element values vakorde XSLT 3 May 2nd, 2006 04:36 AM
Swapping Images software_developer_kk Javascript How-To 1 April 13th, 2005 10:00 AM
Update a table with XML element values alavastros XML 1 July 16th, 2004 11:36 PM





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