如何重用模板匹配找到的ID?

时间:2015-02-06 13:47:03

标签: xslt

如何在其他模板中使用搜索结果? 假设:

 <parent>
    <test>
        <bla Id="1">
            <mychild attr="x" />
        </bla>
        <bla Id="2">
            <mychild attr="y" />
        </bla>
        <bla Id="3">
            <mychild attr="z" />
        </bla>
    </test>

    <test2>
        <bla2 Id="1" />
        <bla2 Id="2" />
        <bla2 Id="3" />
    </test2>
</parent>

我想首先找出哪个bla有一个attr =“x”的孩子,保存该bla的Id,然后删除具有该Id的每个<bla2>以及<bla>本身。 到目前为止,我有这个:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:wix="http://schemas.microsoft.com/wix/2006/wi">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*" />

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


<xsl:template match="parent/test/bla">
  <xsl:choose>
        <xsl:when test="mychild[@attr='x']">
            <!-- apparently I cannot reuse this.. -->
            <xsl:variable name="idToDelete"><xsl:value-of select="@Id"/></xsl:variable>
        </xsl:when>
        <xsl:otherwise>
          <xsl:copy-of select="."/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

它可以正常删除第一个<bla>,但现在我仍然坚持如何重复使用Id在另一个模板匹配/搜索中删除具有该ID的bla2项。 我尝试了xsl:variable(它们显然超出范围..)我尝试使用with-param调用模板...由于某种原因,在第一次匹配中没有删除项目,第二个模板也没有工作。 如何以XSLT™方式解决此问题?

2 个答案:

答案 0 :(得分:6)

我建议从相反的方向看它:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="bla" match="bla" use="@Id" />

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

<xsl:template match="bla[mychild/@attr='x']"/>
<xsl:template match="bla2[key('bla', @Id)/mychild/@attr='x']" />

</xsl:stylesheet>

IOW,删除任何bla attr =“x”的mychild,然后删除bla2具有blamychild的{​​{1}} <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:key name="deleted-bla" match="bla[mychild/@attr='x']" use="@Id" /> <!-- identity transform --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="bla[key('deleted-bla', @Id)]"/> <xsl:template match="bla2[key('deleted-bla', @Id)]" /> </xsl:stylesheet> 1}}有attr =“x”。


编辑:

如果您愿意,可以通过告诉密钥仅索引感兴趣的节点来提高效率:

XSLT 1.0

{{1}}

答案 1 :(得分:4)

您可以使用此样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*" />

<xsl:variable name="id-to-delete">
    <xsl:value-of select="/parent/test/bla[mychild/@attr = 'x']/@Id"/>
</xsl:variable>
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="bla | bla2">
    <xsl:choose>
        <xsl:when test="@Id = $id-to-delete"/>
        <xsl:otherwise>
            <xsl:copy>
                <xsl:apply-templates select="@* | node()"/>
            </xsl:copy>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
</xsl:stylesheet>
相关问题