[英]Limit XSLT apply-template to current node only?
是否可以將xsl:apply-templates限制為僅匹配節點,而不嘗試匹配嵌套元素?
我知道我可以使用呼叫模板。 以下是有關我為什么要這樣做的詳細信息-
我有一個由Section和Field元素組成的文檔。
Section可以包含嵌套的Section元素或Field元素。 字段元素沒有嵌套。
例如,對於Field,模板代碼的片段為:
<xsl:template match="Field" mode="Field">
<xsl:apply-templates select="." mode="FieldHeight" />
.....
</xsl:template>
<xsl:template match="Field[LayoutTitlePosition = 'top']" mode="FieldHeight">
<xsl:attribute name="h" select="$glTopFieldHeight" />
</xsl:template>
<xsl:template match="Field[LayoutTitlePosition = ('left', 'right', 'none')]" mode="FieldHeight">
<xsl:attribute name="h" select="$glHorzFieldHeight" />
</xsl:template>
使用xsl:choose / xsl:when語句可以完成相同的操作,但是我發現匹配的搜索參數的語法更好,更易於維護。 我能夠維護一個通用的Field模板,並根據輸入覆蓋特定的部分。 由於字段不是嵌套的,因此我不必擔心'subFields'是否匹配。
我想對Section標題做同樣的事情,但是問題是,如果我將模板應用於Section,它也可能匹配嵌套的section。 當我將模板應用於不包含標題的子節的無標題的節時,會發生這種情況-該子節被匹配兩次。
編輯問題示例。
我試圖創建一個最小的示例來重現該問題。 輸入文件:
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<Section>
<!-- no title -->
<Section>
<Title>Sub section title</Title>
<Field>
<Type>Text</Type>
</Field>
</Section>
</Section>
</Root>
XSL轉換文檔:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />
<xsl:template match="*" >
<subform layout="tb">
<xsl:apply-templates select=".[name() = 'Section']" mode="Heading" />
<xsl:apply-templates select=".[name() = 'Field']" mode="Field" />
<xsl:apply-templates select="*[name() = ('Section', 'Field')]" />
</subform>
</xsl:template>
<xsl:template match="Section[Title]" mode="Heading">
<subform layout="tb">
<xsl:call-template name="drawHeading" />
</subform>
</xsl:template>
<xsl:template name="drawHeading">
<Heading><xsl:value-of select="Title" /></Heading>
</xsl:template>
<xsl:template match="Field" mode="Field">
<field name="{@Name}">
</field>
</xsl:template>
輸出,來自轉換輸入文檔:
<?xml version="1.0" encoding="UTF-8"?>
<subform layout="tb">
<subform layout="tb">
<subform layout="tb">
<Heading>Sub section title</Heading>
</subform>
<subform layout="tb">
<subform layout="tb">
<Heading>Sub section title</Heading>
</subform>
<subform layout="tb">
<field name=""/>
</subform>
</subform>
</subform>
</subform>
當沒有標題的父節不匹配時,子節標題似乎已匹配。
我想要達到的結果是:
<?xml version="1.0" encoding="UTF-8"?>
<subform layout="tb">
<subform layout="tb">
<subform layout="tb">
<subform layout="tb">
<Heading>Sub section title</Heading>
</subform>
<subform layout="tb">
<field name=""/>
</subform>
</subform>
</subform>
</subform>
如果源文檔中存在標題,則示例:
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<Section>
<Title>Section title</Title>
<Section>
<Title>Sub section title</Title>
<Field>
<Type>Text</Type>
</Field>
</Section>
</Section>
</Root>
然后它應該產生:
<?xml version="1.0" encoding="UTF-8"?>
<subform layout="tb">
<subform layout="tb">
<subform layout="tb">
<Heading>Section title</Heading>
</subform>
<subform layout="tb">
<subform layout="tb">
<Heading>Sub section title</Heading>
</subform>
<subform layout="tb">
<field name=""/>
</subform>
</subform>
</subform>
</subform>
似乎只需對Field
和Title
使用兩個專用模板即可:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />
<xsl:template match="*" >
<subform layout="tb">
<xsl:apply-templates/>
</subform>
</xsl:template>
<xsl:template match="Title">
<subform layout="tb">
<Heading><xsl:value-of select="." /></Heading>
</subform>
</xsl:template>
<xsl:template match="Field">
<subform layout="tb">
<field name="{@Name}"></field>
</subform>
</xsl:template>
</xsl:stylesheet>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.