ChatGPT
ChatGPT

Reputation: 5617

How to transform this HTML into desired text using XSLT

it's been nearly 20 years since I did anything with XSLT. Trying to convert docs like this HTML snippet into the text below.

<p>
おばあさんは、とても
<ruby><rb>喜</rb><rp>(</rp><rt>よろこ</rt><rp>)</rp></ruby>びました。<br/>
「おじいさん、
<ruby><rb>今</rb><rp>(</rp><rt>こん</rt><rp>)</rp></ruby>
<ruby><rb>晩</rb><rp>(</rp><rt>ばん</rt><rp>)</rp></ruby>、このたぬきで、たぬき
</p>

desired output

おばあさんは、とても<ruby=よろこ>喜</ruby>びました。<br/>
「おじいさん、
<ruby=こん>今</ruby><ruby=ばん>晩</ruby>、このたぬきで、たぬき

I have many pages of text I need to convert to this proprietary markup.

here's what I got started, but it's far from working as I'm struggling with handling the text outside of the ruby tags. This has long been a difficulty of XSLT right?

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes"/>
<xsl:template match="/p">
    <xsl:text disable-output-escaping="yes">&lt;p&gt;</xsl:text>
            <xsl:value-of select="."/>
    <xsl:apply-templates select="ruby"/>
    <xsl:text disable-output-escaping="yes">&lt;/p&gt;</xsl:text>
</xsl:template>
<xsl:template match="ruby">

        <xsl:apply-templates select="rb"/>
        <xsl:apply-templates select="rt"/>

</xsl:template>
<xsl:template match="rb">
    <xsl:value-of select="."/>
            <xsl:text>&lt;/ruby&gt;</xsl:text>
</xsl:template>
<xsl:template match="rt">
    <xsl:text>&lt;ruby=</xsl:text>
    <xsl:value-of select="."/>
    <xsl:text>&gt;</xsl:text>
</xsl:template>
</xsl:stylesheet>

UPDATE ah I see part of the story to use priority

<xsl:template match="/p" priority="0">
    <xsl:text disable-output-escaping="yes">&lt;p&gt;</xsl:text>
       <xsl:apply-templates/>

    <xsl:apply-templates select="ruby"/>
    <xsl:text disable-output-escaping="yes">&lt;/p&gt;</xsl:text>
</xsl:template>

priority is needed to ensure it doesn't get applied ahead of the templates matching "ruby" and "rt" for example.

Upvotes: 1

Views: 109

Answers (1)

ChatGPT
ChatGPT

Reputation: 5617

SOLVED it

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes"/>
<xsl:template match="/p" priority="0">
    <xsl:text>&lt;p&gt;</xsl:text>
    <xsl:apply-templates/>
    <xsl:text>&lt;/p&gt;</xsl:text>
</xsl:template>
<xsl:template match="ruby">
    <xsl:text>&lt;ruby=</xsl:text>
    <xsl:apply-templates select="rt"/>
    <xsl:text>&gt;</xsl:text>
    <xsl:apply-templates select="rb"/>
    <xsl:text>&lt;/ruby&gt;</xsl:text>
</xsl:template>
<xsl:template match="rb">
    <xsl:value-of select="."/>
</xsl:template>
<xsl:template match="rt">
    <xsl:value-of select="."/>
</xsl:template>
<xsl:template match="br">
    <xsl:text>&lt;br/&gt;</xsl:text>
</xsl:template>
</xsl:stylesheet>

Upvotes: 1

Related Questions