XSLT - How to handle apostrophe and quote in XSLT

The XML sample

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <textarea name="123'_a">apostrophe: 123'_a</textarea>
  <textarea name="123">b</textarea>
  <textarea name="123">a</textarea>
  <textarea name="123'_&quot;_a">apostrophe and quote: 123'_&quot;_a</textarea>
  <textarea name="123345">c</textarea>
  <textarea name="555">a</textarea>    
</root>

The XSL codes:

<?xml version="1.0" encoding="UTF-8"?>
<!--
Below are examples showing how to handle apostrophe and quote in XSLT
-->

XSLT - Remove Namespace

<?xml version="1.0" encoding="UTF-8"?>
<!-- Remove Namespace -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="*">
      <!-- remove element prefix (if any) -->
      <xsl:element name="{local-name()}">
        <!-- process attributes -->
        <xsl:for-each select="@*">
          <!-- remove attribute prefix (if any) -->
          <xsl:attribute name="{local-name()}">
            <xsl:value-of select="."/>

XSLT - Remove Element Without Child Nodes

<?xml version="1.0" encoding="UTF-8"?>
<!-- Remove Element Without Child Nodes -->
<!-- Only the 1st level is removed. -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="*">
        <xsl:if test="count(@*) &gt; 0 or count(node()) &gt; 0">
            <xsl:copy>
                <xsl:apply-templates select="@* | node()"/>
            </xsl:copy>
        </xsl:if>
    </xsl:template>
    <xsl:template match="@* | text()">

XSLT - Remove attributes or / and nodes

<?xml version="1.0" encoding="UTF-8"?>
<!-- Remove unwanted attributes or/and nodes -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
 
    <!-- Copy everything -->
    <xsl:template match="@*|node()|text()|comment()|processing-instruction()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()|text()|comment()|processing-instruction()"/>
        </xsl:copy>
    </xsl:template>
 

XSLT - Loops

Decrement Looping

<?xml version="1.0" encoding="UTF-8"?>
<!-- Decrement Looping: Recursively decrementing specified value down to 1. -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:output method="text" encoding="UTF-8"/>
 
    <xsl:template match="/">
        <!-- Calling DecrementLooping template -->
        <xsl:call-template name="DecrementLooping">
          <xsl:with-param name="i" select="9"/>
        </xsl:call-template>
     </xsl:template>

XSLT - Indent content of XML file

<?xml version="1.0" encoding="UTF-8"?>
<!--
Description: Indent the content of the XML file.
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <xsl:output method="xml" encoding="UTF-8"/><!--Instruct how to output-->
 
  <xsl:param name="indent-increment" select="'   '"/>
  <xsl:template match="*">
    <xsl:param name="indent" select="'&#xA;'"/>
    <xsl:value-of select="$indent"/>
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates>

XSLT - Copy XML file without comments

<?xml version="1.0" encoding="UTF-8"?>
<!-- 

XSLT - Add / Change element name

Change element name

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
  Change element name:
    -While copying everything:
      -Catch the element you want to change.
      -Reconstruct the element you catched under another name.
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
 
    <!-- Copy everything -->
    <!-- ############# -->
    <xsl:template match="@*|node()">
      <xsl:copy>

XSLT - Sample Template Code

Sample XSLT Template Code

XSLT - Removing leading zeros

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="text" encoding="UTF-8" />
  <!-- 
    Usage example: Removing leading zeros 
  -->
  <xsl:template match="*">
    <xsl:param name="RemoveLeadingZerosResult">
      <xsl:call-template name="RemoveLeadingZeros">
        <xsl:with-param name="sTmp" select="00001"/>
      </xsl:call-template>   
    </xsl:param>
    <xsl:value-of select="$RemoveLeadingZerosResult"/><!-- Display the result -->
  </xsl:template>
 
  <!-- 

Pages

Subscribe to OpenWritings.net RSS