Xml: Difference between revisions

From miki
Jump to navigation Jump to search
(Created page with "* XSLT")
 
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Links ==
* [[XSLT]]
* [[XSLT]]
* [https://www.w3.org/TR/REC-xml/#charsets XML Charset] — If you are not sure about XML being by far the shittiest standard ever, then looking at this page will remove any doubt.

== Entity ==
<source lang="xml">
<!ENTITY cr '<xsl:text xmlns:xsl="http://www.w3.org/1999/XSL/Transform">&#13;</xsl:text>'>
<!ENTITY space '<xsl:text xmlns:xsl="http://www.w3.org/1999/XSL/Transform">&#32;</xsl:text>'>
<!ENTITY tab '<xsl:text xmlns:xsl="http://www.w3.org/1999/XSL/Transform">&#9;</xsl:text>'>
</source>

== Tips ==
=== Format XML document ===
Using '''xmllint''' from package {{deb|libxml2-utils}} [https://stackoverflow.com/questions/16090869/how-to-pretty-print-xml-from-the-command-line]:

<source lang="bash">
xmllint --format doc.xml
XMLLINT_INDENT=" " xmllint --format doc.xml
XMLLINT_INDENT=$'\t' xmllint --format doc.xml | sponge doc.xml
</source>

Using '''xsltproc''' and XSLT stylesheet. This requires an intermediate XSLT document:
<source lang="bash">
cat > indent.xslt << __HERE__
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:transform>
__HERE__
xsltproc indent.xslt doc.xml
</source>

=== Use both single and double quotes in attributes ===

From the XML specification [https://stackoverflow.com/questions/3961505/how-can-i-escape-double-quotes-in-xml-attributes-values]:
To allow attribute values to contain both single and double quotes, the apostrophe or
single-quote character (') may be represented as "&amp;apos;", and the double-quote
character (") as "&amp;quot;".

<source lang="xml">
<element attr="quote &quot; apostrophe &apos;">Foo</element>
</source>

Latest revision as of 19:09, 17 February 2022

Links

  • XSLT
  • XML Charset — If you are not sure about XML being by far the shittiest standard ever, then looking at this page will remove any doubt.

Entity

<!ENTITY cr '<xsl:text xmlns:xsl="http://www.w3.org/1999/XSL/Transform">&#13;</xsl:text>'>
<!ENTITY space '<xsl:text xmlns:xsl="http://www.w3.org/1999/XSL/Transform">&#32;</xsl:text>'>
<!ENTITY tab '<xsl:text xmlns:xsl="http://www.w3.org/1999/XSL/Transform">&#9;</xsl:text>'>

Tips

Format XML document

Using xmllint from package libxml2-utils [1]:

xmllint --format doc.xml
XMLLINT_INDENT="    " xmllint --format doc.xml
XMLLINT_INDENT=$'\t'  xmllint --format doc.xml | sponge doc.xml

Using xsltproc and XSLT stylesheet. This requires an intermediate XSLT document:

cat > indent.xslt << __HERE__
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml"  encoding="UTF-8" indent="yes" />
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:transform>
__HERE__
xsltproc indent.xslt doc.xml

Use both single and double quotes in attributes

From the XML specification [2]:

 To allow attribute values to contain both single and double quotes, the apostrophe or 
 single-quote character (') may be represented as "&apos;", and the double-quote 
 character (") as "&quot;".
  <element attr="quote &quot; apostrophe &apos;">Foo</element>