1

I am unable to figure it out how to build below XML using XSLT 1.0

<values>
      <field name="abc"></field>
      <field name="nch"></field>
</values>

there should not be any space between elements start and end tag. Kindly help me as soon as possible.

Thanks.

2 Answers 2

2

In Saxon you have to change the output method to "html".

Example:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes"/>
  <xsl:template match="/">
    <values>
      <field name="abc"></field>
      <field name="nch"></field>
    </values>
  </xsl:template>
</xsl:stylesheet>

Here´s a workearound that works for vs2010.

Example 1:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/">
    <values>
      <field name="abc">
        <xsl:value-of select="substring-before(' ',' ')"/>
      </field>
      <field name="nch">
        <xsl:value-of select="substring-before(' ',' ')"/>
      </field>
    </values>
  </xsl:template>
</xsl:stylesheet>

Example 2:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/">
    <xsl:element name="values">
      <xsl:element name="filed">
        <xsl:attribute name="name">abc</xsl:attribute>
        <xsl:value-of select="substring-before(' ',' ')"/>
      </xsl:element>
      <xsl:element name="filed">
        <xsl:attribute name="name">nch</xsl:attribute>
        <xsl:value-of select="substring-before(' ',' ')"/>
      </xsl:element>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

Output:

<?xml version="1.0" encoding="utf-8"?>
<values>
  <filed name="abc"></filed>
  <filed name="nch"></filed>
</values>
Sign up to request clarification or add additional context in comments.

4 Comments

Not producing the wanted result with Saxon.
thanks for your response but none of these is working for me.It gives same out in both examples.
Yes you´r right. I have tested it with saxon and that´s not the same output as i had in vs2010. If you use saxon you have to change the output method to "html" instead of "xml".
thanks for your quick reply.but in my scenario i have to stick with output method to XML.i cant use HTML.so,just let me know if you have any workaround for the same.
0

The difference between <x></x> and <x/> is purely lexical and generally cannot be controlled by the XSLT processor, because the final output is performed by the Serializer.

With some XSLT processors (using their built-in serializers), it may be possible to output the full form of an empty element. However, with other processors, such as Saxon, this isn't (easily) possible.

2 Comments

i am using xalan xslt processor.is this possible in xalan?
@user1904426, Sorry, I haven't Xalan installed. You may try something like <xsl:copy><xsl:copy-of select="@*"/><xsl:value-of select="$someEmptyVariable"/></xsl:copy>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.