[英]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 输入和输出。
XML to XML XSLT transformation. XML 到 XML XSLT 的转换。 MSXML in VBScript VBScript 中的 MSXML
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>
?我需要对此进行哪些更改才能将</xsl:for-each>
更改为<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 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.但总的来说,你当然可以使用<xsl:apply-templates select="foo"/>
而不是<xsl:for-each select="foo"><bar>...</bar></xsl:for-each>
<xsl:apply-templates select="foo"/>
与匹配的<xsl:template match="foo"><bar>...</bar></xsl:template>
,如果这是您的主要问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.