Setting disable-output-escaping="yes" for every xsl:text tag in the xml

23,402

Solution 1

Thanks for clarifying the question. In this case, I'm fairly sure there's no need to disable output escaping. XSLT was designed to accomplish what you're doing:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/*">
 <display>

  <screen name="{name(.)}">

  <xsl:for-each select="logline_t">
     <xsl:variable name="nameContent">
        <xsl:for-each select="*">
          <xsl:if test="position() > 1"><xsl:text> </xsl:text></xsl:if>                 
          <xsl:value-of select="."/>
        </xsl:for-each>
     </xsl:variable>
     <field name="{$nameContent}" value="" type="label" />
  </xsl:for-each>

  </screen>

 </display>
</xsl:template>

</xsl:stylesheet>

I'm a bit unclear on this point: Note that I need the "<" inside the field name not to be escaped, this is why I can't use output method text.

Which < are you referring to? Is it the < and > around "hola"? If you left those unescaped you would wind up with invalid XML. It also looks like the name attribute in your sample output have a lot of values that aren't in the input XML. Where did those come from?

Solution 2

Given your expected output you don't need d-o-e at all for this. Here is a possible solution that doesn't use d-o-e, and is based on templates rather than for-each:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />

  <xsl:template match="/*">
    <display>
      <screen name="{name(.)}">
        <xsl:apply-templates select="logline_t"/>
      </screen>
    </display>
  </xsl:template>

  <xsl:template match="logline_t">
    <field value="" type="label">
      <xsl:attribute name="name">
        <xsl:apply-templates select="*" mode="fieldvalue"/>
      </xsl:attribute>
    </field>
  </xsl:template>

  <xsl:template match="*[last()]" mode="fieldvalue">
    <xsl:value-of select="." />
  </xsl:template>

  <xsl:template match="*" mode="fieldvalue">
    <xsl:value-of select="." />
    <xsl:text> </xsl:text>
  </xsl:template>

</xsl:stylesheet>

Solution 3

If you want to set d-o-e on everything, that suggests you are trying to generate markup "by hand". I don't think that's a particularly good idea (in fact, I think it's a lousy idea), but if it's what you want to do, I would suggest using the text output method instead of the xml output method. That way, no escaping of special characters takes place, and therefore it doesn't need to be disabled.

Share:
23,402
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    say I have the following xml:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="/*">
      <display>
        <xsl:for-each select="logline_t">
          <xsl:text disable-output-escaping="yes">&lt;</xsl:text> <xsl:value-of select="./line_1" <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
          <xsl:text disable-output-escaping="yes">&lt;</xsl:text> <xsl:value-of select="./line_2" <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
          <xsl:text disable-output-escaping="yes">&lt;</xsl:text> <xsl:value-of select="./line_3" <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
        </xsl:for-each>
      </display>
    </xsl:template>
    </xsl:stylesheet>
    

    Is there a way to set disable-output-escaping="yes" to all of the xsl:text that appear in the document?

    I know there is an option to put < xsl:output method="text"/ > and every time something like & lt; appears, a < will appear, but the thing is that sometimes in the values of line_1, line_2 or line_3, there is a "$lt;" that I don't want changed (this is, I only need whatever is between to be changed)

    This is what I'm trying to accomplish. I have this xml:

    <readlog_l>
     <logline_t>
      <hora>16:01:09</hora>
      <texto>Call-ID: 663903&lt;hola&gt;[email protected]</texto>
     </logline_t>
    </readlog_l>
    

    And this translation:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="/*">
     <display>
    
      &lt;screen name="<xsl:value-of select="name(.)"/>"&gt;
    
      <xsl:for-each select="logline_t">
      &lt; field name="<xsl:for-each select="*"><xsl:value-of select="."/></xsl:for-each>" value="" type="label"/&gt;
      </xsl:for-each>
    
      &lt;/screen&gt;
    
     </display>
    </xsl:template>
    
    </xsl:stylesheet>
    

    I want this to be the output:

    <?xml version="1.0"?>
    <display>
    
    <screen name="readlog_l">
    
      <field name="16:01:09 Call-ID: 663903&lt;hola&gt;[email protected] " value="" type="label">
    
    </screen>
    </display>
    

    Note that I need the "<" inside the field name not to be escaped, this is why I can't use output method text.

    Also, note that this is an example and the translations are much bigger, so this is why I'm trying to find out how not to write disable-output-escaping for every '<' or '>' I need.

    Thanks!

  • Admin
    Admin over 11 years
    Yes, I realized that using the text output method would make no escaping of special character to take place, but the thing is, it also makes not to escape to something gathered by a value-of tag (which I don't want it to happen). What I would need is a way of making not escaping everything that is between <xsl:text> tags, but nothing else.
  • Admin
    Admin over 11 years
    Sure,I'll add it to the main question.
  • Admin
    Admin over 11 years
    Stackoverflow escaped my intended writing. I was trying to write: Note that I need the "& lt;" inside the field name not to be escaped, this is why I can't use output method text.
  • JLRishe
    JLRishe over 11 years
    Ok, but the whole point of XSLT is to output XML, so if you just write them as XML tags, they'll be output as XML tags. That's what you want, right?
  • Admin
    Admin over 11 years
    Regarding your solution, it's almost perfect, since it keeps the unescaped symbols as I want them. The only issue is that it erases the spaces from $nameContent
  • JLRishe
    JLRishe over 11 years
    I think you mean it doesn't add spaces, right? :-) How about now?
  • Admin
    Admin over 11 years
    Mmh,I want to output an XML, but between " " I want the possibility of having an unescaped <. Perhaps this is not the intended purpose of XSLT, but I'm sure it's possible.
  • Admin
    Admin over 11 years
    It works! Thank you very much! Just so you can fix it, you have an extra / in the first screen tag. Thanks again!
  • JLRishe
    JLRishe over 11 years
    Indeed I do. Thanks. Fixed now.