Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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
Xml 特定元素的XSL计数编号_Xml_Xslt_Count_Xsl Choose - Fatal编程技术网

Xml 特定元素的XSL计数编号

Xml 特定元素的XSL计数编号,xml,xslt,count,xsl-choose,Xml,Xslt,Count,Xsl Choose,因此,我试图根据父元素中有多少元素来打印稍微不同的文本。给我带来麻烦的代码是when-xsl代码 <?xml version="1.0" standalone="no" ?> <?xml-stylesheet publisher="text/xsl" href="books3.xsl"?> <books> <book> <title type="fiction">HG</title>

因此,我试图根据父元素中有多少
元素来打印稍微不同的文本。给我带来麻烦的代码是when-xsl代码

<?xml version="1.0" standalone="no" ?>
<?xml-stylesheet publisher="text/xsl" href="books3.xsl"?>
<books>
    <book>
        <title type="fiction">HG</title>
        <author>Test</author>
        <publisher>Junk</publisher>
        <year>2001</year>
        <price>29</price>
    </book>
    <book>
        <title type="fiction">HP</title>
        <author>Test1</author>
        <author>Test1</author>
        <publisher>Junk1</publisher>
        <year>2002</year>
        <price>29</price>
    </book>
    <book>
        <title type="fiction">HG</title>
        <author>Test</author>
        <publisher>Junk</publisher>
        <year>2001</year>
        <price>40</price>
    </book>
</books>

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output  method="html" indent="yes" version="4.0" />   
  <xsl:template match="/">
    <div>
      <xsl:for-each select="//book[price &lt; 30]">
        <span id="title"><xsl:value-of select="title"/></span>
        <xsl:choose>
            <xsl:when test="count(//author) &gt; 1">
                <span id="author"><xsl:value-of select="author"/> et al</span>
            </xsl:when>
            <xsl:when test="count(//author) = 1 ">
                <span id="author"><xsl:value-of select="author"/></span>
            </xsl:when>
        </xsl:choose>
        <span id="price"><xsl:value-of select="price"/></span>
      </xsl:for-each>
    </div>
  </xsl:template>
</xsl:stylesheet>

汞
试验
废旧物品
2001
29
惠普
测试1
测试1
六月一日
2002
29
汞
试验
废旧物品
2001
40
等
表达式:

count(//author)
统计整个文档中的作者数。计算本书的作者数量——即在以下背景下:

<xsl:for-each select="//book[price &lt; 30]">
假设所有作者都是这本书的孩子

--
顺便说一句,你可以通过总是输出第一作者的名字来简化这个过程,如果有多个作者,可以加上“et al.”

count(author)