繁体   English   中英

xsl:apply-templates不迭代select给出的每个节点

[英]xsl:apply-templates not iterating over each node given by select

我正在使用Saxon HE 9-4-0-3J。 我的目的是使用xsl:apply-templates上的select属性迭代每个<RECORD>元素,但是只处理xml中的一个<RECORD>元素。

XML

<ENVELOPE>
<PRODUCT>
    <HEAD><FLAG>No</FLAG></HEAD>
    <BODY>
        <RECORD><Value>9</Value></RECORD>
        <RECORD><Value>10</Value></RECORD>
        <MISC>9</MISC>
    </BODY>
</PRODUCT>

xslt 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/ENVELOPE">
<Interface>
   <Data><xsl:apply-templates select="PRODUCT"/></Data>
</Interface>
</xsl:template>

<xsl:template match="PRODUCT">
   <xsl:choose>
      <xsl:when test="HEAD/FLAG='Yes'">
         <xsl:attribute name="Match">Affirmative</xsl:attribute>
         <xsl:apply-templates select="BODY/RECORD" mode="affr"/>
      </xsl:when>
      <xsl:when test="HEAD/FLAG='No'">
         <xsl:attribute name="Match">Negative</xsl:attribute>
         <xsl:apply-templates select="BODY/RECORD" mode="neg"/>
      </xsl:when>
   </xsl:choose>
</xsl:template>

<xsl:template match="RECORD" mode="affr">
   <xsl:attribute name="Value"><xsl:value-of select="Value"/></xsl:attribute>
</xsl:template>

<xsl:template match="RECORD" mode="neg">
   <xsl:attribute name="Value"><xsl:value-of select="Value"/></xsl:attribute>
</xsl:template>

</xsl:stylesheet>

产量

<Interface>
      <Data Match="Negative" Value="9"/> <!-- this line doesn't appear, I want it to -->
      <Data Match="Negative" Value="10"/>
</Interface>

匹配<ENVELOPE...>时会生成<DATA...>输出标记,因此只能有其中一个输出。 任何<xsl:attribute>指令只会添加到此输出标记,以后的值会覆盖之前的值。

您必须显式生成要输出的DATA标记,如下所示:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">

  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/ENVELOPE">
    <Interface>
      <xsl:apply-templates select="PRODUCT"/>
    </Interface>
  </xsl:template>

  <xsl:template match="PRODUCT">
    <xsl:variable name="flag">
      <xsl:choose>
        <xsl:when test="HEAD/FLAG='Yes'">Affirmative</xsl:when>
        <xsl:when test="HEAD/FLAG='No'">Negative</xsl:when>
      </xsl:choose>
    </xsl:variable>

    <xsl:apply-templates select="BODY/RECORD">
      <xsl:with-param name="flag"><xsl:value-of select="$flag"/></xsl:with-param>
    </xsl:apply-templates>

  </xsl:template>

  <xsl:template match="RECORD">
    <xsl:param name="flag"/>
    <Data Value="{Value}" Match="{$flag}"/>
  </xsl:template>

</xsl:stylesheet>

问题是您正在创建两个具有相同名称的属性 - Value 在格式良好的XML文档中,元素不能具有多个具有相同名称的属性。

引用W3C XSLT 2.0规范 (这在XSLT 1.0中也是如此):

9.如果结果序列中的属性A与稍后出现在结果序列中的另一个属性B具有相同的名称,则从结果序列中丢弃属性A.

因此,每当XSLT为一个元素生成多个具有相同名称的属性时,只有最后一个属性被复制到输出中 - 这就是您所看到的。

这是一个简短而简单的解决方案(只有两个模板,没有条件,没有参数)

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:variable name="vDoc" select="/"/>

 <xsl:variable name="vMatch" select=
 "('Affirmative', 'Negative')[2 - number($vDoc/*/*/HEAD/FLAG eq 'Yes')]"/>

 <xsl:template match="/*">
  <Interface>
    <xsl:apply-templates select="*/BODY/RECORD"/>
  </Interface>
 </xsl:template>

 <xsl:template match="RECORD">
  <Data Match="{$vMatch}" Value="{Value}"/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的XML文档时:

<ENVELOPE>
    <PRODUCT>
        <HEAD>
            <FLAG>No</FLAG>
        </HEAD>
        <BODY>
            <RECORD>
                <Value>9</Value>
            </RECORD>
            <RECORD>
                <Value>10</Value>
            </RECORD>
            <MISC>9</MISC>
        </BODY>
    </PRODUCT>
</ENVELOPE>

产生了想要的正确结果

<Interface>
   <Data Match="Negative" Value="9"/>
   <Data Match="Negative" Value="10"/>
</Interface>

暂无
暂无

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

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