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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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:for每个具有属性的元素_Xml_Xslt - Fatal编程技术网

Xml xsl:for每个具有属性的元素

Xml xsl:for每个具有属性的元素,xml,xslt,Xml,Xslt,我有这个xml <Books> <Book Cover="Audiobook" xmlns="http://tempuri.org/XMLSchema.xsd"> <Title>Middlesex: A Novel</Title> <FBT>The Woman Warrior</FBT> <FBT>The House on Mango Street</FBT> <Isbn>

我有这个xml

<Books>
<Book Cover="Audiobook" xmlns="http://tempuri.org/XMLSchema.xsd">
  <Title>Middlesex: A Novel</Title>
  <FBT>The Woman Warrior</FBT>
  <FBT>The House on Mango Street</FBT>
  <Isbn>0312427735</Isbn>
  <Author>
    <Name>
      <First>Jeffrey</First>
      <Last>Eugenides</Last>
    </Name>
    <Contact Office="Office1">
      <Phone>110-222-3333</Phone>
    </Contact>
  </Author>
  <Publisher>Picador</Publisher>
  <Year Edition="1">2002</Year>
</Book>
<Book Cover="Audiobook" xmlns="http://tempuri.org/XMLSchema.xsd">
  <Title>Me Talk Pretty One Day</Title>
  <FBT>Dress Your Family in Corduroy and Denim</FBT>
  <FBT>Naked</FBT>
  <Isbn>9780316776967</Isbn>
  <Author>
    <Name>
      <First>David</First>
      <Last>Sedaris</Last>
    </Name>
    <Contact Office="Office1">
      <Phone>119-222-3333</Phone>
    </Contact>
   </Author>
  <Publisher>Back Bay Books</Publisher>
  <Year Edition="2">2001</Year>
 </Book>
</Books>

米德尔塞克斯:一部小说
女勇士
芒果街的小屋
0312427735
杰弗里
尤金尼德斯
110-222-3333
皮卡多
2002
总有一天我会说很多的
给家人穿上灯芯绒和牛仔布
裸体的
9780316776967
大卫
塞达里斯
119-222-3333
后海湾图书
2001
我遇到的主要问题是当我尝试在xslt代码中进行迭代时

<xsl:for-each select="Books/Book">

我提到的任何一本书都没有提到这一点。我试图查找为什么会出现这个问题,但仍然不确定,我从一个book实例中删除了属性,它最终能够传递到子元素,以便将数据放入表中。 我的第一个问题是为什么在我的book元素中包含属性甚至会对我的for each产生影响

第二,有没有解决这个问题的办法?我想把属性保存在书中

编辑:这是我做循环的部分

 <xsl:for-each select="Books/Book">
          <tr style="font-size: 12pt; font-family: verdana">
            <td>

              <xsl:value-of select="Title"/>

            </td>
            <td>
              <xsl:value-of select="Isbn"/>
            </td>
            <td>
              <xsl:value-of select="Author"/>
            </td>
            <td>
              <xsl:value-of select="Publisher"/>
            </td>
            <td>
              <xsl:value-of select="Year"/>
            </td>
          </tr>
        </xsl:for-each>

您似乎使用了正确的语法

 <xsl:for-each select="Books/Book">
     <xsl:value-of select="Title" />
     <xsl:value-of select="FBT" /> 
 </xsl:for-each>

如果您使用相同的方法,那么应该可以从XML中获取标题和FBT。如果可能,尝试在表中填充完整的XML并检查哪里出错。从语法的角度来看,根据

令人惊讶的是,以下XML工作正常,名称空间可能有问题(xmlns=”http://tempuri.org/XMLSchema.xsd"). 请检查一下

    <Books>
<Book Cover="Audiobook" xmlns="http://tempuri.org/XMLSchema.xsd">
  <Title>Middlesex: A Novel</Title>
  <FBT>The Woman Warrior</FBT>
  <FBT>The House on Mango Street</FBT>
  <Isbn>0312427735</Isbn>
  <Author>
    <Name>
      <First>Jeffrey</First>
      <Last>Eugenides</Last>
    </Name>
    <Contact Office="Office1">
      <Phone>110-222-3333</Phone>
    </Contact>
  </Author>
  <Publisher>Picador</Publisher>
  <Year Edition="1">2002</Year>
</Book>
<Book Cover="Audiobook" xmlns="http://tempuri.org/XMLSchema.xsd">
  <Title>Me Talk Pretty One Day</Title>
  <FBT>Dress Your Family in Corduroy and Denim</FBT>
  <FBT>Naked</FBT>
  <Isbn>9780316776967</Isbn>
  <Author>
    <Name>
      <First>David</First>
      <Last>Sedaris</Last>
    </Name>
    <Contact Office="Office1">
      <Phone>119-222-3333</Phone>
    </Contact>
   </Author>
  <Publisher>Back Bay Books</Publisher>
  <Year Edition="2">2001</Year>
 </Book>
</Books>

米德尔塞克斯:一部小说
女勇士
芒果街的小屋
0312427735
杰弗里
尤金尼德斯
110-222-3333
皮卡多
2002
总有一天我会说很多的
给家人穿上灯芯绒和牛仔布
裸体的
9780316776967
大卫
塞达里斯
119-222-3333
后海湾图书
2001

元素上缺少名称空间。
因此,在
xsl:stylesheet
元素上使用

xmlns:bk="http://tempuri.org/XMLSchema.xsd"
它被设置为
Book
元素的默认值,使其

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

...
然后通过将模板更改为,将此更改应用于模板

<xsl:template match="/">
  <xsl:for-each select="Books/bk:Book">
      <tr style="font-size: 12pt; font-family: verdana">
        <td>
          <xsl:value-of select="bk:Title"/>
        </td>
        <td>
          <xsl:value-of select="bk:Isbn"/>
        </td>
        <td>
          <xsl:value-of select="concat(bk:Author/bk:Name/bk:First,' ', bk:Author/bk:Name/bk:Last)"/>
        </td>
        <td>
          <xsl:value-of select="bk:Publisher"/>
        </td>
        <td>
          <xsl:value-of select="bk:Year"/>
        </td>
      </tr>
  </xsl:for-each>
</xsl:template>

可以删除包装模板。

结果如预期。

是的,这很奇怪。。我做了一个编辑来显示我的循环是如何运行的,这与你的非常相似。请检查一下,现在发现了这个问题。如果您感到满意,并且问题已解决,请投票支持答案:)Thx