Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

XML Transformation

Options
  • 20-06-2006 4:32pm
    #1
    Closed Accounts Posts: 36


    Hi,

    I am performing an xml transformation,from xml to xml.My problem is that the tags that are discarded from the input appear to show up as whitespace in the output.I am using http://javaalmanac.com/egs/javax.xml.transform/BasicXsl.html

    to perform the transformation and my xslt stylesheet is

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:output method="xml" encoding="iso-8859-1" indent="yes"/>
    <xsl:template match="top">
    <top>
    <xsl:apply-templates select="model"/>
    </top>
    </xsl:template>
    <xsl:template match="model">
    <model>
    <xsl:apply-templates select="instance"/>
    </model>
    </xsl:template>
    <xsl:template match="instance">
    <MO Type="{@type}&quot; Name="{@name}&quot;>
    <xsl:apply-templates select="attribute"/>
    <xsl:apply-templates select="instance"/>
    </MO>
    </xsl:template>
    <xsl:template match="attribute">
    <xsl:element name="{@name}&quot;>
    <xsl:value-of select="normalize-space(name)"/>
    <xsl:apply-templates select="datatype"/><!-- rules for other children -->
    </xsl:element>
    </xsl:template>
    <xsl:template match="datatype/enumRef">
    <xsl:element name="{@name}&quot;>
    <xsl:apply-templates select="value"/><!-- rules for other children -->
    </xsl:element>
    </xsl:template>
    <xsl:template match="value"><!--<xsl:apply-templates select = "enumRef"/>-->
    <xsl:value-of select="normalize-space(.)"/>
    </xsl:template>
    <xsl:template match="datatype/struct">
    <xsl:element name="{@name}&quot;>
    <xsl:apply-templates select="structMember"/><!-- rules for other children -->
    </xsl:element>
    </xsl:template>
    <xsl:template match="structMember">
    <xsl:element name="{@name}&quot;>
    <xsl:apply-templates/><!-- rules for other children -->
    </xsl:element>
    </xsl:template>
    </xsl:stylesheet>


    Thanks


Comments

  • Closed Accounts Posts: 80 ✭✭Torak


    <xsl:stylesheet>
    
    .....
    ....
    
    <xsl:template match="text()"/>
    
    </xsl:stylesheet>
    

    basically this tells it to ignore text nodes that are encountered!


  • Closed Accounts Posts: 36 pjfogarty


    I think my problem is misunderstood.
    Here is some sample output.

    <O Name="10479" Type="barry">
    <Id>

    10479

    </Id>
    <userLabel>

    10479

    </userLabel>
    <sac>

    10479

    </sac>
    </O>



    I want to get rid of the blank lines between the tags and the values


  • Closed Accounts Posts: 80 ✭✭Torak


    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        
        <xsl:output method="xml" encoding="iso-8859-1" indent="yes" />
        
        <xsl:strip-space elements="*"/>
        
        <xsl:template match="top">
            <top>
                <xsl:apply-templates select="model"/>
            </top>
        </xsl:template>
        
        <xsl:template match="model">
            <model>
                <xsl:apply-templates select="instance"/>
            </model>
        </xsl:template>
        
        <xsl:template match="instance">
            <MO Type="{@type}" Name="{@name}">
                <xsl:apply-templates select="attribute"/>
                <xsl:apply-templates select="instance"/>
            </MO>
        </xsl:template>
        
        <xsl:template match="attribute">
            <xsl:element name="{@name}">
                <xsl:value-of select="normalize-space(name)"/>
                <xsl:apply-templates select="datatype"/><!-- rules for other children -->
            </xsl:element>
        </xsl:template>
        
        <xsl:template match="datatype/enumRef">
            <xsl:element name="{@name}">
                <xsl:apply-templates select="value"/><!-- rules for other children -->
            </xsl:element>
        </xsl:template>
        
        <xsl:template match="value"><!--<xsl:apply-templates select = "enumRef"/>-->
            <xsl:value-of select="normalize-space(.)"/>
        </xsl:template>
        
        <xsl:template match="datatype/struct">
            <xsl:element name="{@name}">
                <xsl:apply-templates select="structMember"/><!-- rules for other children -->
            </xsl:element>
        </xsl:template>
        
        <xsl:template match="structMember">
            <xsl:element name="{@name}">
                <xsl:apply-templates/><!-- rules for other children -->
            </xsl:element>
        </xsl:template>
    </xsl:stylesheet>
    

    The important things to note are:

    1) It is formatted.
    2) <xsl:strip-space elements="*"/>


  • Closed Accounts Posts: 36 pjfogarty


    tags that had values which were empty as

    <attribute name = id>
    <value>

    </value>
    </attribute>

    would usually return <id> </id> ,but now shows up as <id/>


  • Closed Accounts Posts: 80 ✭✭Torak


    that's valid and correct xml...

    why is it a problem?


  • Advertisement
  • Closed Accounts Posts: 36 pjfogarty


    Its a problem because i need to keep empty tags so that they can be modified,as an empty tag is a valid tag.As tags can contain null values or empty strings.


  • Closed Accounts Posts: 80 ✭✭Torak


    pjfogarty wrote:
    Its a problem because i need to keep empty tags so that they can be modified,as an empty tag is a valid tag.As tags can contain null values or empty strings.

    how are you modifying it though?

    Are you using text manipulation or an xml parser?

    To an xml parser both should appear the same! If they are not then you need to get a new parser!

    I can only help if you give enough information tbh.

    If you run an xml file with <id/> and another with <id></id> through a SaxParser the values passed to the startElement method will be the same and nothing will be passed to the characters method implying that there is no content.

    If you are manipulating the text then all bets are off..


Advertisement