我試圖更新一些XSLT以適應新的XML輸入格式。新的結構幾乎打破了我以前的模板邏輯,但我試圖避免重寫整個過程。如何獲取XSLT模板中參數的節點名稱?
本質上,我想抓取模板參數的名稱,刪除前綴,並在<xsl:value-of/>
中使用結果。例如:
<!-- calling location -->
<xsl:call-template name="myTemplate">
<xsl:with-param name="field" select="Name"/>
</xsl:call-template>
<!-- template to do the processing. -->
<xsl:template name="myTemplate">
<xsl:param name="field"/>
<xsl:variable name="fieldName" select="replace($field, 'Prefix', '')"/>
<xsl:value-of select="$fieldName"/>
</xsl:template>
上下文。
由於我被要求提供問題的本質和所有上下文,這更多的是全面的。我的樣式表的簡化版本可以蜜蜂如下圖所示:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:variable name="delimiter" select="','" />
<xsl:variable name="newline" select="'
'" />
<xsl:template match="/root">
<xsl:apply-templates select="entry"/>
<xsl:apply-templates select="entry/Dependents"/>
</xsl:template>
<xsl:template match="entry">
<xsl:value-of select="Name"/>
<xsl:value-of select="$delimeter"/>
<xsl:value-of select="Address"/>
<xsl:value-of select="$delimeter"/>
<xsl:value-of select="Phone"/>
<xsl:value-of select="$newline"/>
</xsl:template>
<xsl:template match="entry/Dependents">
<xsl:call-template name="valueOrParentOrNone">
<xsl:with-param name="fieldValue" select="Name"/>
</xsl:call-template>
<xsl:value-of select="$delimeter"/>
<xsl:call-template name="valueOrParentOrNone">
<xsl:with-param name="fieldValue" select="Address"/>
</xsl:call-template>
<xsl:value-of select="$delimeter"/>
<xsl:call-template name="valueOrParentOrNone">
<xsl:with-param name="fieldValue" select="Phone"/>
</xsl:call-template>
<xsl:value-of select="$newline"/>
</xsl:template>
<xsl:template name="valueOrParentOrNone">
<xsl:param name="fieldValue"/>
<xsl:choose>
<!-- Use passed in node if exists -->
<xsl:when test="$fieldValue">
<xsl:value-of select="$fieldValue" />
</xsl:when>
<!-- Use parent node of the same name if exists -->
<xsl:when test="../$fieldName">
<xsl:value-of select="../$fieldName" />
</xsl:when>
<!-- Use "None" -->
<xsl:otherwise>
<xsl:text>"None"</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
原始輸入XML:
<root>
<entry>
<Name>J. Smith</Name>
<Address>1123 XML Lane</Address>
<Phone>123-456-7890</Phone>
<Dependents>
<Name>S. Smith</Name>
<Address>1123 XML Lane</Address>
<Phone>123-456-0987</Phone>
</Dependents>
</entry>
<entry>
<Name>L. Hines</Name>
<Address>423 Programming Way</Address>
<Dependents>
<Name>P. Hines</Name>
</Dependents>
</entry>
</root>
投入新XML
<root>
<entry>
<Name>J. Smith</Name>
<Address>1123 XML Lane</Address>
<Phone>123-456-7890</Phone>
<DependentName>S. Smith</DependentName>
<DependentAddress>1123 XML Lane</DependentAddress>
<DependentPhone>123-456-0987</DependentPhone>
</entry>
<entry>
<Name>L. Hines</Name>
<Address>423 Programming Way</Address>
<DependentName>P. Hines</DependentName>
</entry>
</root>
你會發現,並不是所有的值都是以上述兩種格式提供。當依賴值不存在時,我需要重用父值。如果不存在,我需要使用「None」的值。輸入格式已更改,以便Dependents
兄弟姐妹不再存在。相反,Dependents
的孩子已經被提升爲兄弟姐妹的正常人價值,現在是entry
的直接子女。
預期輸出。
"J. Smith","1123 XML Lane","123-456-7890"
"S. Smith","1123 XML Lane","123-456-0987"
"L. Hines","423 Programming Way",,
"P. Hines","423 Programming Way","None"
編輯:對不起,容易混淆。
請不要使用示例XML,上面寫着'','',等當你的節點真的沒有在其名稱中的計數器。對於試圖回答你的問題的人來說,編寫XSLT是非常困難的。此外,修正示例中的語法錯誤,並使它們適合您的XSLT代碼 - 或從您的XSLT代碼中刪除與您的問題無關的所有部分。 –
Tomalak
@Tomalak。固定。 – jktravis
好的,還有一些'functx:stripCommas()'業務正在進行,這並沒有什麼實際影響,但它更好。對未來而言:將問題簡化爲本質有益於那些回答問題的人,對於下一個發現問題的人,而不是至少對你而言,因爲它有助於充實自己的頭腦。 – Tomalak