Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt XSL使用当前上下文中的变量在层次结构之外应用模板_Xslt - Fatal编程技术网

Xslt XSL使用当前上下文中的变量在层次结构之外应用模板

Xslt XSL使用当前上下文中的变量在层次结构之外应用模板,xslt,Xslt,假设我有以下XML: <Zoo> <Keepers> <Keeper name="Joe" manager="Miles" /> <Keeper name="Bob" manager="Karen"/> </Keepers> <Animals> <Animal type="tiger" keeper="Joe"/> <Animal type="lion" keep

假设我有以下XML:

<Zoo>
  <Keepers>
    <Keeper name="Joe" manager="Miles" />
    <Keeper name="Bob" manager="Karen"/>
  </Keepers>
  <Animals>
    <Animal type="tiger" keeper="Joe"/>
    <Animal type="lion" keeper="Joe"/>
    <Animal type="giraffe" keeper="Bob"/>
  </Animals>
</Zoo>

我基本上希望使用Keeper.name作为变量,然后将模板应用于匹配的动物节点,其中Keeper.name=Animal.Keeper

使用应用模板或其他类型的XSL语法是否可以实现这一点

在我的示例中,我希望删除由Miles管理的所有管理员,并删除由Miles管理的管理员保留的所有动物节点,因此我将应用空白模板

下面是我的sudo XSL,它不太管用:

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="*[@manager='Miles']">
    <xsl:apply-templates select="/Zoo/Animals/Animal[@keeper=current()/@name]"/>
    <!-- apply a blank template to this Keeper -->
</xsl:template>

<xsl:template match="Animal">
    <!-- apply a blank template to this Animal -->
</xsl:template>

我希望的输出XML如下所示:

<Zoo>
  <Keepers>
    <Keeper name="Bob" manager="Karen"/>
  </Keepers>
  <Animals>
    <Animal type="giraffe" keeper="Bob"/>
  </Animals>
</Zoo>


谢谢大家!

在这里,根据列出的输入,如下所示:

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        version="1.0">
        <xsl:param name="removeKeeper">Miles</xsl:param>
        <xsl:template match="Keeper">
            <xsl:if test="not(@manager=$removeKeeper)">
                <Keeper>
                <xsl:apply-templates select="@*"/>
                </Keeper>
            </xsl:if>
        </xsl:template>
        <xsl:template match="Animal">
            <xsl:variable name="keeper" select="@keeper"/>
            <xsl:if test="//Keepers/Keeper[@name=$keeper][not(@manager=$removeKeeper)]">
                <Animal>
                    <xsl:apply-templates select="@*"/>
                </Animal>
            </xsl:if>
        </xsl:template>
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>

英里
产生以下输出:

   <Zoo>
      <Keepers>

       <Keeper name="Bob" manager="Karen"/>
      </Keepers>
     <Animals>


          <Animal type="giraffe" keeper="Bob"/>
     </Animals>
    </Zoo>

为了更通用,您还可以使用:

                <xsl:copy>
                    <xsl:apply-templates select="@*"/>
                </xsl:copy>


而不是XSL中显式的Keeper和Animal标记。取决于您。

您的输出中的Snowy来自哪里?根本无法将输入连接到输出Kevin-修复了我的示例,对此表示抱歉