[英]For-Each to apply templates XSLT
我在這里發布了這個問題。 它得到了回答,我很高興。 它只是在這里引用 XML 輸入和輸出。
XML 到 XML XSLT 的轉換。 VBScript 中的 MSXML
這是我的樣式表:
<?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>
我需要對此進行哪些更改才能將</xsl:for-each>
更改為<xsl:apply-templates>
? 改變容易嗎? 或者這是否會對整個樣式表進行大修?
如果有相同代碼的重復,則可以在模板中實現它們,因此無論如何進行大修是有幫助的:
<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
但總的來說,你當然可以使用<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.