XSLT:获取具有指定属性的 parents 中的所有 children

XSLT: Get all children of parents with specified attributes

鉴于此 XML:

<?xml version="1.0" encoding="iso-8859-2" ?>
<r>
    <products start="5" stop="8">
        <p>
            <id> 50 </id>
            <name> Murphy </name>
            <price> 33 </price>
        </p>
        <p>
            <id> 10 </id>
            <name> Margarethe </name>
            <price> 11 </price>
        </p>
    </products>
    <products start="1" stop="4">
        <p>
            <id> 555 </id>
            <name> XXXX</name>
            <price> 333 </price>
        </p>
        <p>
            <id> 10 </id>
            <name> Mexico </name>
            <price> 11 </price>
        </p>
    </products>
    <products start="1" stop="4">
        <p>
            <id> 30 </id>
            <name> HKoney </name>
            <price> 11 </price>
        </p>
        <p>
            <id> 10 </id>
            <name> HMargarethe </name>
            <price> 11 </price>
        </p>
    </products>
</r>

使用此 XSL:

<?xml version="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:template match="products[@start &lt; 2 and @stop &gt; 2]" >
    <xsl:call-template name="test"/>
</xsl:template>

<xsl:template name="test">
     <test><xsl:value-of select="*/name"/></test>
</xsl:template>

<xsl:template match="products"/>

</xsl:stylesheet>

有这个输出:

<?xml version="1.0" encoding="UTF-8"?>
<test> XXXX</test>
<test> HKoney </test>

Q : Why am I not getting all the <p> with parent of specified attributes, only the first one? Mexico and HMargarethe was expected also.


期望的输出:

<?xml version="1.0" encoding="UTF-8"?>
<test> XXXX</test>
<test> Mexico </test>
<test> HKoney </test>
<test> HMargerethe</test>

NOTE: I wish to have it working without for-each loop.

您必须处理所有 p 个元素,请参阅此处的 xsl:for-each

<xsl:template name="test">
    <test>
        <xsl:for-each select="*">
            <xsl:value-of select="name"/>
        </xsl:for-each>
    </test>
</xsl:template>

您可以尝试直接在 <name> 元素上使用 apply-templates

<?xml version="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:template match="products[@start &lt; 2 and @stop &gt; 2]" >
        <xsl:apply-templates select="p/name"/>
    </xsl:template>

    <xsl:template match="name">
        <test>
            <xsl:value-of select="."/>
        </test>
    </xsl:template>

    <xsl:template match="products"/>

</xsl:stylesheet>

Q : Why am I not getting all the <p> with parent of specified attributes, only the first one? Mexico and HMargarethe was expected also.

注意模板 test 只为每个 products 元素调用一次 :

<xsl:template match="products[@start &lt; 2 and @stop &gt; 2]" >
    <xsl:call-template name="test"/>
</xsl:template>

每次调用模板时,您都会拉取多个 name 元素以供 <xsl:value-of> 处理。根据 MSDN 关于 <xsl:value-of>select 属性:

Required. The Expressions (XML) to be evaluated against the current context. The results are converted to a string, as by a call to the string() function. A node-set is converted to a string by inserting the string value of the first node in the set.

规范也讲述了同样的故事,参见 W3C xslt value-of and xpath function string()

修改了不使用for循环的代码

请使用下面的代码,它按预期工作

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/" name ="ParentCopy">
<xsl:param name="LoopCountA">1</xsl:param>
<xsl:variable name = "Count" >
        <xsl:value-of select = "count(r/products)"/>
    </xsl:variable>
    <xsl:choose>
    <xsl:when test ="($LoopCountA  &lt;= $Count) ">
<xsl:choose>
            <xsl:when test ="r/products[$LoopCountA][@start &lt; 2 and @stop &gt; 2]">
                <test>
    <xsl:value-of select = "r/products[$LoopCountA]/p[1]/name"/>
                </test> 
<test>
                    <xsl:value-of select = "r/products[$LoopCountA]/p[2]/name"/>
</test>
            </xsl:when>
</xsl:choose>
    <xsl:call-template name="ParentCopy">
            <xsl:with-param name="LoopCountA">
<xsl:value-of select="$LoopCountA + 1"/>
            </xsl:with-param>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>

如有任何其他疑问,请告诉我