最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

xml - How to translate special characters using unicode and display & correctly in the same Choose block - Stack Overflo

programmeradmin2浏览0评论

I am trying to translate names to display correctly in a database using xsl version 1.0 logic that have special characters, specifically umlauts. I have been able to get ö, ä, and ü to be translated to o,a,u respectively when they are part of the value being included under the field mapped in the xml.

I've been able to get that to work correctly with the first xsl block below but the Ampersand actually displays as &amp instead of & and it's only for a single name. How can I modify these two xsl examples into one so that both transformations display how intended. Everything I've tried to combine the two cases results in an xsl compile error.

End Goal: Have all names display correctly with replaced/translated values of o,u,a in place of umlauts and still display & correctly, not just for a single office value like Test & Welcome.

ex."Test & Welcöme" should display as "Test & Welcome"

  1. Works without xsl compile error but displays &amp instead of & Test results: Test & Welcome
  <xsl:when test="ROF_NAME='Test &amp; Welcme'">
     <xsl:text>Test &amp; Welcome</xsl:text><xsl:text>!~</xsl:text>
  </xsl:when>   
  <xsl:otherwise>
    <xsl:value-of select="substring(ROF_NAME,1,149)" disable-output-escaping="yes"/><xsl:text>!~</xsl:text>
  </xsl:otherwise>
</xsl:choose><xsl:text>!~</xsl:text>```


 2. Uses unicode for umlaut values and no compile error but isn't actually translating the umlauts into plain characters but the ampersand does display correctly: 

 Output results: Test & Welcme

    ```<xsl:choose>
      <xsl:when test="normalize-space(ROF_NAME) != ''">
        <xsl:value-of select="substring(translate(ROF_NAME, '&#246;&#228;&#252;', 'oau'), 1, 149)" disable-output-escaping="yes"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text> </xsl:text>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:text>!~</xsl:text>```

3.I also tried this but it gives an xslt compile error: 

    ```<xsl:choose>
        <xsl:when test="normalize-space(ROF_NAME) != ''"> 
            <xsl:value-of select="substring(translate(ROF_NAME, 'öäü', 'oau'), 1, 149)" disable-output-escaping="yes"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:text> </xsl:text>
        </xsl:otherwise>
    </xsl:choose>
    <xsl:text>!~</xsl:text>```


OVERALL I think 2 is the closest to what I want but it doesn't display the translated value  

I am trying to translate names to display correctly in a database using xsl version 1.0 logic that have special characters, specifically umlauts. I have been able to get ö, ä, and ü to be translated to o,a,u respectively when they are part of the value being included under the field mapped in the xml.

I've been able to get that to work correctly with the first xsl block below but the Ampersand actually displays as &amp instead of & and it's only for a single name. How can I modify these two xsl examples into one so that both transformations display how intended. Everything I've tried to combine the two cases results in an xsl compile error.

End Goal: Have all names display correctly with replaced/translated values of o,u,a in place of umlauts and still display & correctly, not just for a single office value like Test & Welcome.

ex."Test & Welcöme" should display as "Test & Welcome"

  1. Works without xsl compile error but displays &amp instead of & Test results: Test & Welcome
  <xsl:when test="ROF_NAME='Test &amp; Welcme'">
     <xsl:text>Test &amp; Welcome</xsl:text><xsl:text>!~</xsl:text>
  </xsl:when>   
  <xsl:otherwise>
    <xsl:value-of select="substring(ROF_NAME,1,149)" disable-output-escaping="yes"/><xsl:text>!~</xsl:text>
  </xsl:otherwise>
</xsl:choose><xsl:text>!~</xsl:text>```


 2. Uses unicode for umlaut values and no compile error but isn't actually translating the umlauts into plain characters but the ampersand does display correctly: 

 Output results: Test & Welcme

    ```<xsl:choose>
      <xsl:when test="normalize-space(ROF_NAME) != ''">
        <xsl:value-of select="substring(translate(ROF_NAME, '&#246;&#228;&#252;', 'oau'), 1, 149)" disable-output-escaping="yes"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text> </xsl:text>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:text>!~</xsl:text>```

3.I also tried this but it gives an xslt compile error: 

    ```<xsl:choose>
        <xsl:when test="normalize-space(ROF_NAME) != ''"> 
            <xsl:value-of select="substring(translate(ROF_NAME, 'öäü', 'oau'), 1, 149)" disable-output-escaping="yes"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:text> </xsl:text>
        </xsl:otherwise>
    </xsl:choose>
    <xsl:text>!~</xsl:text>```


OVERALL I think 2 is the closest to what I want but it doesn't display the translated value  
Share Improve this question asked Mar 17 at 14:54 flea02flea02 91 bronze badge 16
  • What is the target output method, text or xml or html? Is the XSLT processor in charge of serialization? What are you trying to achieve with disable-output-escaping="yes"? – Martin Honnen Commented Mar 17 at 15:10
  • 2 Please provide a minimal reproducible example showing an example of the input, your current stylesheet (as executable code, not snippets taken out of context) and the expected output. – michael.hor257k Commented Mar 17 at 15:27
  • xml is the target output and I'm reviewing a .dat file that will be inserted into the db using a downloader.exe and yes the xslt processor is in charge of serialization I am understanding the process correctly. If anything in the xsl is out of order things go awry and are out of order in the DB. For your third question, disable-output-escaping="yes" is being used to ensure that certain characters (like the ampersand) are not escaped so they display as is. To be honest this is a bit complex for me; I tried running it through AI for assistance but nothing has quite worked exactly as I want yet. – flea02 Commented Mar 17 at 15:31
  • 1 Are you quite sure about the requirement? It's normal in German to substitute ö, ä, ü as oe, ae, ue when umlauts aren't available; substituting "o", "a", "e" would normally be considered incorrect. – Michael Kay Commented Mar 17 at 15:37
  • 1 If your output is XML then an ampersand MUST be escaped. And if your input is XML (as it must be with XSLT 1.0) then it cannot contain "Test & Welcöme". – michael.hor257k Commented Mar 17 at 15:44
 |  Show 11 more comments

1 Answer 1

Reset to default 0

End Goal: Have all names display correctly with replaced/translated values of o,u,a in place of umlauts and still display & correctly

Here is a simple example:

XML input:

<root>
    <name>Test</name>
    <name>Test &amp; Welcome</name>
    <name>Welcöme</name>
    <name>Test &amp; Welcöme</name>
</root>

XSLT:

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

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="name">
    <xsl:copy>
        <xsl:value-of select="translate(., 'öäü', 'oau')"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

The result here will be:

<?xml version="1.0"?>
<root>
    <name>Test</name>
    <name>Test &amp; Welcome</name>
    <name>Welcome</name>
    <name>Test &amp; Welcome</name>
</root>

which meets both your requirements of (1) removing the umlauts and (2) displaying the ampersand correctly (as required by the XML specification).

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论