Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Viewing and editing XML
Message
De
23/12/2021 03:09:43
 
 
Information générale
Forum:
Open Source
Catégorie:
Source
Divers
Thread ID:
01683043
Message ID:
01683067
Vues:
36
>>Naomi, just to make sure I understood correctly: you're looking for a way to prettify an XML document?
>
>I want to be able to see it not as huge big chunk of data, but normally with the tree structure (and have an ability to change something when looking at it this way).
>
>Also, files may be huge (~900KB or more)

Naomi, you can transform the XML using a stylesheet transformation (that's what Altova does, probably). The size may pose a problem for a MSXML2 parser, but the transformation can be used with a more capable processor (like .NET's).

In VFP (but, remember, the transformation can be used elsewhere):
LOCAL XML AS MSXML2.DOMDocument60
LOCAL XSLT AS MSXML2.DOMDocument60
LOCAL XMLPrettifier AS String
LOCAL Prettified AS String

m.XML = CREATEOBJECT("MSXML2.DOMDocument.6.0")
m.XSLT = CREATEOBJECT("MSXML2.DOMDocument.6.0")

m.XML.LoadXML("<root><child><grandchild>Vera</grandchild><grandchild>Chuck</grandchild><grandchild>Dave</grandchild></child></root>")

TEXT TO m.XMLPrettifier NOSHOW FLAGS 1
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" version="1.0">

    <xsl:output encoding="utf-8" method="text" indent="no"/>

    <xsl:variable name="spaces" select="'  '" />
    <xsl:variable name="NL" select="'&#13;&#10;'" />

    <xsl:template match="/">
        <xsl:apply-templates select="node()">
            <xsl:with-param name="indent" select="''"/>
        </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="node()">
        <xsl:param name="indent"/>

        <xsl:value-of select="$indent"/>

        <xsl:text>&lt;</xsl:text><xsl:value-of select="name(.)"/><xsl:apply-templates select="@*"/>

            <xsl:if test="not(node())"><xsl:text> /</xsl:text></xsl:if>
        <xsl:text>&gt;</xsl:text>

        <xsl:if test="node()">

            <xsl:if test="node()[node()]">
              <xsl:value-of select="$NL" />
            </xsl:if>

            <xsl:apply-templates>
                <xsl:with-param name="indent" select="concat($indent, $spaces)"/>
            </xsl:apply-templates>


            <xsl:if test="node()[node()]">
                <xsl:value-of select="$indent"/>
            </xsl:if>

            <xsl:text>&lt;/</xsl:text><xsl:value-of select="name(.)"/><xsl:text>&gt;</xsl:text>
        </xsl:if>

        <xsl:value-of select="$NL" />

    </xsl:template>

    <xsl:template match="@*">
        <xsl:text> </xsl:text>
        <xsl:value-of select="name(.)"/>
        <xsl:text>=</xsl:text>
        <xsl:value-of select="concat('&quot;', ., '&quot;')"/>
    </xsl:template>

    <xsl:template match="text()">
        <xsl:value-of select="normalize-space(.)"/>
    </xsl:template>

    <xsl:template match="comment()">
        <xsl:text>&lt;--</xsl:text><xsl:value-of select="."/><xsl:text>--&gt;</xsl:text>
    </xsl:template>

    <xsl:template match="processing-instruction()">
        <xsl:text>&lt;?</xsl:text><xsl:value-of select="name(.)"/><xsl:text> </xsl:text><xsl:value-of select="."/><xsl:text>?&gt;</xsl:text>
        <xsl:value-of select="$NL" />
    </xsl:template>
</xsl:stylesheet>
ENDTEXT

m.XSLT.LoadXML(m.XMLPrettifier)

m.Prettified = m.XML.Transformnode(m.XSLT)

? m.Prettified
Credits: transformation stylesheet from https://gist.github.com/Jalalhejazi/5550511, with small changes.
----------------------------------
António Tavares Lopes
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform