dcsimg
www.webdeveloper.com
Results 1 to 2 of 2

Thread: Moving attribute name

  1. #1
    Join Date
    Nov 2012
    Posts
    2

    Moving attribute name

    Hi!
    I'm working on a big XML document that I need to change slightly. I think it can be done with XSLT but I can't figure out how. The document is full of tags like these:
    HTML Code:
    <verse value="1" chapter="1">...some text...</verse>
    I need to extract the numbers of the 'value' and 'chapter' attributes to add them in the text and style them separately. Something like this:

    HTML Code:
    <verse>
    <chapter>1</chapter>
    <value>1</value>
    ...some text...
    </verse>
    Can this be done with XSLT or is some other script solution better? I don't know XSLT and I tried to learn it just to solve this problem but I'm not doing very well. This is my attempt so far:

    HTML Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    
    <xsl:template match="verse[@value]">
    	<xsl:value-of select="verse[@value]"/><xsl:apply-templates/>
    </xsl:template>
    
    </xsl:stylesheet>

  2. #2
    Join Date
    Nov 2012
    Posts
    2
    I've made some progress and this is the code I have now:

    HTML Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="/">
        <book>
    	<xsl:apply-templates/>
        </book>
    </xsl:template>
    
    <xsl:template match="verse">
        <xsl:if test="@value = 1">
        	<chapter-number><xsl:value-of select="@chapter"/></chapter-number>
        </xsl:if>
        <verse-number><xsl:value-of select="@value"/></verse-number>
        <xsl:apply-templates/>
    </xsl:template>
    
    </xsl:stylesheet>
    It does the job but the problem is that it removes all tags other than ones explicitly written in my code. How can I keep all the tags from the original file and just rebuild the <verse> tags according to my code?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles