简体   繁体   English

我如何使用 XSLT 获得新的 DITA 标签以转移到我的 output?

[英]How do I get a new DITA tag to carry over to my output using XSLT?

I had successfully updated my XSLT to script to add the audience tag to my output but I was asked for the output to display slightly differently.我已成功将我的 XSLT 更新为将audience标签添加到我的 output 的脚本,但我被要求让 output 显示略有不同。

My original input looked like this:我的原始输入如下所示:

<?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>

The portion of my original script looked like this:我的原始脚本部分如下所示:

<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>

But now I'm being asked to move the audience tag to a different spot and I'm having trouble getting the transform to grab the tag.但现在我被要求将audience标签移动到不同的位置,我无法通过转换来获取标签。

My new input looks like this:我的新输入如下所示:

<?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>

This is the script I tried but it's not pulling over the audience tag.这是我试过的脚本,但它没有拉过观众标签。

<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>

What am I doing wrong?我究竟做错了什么?

This xsl:when expression:这个 xsl:when 表达式:

 <xsl:when test="bookmap/chapter/@audience">

is a relative xpath, meaning that the XSLT processor searches for an element "bookmap" which is a child of the current "topichead" element matched by the xsl:message.是相对的 xpath,这意味着 XSLT 处理器搜索元素“bookmap”,它是与 xsl:message 匹配的当前“topichead”元素的子元素。 You need to search upwards, something like:您需要向上搜索,例如:

 <xsl:when test="ancestor::chapter/@audience">

Then this value-of:然后这个值:

 <xsl:value-of select="@audience"/>

it's a relative xpath expression again, relative to the current "topichead" element matched by the xsl:template.它又是一个相对的 xpath 表达式,相对于 xsl:template 匹配的当前“topichead”元素。 So you would probably need to replace it with:因此,您可能需要将其替换为:

<xsl:value-of select="ancestor::chapter/@audience">

In addition to the answer of @Radu Coravu: A bit shorter would be instead of:除了@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>

just use:只需使用:

<xsl:copy-of select="ancestor::chapter/@audience"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM