Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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

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模板格式化xml元素_Xml_Xslt - Fatal编程技术网

重用xslt模板格式化xml元素

重用xslt模板格式化xml元素,xml,xslt,Xml,Xslt,我有一个xml文件 <catalog> <s1> <cd> <title>TRACK A</title> <artist>ARTIST A</artist> </cd> </s1> <s2> <cd> <title>TRACK B</title> <ar

我有一个xml文件

<catalog>
  <s1>
    <cd>
      <title>TRACK A</title>
      <artist>ARTIST A</artist>
    </cd>
  </s1>
  <s2>
    <cd>
      <title>TRACK B</title>
      <artist>TRACK B</artist>
    </cd>
  </s2>
  <s3>
    <cd>
      <title>TRACK C</title>
      <artist>ARTIST C</artist>
    </cd>
    <cd>
      <title>TRACK D</title>
      <artist>ARTIST D</artist>
    </cd>
  </s3>
</catalog>

跟踪A
艺术家A
B轨道
B轨道
C轨道
艺术家C
D轨道
艺术家D
我试图设置模板,使s1和s3元素的格式相同,但s2元素的格式不同

我拥有的xslt是

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

  <xsl:for-each select="catalog/s1">
    <xsl:call-template name="style1"/> 
  </xsl:for-each>

  <xsl:for-each select="catalog/s2">
    <xsl:call-template name="style2"/>    
  </xsl:for-each>

  <xsl:for-each select="catalog/s3">
    <xsl:call-template name="style1"/> 
  </xsl:for-each>

</xsl:template>

<xsl:template match="cd" name="style1">
  <b><xsl:value-of select="title" /></b>
  <b><xsl:value-of select="artist" /></b>
</xsl:template>

<xsl:template match="cd" name="style2">
  <i><xsl:value-of select="title" /></i>
</xsl:template>

</xsl:stylesheet>

但它没有产生任何产出。我认为我需要,但这样做似乎称之为“风格1”

为什么这不能产生产出

多谢各位

Ryan

将“cd/”放在您的select语句中:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

  <xsl:for-each select="catalog/s1">
    <xsl:call-template name="style1"/> 
  </xsl:for-each>

  <xsl:for-each select="catalog/s2">
    <xsl:call-template name="style2"/>    
  </xsl:for-each>

  <xsl:for-each select="catalog/s3">
    <xsl:call-template name="style1"/> 
  </xsl:for-each>

</xsl:template>

<xsl:template match="cd" name="style1">
  <b><xsl:value-of select="cd/title" /></b>
  <b><xsl:value-of select="cd/artist" /></b>
</xsl:template>

<xsl:template match="cd" name="style2">
  <i><xsl:value-of select="cd/title" /></i>
</xsl:template>

</xsl:stylesheet>


Doh。谢谢。我在for each中的select语句中添加了cd,这似乎更有意义,因为我的模板旨在格式化“cd”,欢迎使用。XSLT语法规则在您习惯之前并不十分明显。如果我的回答能解决你的问题,你也可以接受。