Xml 如何在XSLT中正确分支?

Xml 如何在XSLT中正确分支?,xml,algorithm,xslt,branch,xsl-fo,Xml,Algorithm,Xslt,Branch,Xsl Fo,我正在尝试XSLT的分支。下面是我正在尝试做的事情的概述: 我正在遍历一个XML文件,并搜索具有匹配Section2、Section3和Section4的值(Section1和Section5仅用于处理目的)。如果所有值匹配,我将打印出相应的第5节,然后进行处理 我遇到的问题是,如果我没有找到三个匹配的部分,例如,如果我找到Section2和Section3,但没有找到Section4,我需要返回Section4并搜索一个值为“#”的部分。如果我找到Section2而不是Section3,我将返

我正在尝试XSLT的分支。下面是我正在尝试做的事情的概述:

我正在遍历一个XML文件,并搜索具有匹配Section2、Section3和Section4的值(Section1和Section5仅用于处理目的)。如果所有值匹配,我将打印出相应的第5节,然后进行处理

我遇到的问题是,如果我没有找到三个匹配的部分,例如,如果我找到Section2和Section3,但没有找到Section4,我需要返回Section4并搜索一个值为“#”的部分。如果我找到Section2而不是Section3,我将返回Section3搜索“#”,然后我将转到Section4并搜索正确的值(如果我没有找到它,我将查找“#”)

我不知道如何实现上述功能。目前所有的代码都在寻找匹配的值,但它没有处理上面描述的pound案例,任何帮助都将不胜感激

我已经包括了下面的XSLT和下面的XML部分

<xsl:template match="Section1">
    <xsl:param name="sec2"/>
    <xsl:param name="sec3"/>
    <xsl:param name="sec4"/>
    <xsl:apply-templates select="Section2[./@value=$sec2]">
        <xsl:with-param name="sec3" select="$sec3"/>
        <xsl:with-param name="sec4" select="$sec4"/>
    </xsl:apply-templates>
</xsl:template>
<xsl:template match="Section2">
    <xsl:param name="sec3"/>
    <xsl:param name="sec4"/>
    <xsl:apply-templates select="Section3[./@value=$sec3]">
        <xsl:with-param name="sec4" select="$sec4"/>
    </xsl:apply-templates>
</xsl:template>
<xsl:template match="Section3">
    <xsl:param name="sec4"/>
    <xsl:apply-templates select="Section4[./@value=$sec4]"/>
</xsl:template>
<xsl:template match="Section4">
    <xsl:apply-templates select="Content"/>
</xsl:template>
<xsl:template match="Section5">
    <!--Value will be printed here-->
</xsl:template>

<Section1>
<Section2 value="AP">
    <Section3 value="JP">
        <Section4 value="true">
            <Section5>Content #1</Section5>
        </Section4>
        <Section4 value="false">
            <Section5>Content #2</Section5>
        </Section4>
    </Section3>
    <Section3 value="KO">
        <Section4 value="true">
            <Section5>Content #3</Section5>
        </Section4>
        <Section4 value="false">
            <Section5>Content #4</Section5>
        </Section4>
    </Section3>
    <Section3 value="#">
        <Section4 value="true">
            <Section5>Content #5</Section5>
        </Section4>
        <Section4 value="false">
            <Section5>Content #6</Section5>
        </Section4>
    </Section3>
</Section2>
<Section2 value="LA">
    <Section3 value="#">
        <Section4 value="true">
            <Section5>Content #7</Section5>
        </Section4>
        <Section4 value="false">
            <Section5>Content #8</Section5>
        </Section4>
    </Section3>
</Section2>
<Section2 value="NA">
    <Section3 value="#">
        <Section4 value="true">
            <Section5>Content #9</Section5>
        </Section4>
        <Section4 value="false">
            <Section5>Content #10</Section5>
        </Section4>
    </Section3>
</Section2>
<Section2 value="E">
    <Section3 value="#">
        <Section4 value="#">
            <Section5>Content #11</Section5>
        </Section4>
    </Section3>
</Section2>

内容#1
内容#2
内容#3
内容#4
内容#5
内容#6
内容#7
内容#8
内容#9
内容#10
内容#11

我已经回答了我自己的问题!基本上,您需要将每个部分包装在一个变量中。测试是否返回了一个值,如果是,请打印它,否则请查找一个#,下面是代码

<xsl:template match="Section1">
    <xsl:param name="sec2"/>
    <xsl:param name="sec3"/>
    <xsl:param name="sec4"/>
    <xsl:variable name="content">
        <xsl:apply-templates select="Section2[./@value=$sec2]">
            <xsl:with-param name="sec3" select="$sec3"/>
            <xsl:with-param name="sec4" select="$sec4"/>
        </xsl:apply-templates>
    </xsl:variable>
    <xsl:choose>
        <xsl:when test="$content = ''">
            <xsl:apply-templates select="Section2[./@value='#']">
                <xsl:with-param name="sec3" select="$sec3"/>
                <xsl:with-param name="sec4" select="$sec4"/>
            </xsl:apply-templates>
        </xsl:when>
        <xsl:otherwise>
            <xsl:copy-of select="$content"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
<xsl:template match="Section2">
    <xsl:param name="sec3"/>
    <xsl:param name="sec4"/>
    <xsl:variable name="content">
        <xsl:apply-templates select="Section3[./@value=$sec3]">
           <xsl:with-param name="sec4" select="$sec4"/>
        </xsl:apply-templates>
    </xsl:variable>
    <xsl:choose>
        <xsl:when test="$content = ''">
            <xsl:apply-templates select="Section3[./@value='#']">
                <xsl:with-param name="sec4" select="$sec4"/>
            </xsl:apply-templates>
        </xsl:when>
        <xsl:otherwise>
            <xsl:copy-of select="$content"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
<xsl:template match="Section3">
    <xsl:param name="sec4"/>
    <xsl:variable name="content">
        <xsl:apply-templates select="Section4[./@value=$sec4]"/>
    </xsl:variable>
    <xsl:choose>
        <xsl:when test="$content = ''">
            <xsl:apply-templates select="Section4[./@value='*']"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:copy-of select="$content"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
<xsl:template match="Section4">
    <xsl:apply-templates select="Content"/>
</xsl:template>
<xsl:template match="Section5">
    <!--Value will be printed here-->
</xsl:template>


谢谢你的帮助

也许您可以在匹配项中添加一个or,例如
,如果没有源XML和预期的输出,这并不容易。Best regarsd,Peterwhith the or statement难道它不会同时选择#和等价项吗?这意味着如果它找到了值,它将返回该值,然后它将继续寻找#?只是想知道,谢谢你的帮助!您是对的,它将匹配第2节和第2节。您是否有一个(简化的)XML示例—查看您要做的事情总是比较容易的。谢谢,PeterI可以写一个简化的版本,但在我解释之前,让我先解释一下。(第2节、第3节、第4节)的每个键都是唯一的。我想搜索匹配的值并返回Section5。如果Section2没有匹配值,我们将查找(#,Section3,Section4),如果Section4没有匹配值,我们将查找(#,Section3,#)。这更有意义吗基本上表示一个默认值,或者如果找不到匹配的节,则表示要转到的值。我认为可以使用
xsl:key
将键应用于节2、节3、节4和#来解决它。如果不存在,则相应的密钥将为空且与处理无关。向你问好,彼得