简体   繁体   中英

How to transform xslt -> html for the this document

xml

  <?xml version='1.0' encoding='UTF-8'?>
    <folder>
    <document>
            <h1>Harry Potter</h1>
            <h2>j.rowling</h2>
            <subheading>book</subheading>
        </document>
        <document>
            <p type="num">
                <num>55</num>           
            </p>
            <h1>Hi-Tech</h1>
        </document>
    </folder>

I can not do it :

  1. If the order of the elements h1, h2, subheading is found, then display h1 and subheading on one line, then output the element h2;

NOW:

   <document>
        <h1>Harry Potter</h1>
        <h2>j.rowling</h2>
        <subheading>book</subheading>
    </document>

SHOULD BE:

   <div class="document">
        <h1 style="display:inline-block">Harry Potter book </h1>
        <h5 style="display:inline-block">book</h5>
        <br/>
        <h2>j.rowling</h2>
    </div>

  1. If before the h1 element there is an element "p" with the attribute type = "num", inside of which there is not an empty element num - display it on one line with the element h1.

Taking the first requirement first, if you know that the input has the sequence (h1, h2, subheading) then it's easily done with this logic:

<xsl:template match="document">
  <div class="document">
    <xsl:apply-templates select="h1"/>
    <xsl:apply-templates select="subheading"/>
    <br/>
    <xsl:apply-templates select="h2"/>
  </div>
</xsl:template>

<xsl:template match="h1"/>
  <h1 style="display:inline-block"><xsl:value-of select="."/></h1>
</xsl:template>

<xsl:template match="h2"/>
  <h2><xsl:value-of select="."/></h2>
</xsl:template>

<xsl:template match="subheading"/>
  <h5 style="display:inline-block"><xsl:value-of select="."/></h5>
</xsl:template>

But then the question arises: what if it isn't in this format? Unfortunately you haven't told us what other formats are possible, and what you want to generate when they are encountered. So we can't really take this any further without more information.

The second rule can be translated into XSLT as

<xsl:template match="h1[preceding-sibling::*[1][self::p/@num][not(child::num)]">
  <h1>
   <xsl:value-of select="preceding-sibling::*[1][self::p/@num]/@num"/>
   <xsl:value-of select="."/>
  </h1>
</xsl:template>

I did it!

My solution based on and operator

* MY WORK SOLUTION:*

<xsl:template match="document[h1 and h2 and subheading]">
  <xsl:value-of select="concat(h1,' ',subheading)"/>
  <br/>
  <xsl:value-of select="h2"/>
</xsl:template>

<xsl:template match="document[p[@type='num'][num] and h1]">  
  <xsl:value-of select="concat(h1,' ',p)"/>
</xsl:template>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM