在我的代碼中,我遇到了當前的問題。我正在嘗試從“config”元素回傳屬性“version”的值并使用它來創建條件。
如果 version = "2" 它應該執行模板 "c-stage",如果它以 "$" 開頭,它應該洗掉 dolar 符號,將其保存到一個變數中,將它與 XML 檔案夾中的變數進行比較,如果比較等于“2”時的變數,然后運行模板“c-stage”。如果元素“config”沒有“version”屬性,它會在節點層次結構中查找父“version”。
目前這是我的代碼:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="c-check" />
</xsl:copy>
</xsl:template>
<!-- c-condition -->
<xsl:template match="//config" mode="c-check">
<xsl:variable name="versionspec">
<xsl:apply-templates select="." mode="c-condition" />
</xsl:variable>
<xsl:variable name="staged">
<xsl:choose>
<xsl:when test="starts-with($versionspec, '2.')">
<xsl:value-of select="$versionspec" />
</xsl:when>
<xsl:otherwise />
</xsl:choose>
</xsl:variable>
<xsl:choose>
<xsl:when test="$staged">
<xsl:copy>
<xsl:apply-templates select="." mode="c-staged" />
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="." mode="c-non-staged" />
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="config" mode="c-condition">
<xsl:message>
Foo!
<xsl:value-of select="@name" />
</xsl:message>
<xsl:choose>
<xsl:when test="@version">
<xsl:choose>
<xsl:when test="starts-with(translate(@version, '0123456789', '9999999999'), '9')">
<xsl:value-of select="." />
</xsl:when>
<xsl:when test="starts-with(@version, '$')">
<xsl:variable name="variableName">
<xsl:call-template name="remove">
<xsl:with-param name="value" select="." />
</xsl:call-template>
</xsl:variable>
<!-- strip the dollar sign an braces and assign the result to a variable "variableName" -->
<xsl:apply-templates select="//variable[@name = $variableName]" mode="c-condition" />
</xsl:when>
<xsl:otherwise />
</xsl:choose>
<!-- find versionSpec -->
</xsl:when>
<xsl:otherwise>
<xsl:if test="@extension">
<xsl:variable name="extension" select="@extension" />
<xsl:apply-templates select="//config[@name=$extension]" mode="c-condition" />
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="config" mode="c-staged">
<!-- CHECKS CURRENT STAGE -->
<xsl:choose>
<xsl:when test="contains(@name, 'release')">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:attribute name="stage">RELEASE</xsl:attribute>
<xsl:copy-of select="*" />
</xsl:copy>
</xsl:when>
<xsl:when test="contains(@name, 'test')">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:attribute name="stage">TEST</xsl:attribute>
<xsl:copy-of select="*" />
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:copy-of select="@*|*" />
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="config" mode="c-non-staged">
<xsl:copy>
<xsl:copy-of select="@*|*" />
</xsl:copy>
</xsl:template>
<xsl:template match="variable" mode="c-condition">
<xsl:value-of select="text()" />
</xsl:template>
<xsl:template name="remove">
<xsl:param name="value" />
<xsl:value-of select="concat(substring-before($value, '$['), substring-after($value, ']'))" />
</xsl:template>
</xsl:stylesheet>
這是我的 XML 檔案:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Launcher.xsl"?>
<launcherConfig>
<variables>
<variable name="productionVersion">1.23</variable>
<variable name="productionPriority">0</variable>
<variable name="releaseSITEVersion">1.1.214c</variable>
<variable name="testSITEVersion">1.1</variable>
<variable name="testSITEVersion4294_RC1">1.4.254</variable>
<variable name="testSITEVersion4607">-1.1</variable>
<variable name="testSITEVersion5210">1.6</variable>
<variable name="testSITEVersion5227_Fix">1.7.2702</variable>
<variable name="testSITEVersion5344">true</variable>
<variable name="testSITEVersion5682">1.3.3418</variable>
<variable name="testSITEVersionALTRAP5693">1.8</variable>
<variable name="testSITEVersion4856">foo.1.2146</variable>
<variable name="testSITEVersion5227">1.1.2530</variable>
</variables>
<configs>
<config name="1_TEST_FOO" extends="1" abstract="true" version="1">
</config>
<config name="1_TEST_FOO_release" extends="1_TEST_FOO">
</config>
<config name="1_TEST_FOO_test" extends="1_TEST_FOO" version="${testSITEVersion5227}">
</config>
<config name="2_TEST_FOO" extends="2" abstract="true">
</config>
<config name="2_TEST_FOO_release" extends="2_TEST_FOO" version="2.0" >
</config>
<config name="2_TEST_FOO_test" extends="2_TEST_FOO" version="${testSITEVersion5227}">
</config>
<config name="3_TEST_FOO" extends="3" abstract="true" version="${releaseSITEVersion}">
</config>
<config name="3_TEST_FOO_release" extends="3_TEST_FOO" >
</config>
<config name="3_TEST_FOO_test" extends="3_TEST_FOO">
</config>
</configs>
</launcherConfig>
從我的角度來看,我的代碼應該可以正常作業,但是(我對 XSL 還是很陌生),它目前忽略了所有條件,直接執行“c-stage”,甚至忽略了版本。給我這個:
<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="Launcher.xsl"?><launcherConfig>
1.23
0
1.1.214c
1.1
1.4.254
-1.1
1.6
1.7.2702
true
1.3.3418
1.8
foo.1.2146
1.1.2530
<config>
<config name="1_TEST_FOO" extends="1" abstract="true" version="1"/>
</config>
<config>
<config name="1_TEST_FOO_release" extends="1_TEST_FOO" stage="RELEASE"/>
</config>
<config>
<config name="1_TEST_FOO_test" extends="1_TEST_FOO" version="${testSITEVersion5227}" stage="TEST"/>
</config>
<config>
<config name="2_TEST_FOO" extends="2" abstract="true"/>
</config>
<config>
<config name="2_TEST_FOO_release" extends="2_TEST_FOO" version="2.0" stage="RELEASE"/>
</config>
<config>
<config name="2_TEST_FOO_test" extends="2_TEST_FOO" version="${testSITEVersion5227}" stage="TEST"/>
</config>
<config>
<config name="3_TEST_FOO" extends="3" abstract="true" version="${releaseSITEVersion}" stage="TEST"/>
</config>
<config>
<config name="3_TEST_FOO_release" extends="3_TEST_FOO" stage="RELEASE"/>
</config>
<config>
<config name="3_TEST_FOO_test" extends="3_TEST_FOO" stage="TEST"/>
</config>
</launcherConfig>
它只是將屬性“stage”添加到它們中的每一個,而沒有遵循適當的條件。
我想知道我做錯了什么,我該如何解決?
也許標題不是最好的,這里的 StackOverflow 和 XSLT noobie
uj5u.com熱心網友回復:
我沒有詳細查看邏輯,但是:
<xsl:variable name="staged">
<xsl:choose>
<xsl:when test="starts-with($versionspec, '2.')">
<xsl:value-of select="$versionspec" />
</xsl:when>
<xsl:otherwise />
</xsl:choose>
</xsl:variable>
在沒有“as”屬性的情況下xsl:variable,該變數是一個檔案節點,因此<xsl:when test="$staged">始終為真。
我不確定你想$staged成為什么型別,但最好 - 為了可讀性、可除錯性、性能和可靠性 - 在@as屬性中宣告它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/471885.html
下一篇:XSLT流在if條件下太慢
