Create an HTML ID attribute from existing untagged content

I built an XML process to create a rich HTML document with automatic hyperlinks and bindings using unique identifiers. But I need to add unique bindings to an existing element that is not bound. I donโ€™t want to go through hundreds of elements to mark the content, so I wonder if there is a way to pull the letter starting with a paragraph and then add it to the section number to create a unique identifier binding attribute. XML looks like this:

<?xml version="1.0" encoding="UTF-8"?> <RULES> <RULE> <RULE_SECTION> <RULE_subhead><num>3-3</num>. DOUBT AS TO PROCEDURE </RULE_subhead> <rule_letterhead>a. Procedure </rule_letterhead> <rule_letterhead-B>b. Determination of Score for Hole </rule_letterhead-B> </RULE_SECTION> <RULE_SECTION> <RULE_subhead><num>4-1</num>. FORM AND MAKE OF CLUBS </RULE_subhead> <rule_letterhead>a. General </rule_letterhead> <rule_letterhead-B>b. Wear and Alteration </rule_letterhead-B> <rule_letterhead-B>c. Damage in Normal Course of Play </rule_letterhead-B> </RULE_SECTION> </RULE> </RULES> 

Note that the two elements <rule_letterhead> and <rule_letterhead-B> begin with the initial letter and period (abc, etc.). I would like the HTML to look like this:

 <body> <h2 class="RULE_subhead" id="r3-3">3-3. DOUBT AS TO PROCEDURE </h2> <h3 class="rule_letterhead" id="r3-3a">a. Procedure </h3> <h3 class="rule_letterhead" id="r3-3b">b. Determination of Score for Hole </h3> <h2 class="RULE_subhead" id="r4-1">4-1. FORM AND MAKE OF CLUBS </h2> <h3 class="rule_letterhead" id="r4-1a">a. General </h3> <h3 class="rule_letterhead" id="r4-1b">b. Wear and Alteration </h3> <h3 class="rule_letterhead" id="r4-1c">c. Damage in Normal Course of Play </h3> </body> 

So, XSL should pull out the section number and add it to the first letter of the <letterhead> or <letterhead-B> elements, like this "4-1a".

Here is the XSL I'm currently using:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" encoding="UTF-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/> 

 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>Untitled Document</title> </head> <body> <xsl:apply-templates select="RULES"/> </body> </html> </xsl:template> <xsl:template match="RULE_subhead"> <h2 class="RULE_subhead" id="{concat('r',translate(normalize-space(num), ' ', ''))}"> <xsl:apply-templates/></h2> </xsl:template> <xsl:template match="rule_letterhead"> <h3 class="rule_letterhead"><xsl:apply-templates/></h3> </xsl:template> <xsl:template match="rule_letterhead-B"> <h3 class="rule_letterhead"><xsl:apply-templates/></h3> </xsl:template> </xsl:stylesheet> 

It works for all other elements.

thanks

+4
source share
1 answer
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"> <xsl:output method="html" encoding="UTF-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/> <xsl:template match="/"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>Untitled Document</title> </head> <body> <xsl:apply-templates select="RULES"/> </body> </html> </xsl:template> <xsl:template match="RULE_subhead"> <h2 class="RULE_subhead" id="{concat('r',translate(normalize-space(num), ' ', ''))}"> <xsl:apply-templates/> </h2> </xsl:template> <xsl:template match="rule_letterhead|rule_letterhead-B"> <h3 class="rule_letterhead" id="{concat('r',translate(normalize-space(preceding-sibling::RULE_subhead/num), ' ', ''), substring(., 1, 1))}"> <xsl:apply-templates/> </h3> </xsl:template> </xsl:stylesheet> 
+1
source

Source: https://habr.com/ru/post/1383459/


All Articles