[英]How do I get a new DITA tag to carry over to my output using XSLT?
我已成功將我的 XSLT 更新為將audience
標簽添加到我的 output 的腳本,但我被要求讓 output 顯示略有不同。
我的原始輸入如下所示:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE bookmap PUBLIC "-//OASIS//DTD DITA BookMap//EN" "bookmap.dtd" []>
<bookmap>
<topicgroup>
<topichead navtitle="Arkansas Property and Casualty Adjuster Law" locktitle="yes" audience="Arkansas">
<topicgroup>
<mapref href="Qbank/maps/Adj_Unit_AR.ditamap" format="ditamap"/>
</topicgroup>
</topichead>
</topicgroup>
</bookmap>
我的原始腳本部分如下所示:
<xsl:template match="*[contains(@class,' mapgroup-d/topichead ')]">
<topichead>
<xsl:attribute name="navtitle">
<xsl:value-of select="@navtitle"/>
</xsl:attribute>
<xsl:choose>
<xsl:when test="@audience">
<xsl:attribute name="audience">
<xsl:value-of select="@audience"/>
</xsl:attribute>
</xsl:when>
<xsl:otherwise />
</xsl:choose>
</topichead>
</template>
但現在我被要求將audience
標簽移動到不同的位置,我無法通過轉換來獲取標簽。
我的新輸入如下所示:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE bookmap PUBLIC "-//OASIS//DTD DITA BookMap//EN" "bookmap.dtd" []>
<bookmap>
<chapter navtitle="Alabama Property and Casualty Adjuster Law" locktitle="yes" audience="Alabama">
<topicgroup
<topicgroup>
<mapref href="Qbank/maps/Adj_Unit_AL.ditamap" format="ditamap"/>
</topicgroup>
</topicgroup>
</chapter>
</bookmap>
這是我試過的腳本,但它沒有拉過觀眾標簽。
<xsl:template match="*[contains(@class,' mapgroup-d/topichead ')]">
<topichead>
<xsl:attribute name="navtitle">
<xsl:value-of select="@navtitle"/>
</xsl:attribute>
<xsl:choose>
<xsl:when test="bookmap/chapter/@audience">
<xsl:attribute name="audience">
<xsl:value-of select="@audience"/>
</xsl:attribute>
</xsl:when>
<xsl:otherwise />
</xsl:choose>
</topichead>
</template>
我究竟做錯了什么?
這個 xsl:when 表達式:
<xsl:when test="bookmap/chapter/@audience">
是相對的 xpath,這意味着 XSLT 處理器搜索元素“bookmap”,它是與 xsl:message 匹配的當前“topichead”元素的子元素。 您需要向上搜索,例如:
<xsl:when test="ancestor::chapter/@audience">
然后這個值:
<xsl:value-of select="@audience"/>
它又是一個相對的 xpath 表達式,相對於 xsl:template 匹配的當前“topichead”元素。 因此,您可能需要將其替換為:
<xsl:value-of select="ancestor::chapter/@audience">
除了@Radu Coravu 的回答:更短一點的是:
<xsl:choose>
<xsl:when test="ancestor::chapter/@audience">
<xsl:attribute name="audience">
<xsl:value-of select="ancestor::chapter/@audience"/>
</xsl:attribute>
</xsl:when>
<xsl:otherwise />
</xsl:choose>
只需使用:
<xsl:copy-of select="ancestor::chapter/@audience"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.