简体   繁体   中英

For-Each to apply templates XSLT

I posted this question here. It was answered and I'm happy with it. It's just here to reference the XML input and output.

XML to XML XSLT transformation. MSXML in VBScript

Here is my stylesheet:

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:b="urn:myNameSpace" exclude-result-prefixes="b">

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/b:MYImportFile">

<MYImportFile>

    <xsl:for-each select="b:SAMPLE">

    <SAMPLE>

        <SAMPLEID>
        <xsl:value-of select="b:SAMPLEID"/>
        </SAMPLEID>

        <NAME1_1>
        <xsl:value-of select="b:LOCATION/b:LOCATIONNAME[text() = 'NAME1']/../b:DATA[1]/b:DATAVALUE"/>
        </NAME1_1>

        <xsl:choose> 
            <xsl:when test="b:LOCATION/b:LOCATIONNAME[text() = 'NAME1']/../b:DATA[2]/b:DATAVALUE">
                <NAME1_2>
                <xsl:value-of select="b:LOCATION/b:LOCATIONNAME[text() = 'NAME1']/../b:DATA[2]/b:DATAVALUE"/>
                </NAME1_2>
            </xsl:when>
            <xsl:otherwise>
                <NAME1_2>
                <xsl:value-of select="b:LOCATION/b:LOCATIONNAME[text() = 'NAME1']/../b:DATA[1]/b:DATAVALUE"/>
                </NAME1_2>
            </xsl:otherwise>
        </xsl:choose>


        '''''''''''''''''''there are 100 NAME entires to recieve the 100 locations

    </SAMPLE>

    </xsl:for-each>

</MYImportFile>

</xsl:template>
</xsl:stylesheet>

What change do I need to make to this to make that </xsl:for-each> to a <xsl:apply-templates> ? Is it an easy change? Or is this going to take an overhaul of the entire stylesheet?

If there a repetitions of the same code, it might be possible to implement them in a template so an overhaul is helpful anyway:

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:b="urn:ohLookHEREaNamespacedeclaration"
    exclude-result-prefixes="b"
    version="1.0">

  <xsl:strip-space elements="*"/>

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/*">
     <xsl:element name="{local-name()}">
        <xsl:apply-templates select="b:SAMPLE"/>
     </xsl:element>
  </xsl:template>

  <xsl:template match="b:SAMPLE">
      <xsl:element name="{local-name()}">
          <xsl:apply-templates select="b:SAMPLEID | b:LOCATION"/>
      </xsl:element>
  </xsl:template>

  <xsl:template match="*">
      <xsl:element name="{local-name()}">
          <xsl:apply-templates/>
      </xsl:element>
  </xsl:template>

  <xsl:template match="b:LOCATION">
      <xsl:element name="{b:LOCATIONNAME}_1">
          <xsl:value-of select="b:DATA[1]/b:DATAVALUE"/>
      </xsl:element>
      <xsl:element name="{b:LOCATIONNAME}_2">
          <xsl:value-of select="b:DATA[2]/b:DATAVALUE | b:DATA[1][current()[not(b:DATA[2])]]/b:DATAVALUE"/>
      </xsl:element>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/bFWRAp3

But in general, instead of doing <xsl:for-each select="foo"><bar>...</bar></xsl:for-each> you can of course use <xsl:apply-templates select="foo"/> with a matching <xsl:template match="foo"><bar>...</bar></xsl:template> , if that is what your main question is about.

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