简体   繁体   中英

convert xslt2.0 to xslt1.0 syntax

I'm new to XSLT and i got to understand from my team that the current xml parser we have supports only version 1.0. I would need help here to convert my current xslt 2.0 code to version 1.0

 <xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/Root">
    <!-- header -->
    <xsl:text>id|parent_id</xsl:text>
    <xsl:value-of select="$newline"/>
    <!-- data -->
    <xsl:for-each select="Package">
        <xsl:variable name="b-id" select="Business_ID" />
        <!-- TPFC data -->
        <xsl:for-each select="TPFC">
            <xsl:variable name="total_level" select="count(.//.//Parent)+1"/>
            <xsl:call-template name="prod-family">
                <xsl:with-param name="total_level" select="$total_level"/>
                <xsl:with-param name="b_id" select="$b-id"/>
            </xsl:call-template>

            <xsl:for-each select="//Parent">
                <!-----Question 2------->
                <xsl:call-template name="prod-family">
                    <xsl:with-param name="total_level" select="$total_level"/>
                    <xsl:with-param name="b_id" select="$b-id"/>
                </xsl:call-template>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:for-each>
</xsl:template>

<!-----Question 1------->
<xsl:template name="prod-family">
    <xsl:param name="total_level"/>
    <xsl:param name="b_id"/>
    <xsl:value-of select="reverse(.//Parent/Name), translate(Name,' ','')" separator="_" />
    <xsl:value-of select="$separator"/>
    <xsl:value-of select="reverse(.//Parent/Name)" separator="_" />
    <xsl:value-of select="$separator"/>
    <xsl:value-of select="$newline"/>
</xsl:template>

</xsl:stylesheet>

XML For the above is

    <?xml version="1.0" encoding="UTF-8"?>
<Root
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Package ID="1122" BusinessID="001233" Version="19.0.2" xsi:type="Doms">
        <Name>ABC</Name>
        <Description> Desc sample</Description>
        <Category>54</Category>
        <Business_ID>001233</Business_ID>
        <TPFC ID="76" xsi:type="TPFC" Pattern="Th_Branch">
            <Name>AM</Name>
            <Parent ID="11d" xsi:type="TPFC" Pattern="Th_Branch">
                <Description>Mob1</Description>
                <Name>Mob1</Name>
                <Parent ID="12F" xsi:type="TPFC" Pattern="Th_Branch">
                    <Description>Mob2</Description>
                    <Name>Mob2</Name>
                </Parent>
            </Parent>
        </TPFC>
    </Package>
</Root>

This is the result

  | id           |  parent_id | 
  | Mob2_Mob1_AM |  Mob2_Mob1 |
  | Mob2_Mob1    | Mob2      | 
  | Mob2         |           | 

In the above example, I have implemented the concatenation using XSLT 2.0 in two places (check template - prod-family ) where

id - Concatenation of all names from its root parent to current name in the below example it will be Mob2_Mob1_AM parent_id - Concatenation of all parent names from its root parent

  1. Can anyone help me out on how to implement concat using for-each in XSLT 1.0 version for the above two cases
  2. Is call-template along with params supported in XSLT 1.0 as well?

At a quick glance the only 2.0 constructs here are the call on reverse() , and the use of xsl:value-of/@separator . These are used together. You should be able to do something like:

<xsl:for-each select=".//Parent/Name | ./Name">
  <xsl:sort select="-position()"/>
  <xsl:value-of select="."/>
  <xsl:if test="position() != last()">_</xsl:if>
</xsl:for-each>

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