0

Based on one of the solutions provided on SO regarding serialization in XSLT: How to convert XML Node to String, I'm trying to serialize something like this.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xdb="http://xmlns.oracle.com/xdb" elementFormDefault="qualified">
    <xs:annotation>
        <xs:documentation>
            Package Name:    Master
            version:  2
            Description: 
                This is a dummy description used for testing purposes.
        </xs:documentation>
    </xs:annotation>
      <!-- ********************************************************************************************************************
    ***         Includes
    *************************************************************************************************************************-->
    <!-- Basic Layer -->
    <xs:include schemaLocation="dummy1.xsd"/>
    <xs:include schemaLocation="dummy2.xsd"/>
    <!-- ********************************************************************************************************************
    ***         Simple Types
    *************************************************************************************************************************-->
    <xs:simpleType name="YESNO">
        <xs:restriction base="xs:string">
            <xs:enumeration value="YES"/>
            <xs:enumeration value="NO"/>
        </xs:restriction>
    </xs:simpleType>
    <!-- ********************************************************************************************************************
    ***         Simple Types
    *************************************************************************************************************************-->
    <xs:complexType name="Custom" xdb:SQLType="L2A_CUSTOM_OT">
        <xs:sequence>
            <xs:element name="Postal" type="TPostal" />
            <xs:element name="Entry" type="Entry" minOccurs="0" maxOccurs="8" />
        </xs:sequence>
    </xs:complexType>    
</xs:schema>

As I'm just beginning with XSLT, I can't seem to find a solution that also includes namespaces and comments. Trying to include everything as-is without any loss of info, I came up with this XSLT.

<xsl:variable name="xsdresultstring">  
  <xsl:apply-templates select="$xsdresult" mode="serialize"/>
</xsl:variable>  

<!-- any element -->
<xsl:template match="*" mode="serialize">
    <xsl:text>&lt;</xsl:text>
    <xsl:value-of select="name()"/>
    <xsl:apply-templates select="@*" mode="serialize" />
    <xsl:choose>
        <xsl:when test="node()">
            <xsl:text>&gt;</xsl:text>
            <xsl:apply-templates mode="serialize" />
            <xsl:text>&lt;/</xsl:text>
            <xsl:value-of select="name()"/>
            <xsl:text>&gt;</xsl:text>
        </xsl:when>
        <xsl:otherwise>
            <xsl:text> /&gt;</xsl:text>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<!-- Attributes -->
<xsl:template match="@*" mode="serialize">
    <xsl:text> </xsl:text>
    <xsl:value-of select="name()"/>
    <xsl:text>="</xsl:text>
    <xsl:value-of select="."/>
    <xsl:text>"</xsl:text>
</xsl:template>

<xsl:template match="text()" mode="serialize">
    <xsl:value-of select="." />
</xsl:template>
Community
  • 1
  • 1
Pigna
  • 13
  • 1
  • 4
  • You ask for a serializer written in XSLT but do not say why. There high risk here of an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Please state your end goal. You show an XSD that is already serialized (obviously). If you want to serialize it again, why? If you want to process it in some manner and then re-serialize, use the identity transformation -- much simpler than writing a serializer in XSLT as it leverages the serializer already present in the XSLT processor. – kjhughes Apr 16 '16 at 13:30
  • I'll guess that you might actually want to produce a new XSD with the `xsd:includes` replaced by their referenced XSDs. If that's the case, what you're asking would be better described as *flattening* than *serializing*. But that's only a *guess*, which we shouldn't have to do. You should write your question more clearly so we *know* your goal. – kjhughes Apr 16 '16 at 13:32
  • Think I got myself into something way over my head. I try to enhance a some default xsd's and add some xdb:SQLType automatically. So fat so good. The end result should be loaded into a db but variable string size can not exceed 32000+ chars. So I need to split the xsd string in pieces. That part I also somewhat working but for it to work properly I need the length of the XSD I have in a var Try to create text like l_var1 := 'xsd piece1'; l_var2 := 'xsd piece2'; insert into tab( xsd ) values ( l_var1 || l_var2 ) Now looking for a way to get correct character length of xsd. – Pigna Apr 16 '16 at 14:26

1 Answers1

2

See http://lenzconsulting.com/xml-to-string/ for a pure XSLT 1.0 solution or consider whether you can use an XSLT processor like Saxon 9.7 that has XPath 3.0 support with https://www.w3.org/TR/xpath-functions-30/#func-serialize to make use of the serialize function.

Note that your approach as shown does not take care of escaping e.g. < as &lt; in attribute values or text nodes. So anyone needing to parse your result as XML will probably show up here asking how to force an XML parser to accept unescaped less than(&lt;) symbols or unescaped ampersand(&amp;) symbols.

zx485
  • 28,498
  • 28
  • 50
  • 59
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • This is a fine answer (+1) given OP's literal question, but do you really think someone *just beginning with XSLT* wants to know how to write a serializer in XSLT? – kjhughes Apr 16 '16 at 13:36