eoglasi
eoglasi

Reputation: 169

Special characters in XML with XSLT transformation

Can somebody help. I am renaming nodes but, loosing formatting. My XML is:

<?xml version="1.0" encoding="UTF-8"?>
<root>    
<demo></demo>
</root>

and i am transforming it with XSLT below. Output is always:

<?xml version="1.0" encoding="UTF-8"?>
<root>    
<description pstyle="description"></description>
</root>

But correct XML output need to be:

<?xml version="1.0" encoding="UTF-8"?>
<root>    
<description aid:pstyle="description"></description>
</root>

Is there a way that this would not happen with XSLT transformation?

My XSLT is:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no"/>
<xsl:template match="@* | node()">
            <xsl:copy>
                <xsl:apply-templates select="@* | node()"/>
            </xsl:copy>
    </xsl:template>
    <xsl:template match="demo">
<description aid:pstyle="description"><xsl:apply-templates select="@*|node()"/>
</description>
</xsl:template>
</xsl:stylesheet>

Thanks in advance for all your help.

Upvotes: 1

Views: 983

Answers (1)

kjhughes
kjhughes

Reputation: 111491

Assuming that you actually want well-formed XML output...

Your input XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>    
  <demo></demo>
</root>

Given to your XSLT modified to define the aid namespace prefix:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="demo">
    <description xmlns:aid="http://example.com/aid"
                 aid:pstyle="description">
      <xsl:apply-templates select="@*|node()"/>
    </description>
  </xsl:template>
</xsl:stylesheet>

Will produce this well-formed output XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>    
  <description xmlns:aid="http://example.com/aid" aid:pstyle="description"/>
</root>

Update per OP's comment:

This is working, but can i have output xml without xmlns:aid="example.com/aid"; in tag description?

Your comment along with your question title "Special characters in XML with XSLT transformation" suggest that you do not understand XML namespaces. The aid: characters before the pstyle attribute are not special characters. They are a namespace prefix. Namespaces are not required in XML, but if you're going to use a namespace prefix such as aid: you must define it (eg xmlns:aid="http://example.com/aid") for the document to be namespace-valid. For an explanation of namespace-valid and namespaces in general, see Namespaces in XML 1.0.

If you use the aid: prefix without defining it, the document will be not be namespace-valid. XSLT is capable of outputting a non-namespace-valid document, or even non-well-formed XML, but there's nearly never a legitimate reason to do so. Note that the definition could occur on root if that's more to your liking.

Upvotes: 4

Related Questions