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
如何查找XML之外的章节号_Xml_Xslt - Fatal编程技术网

如何查找XML之外的章节号

如何查找XML之外的章节号,xml,xslt,Xml,Xslt,我正在编写一个XML和XSL来基于一本书创建一个HTML文档。所有文件都称为chapter-1.xml,chapter-2.xml等,每章中的标题将以章节开始,然后是章节标题。这些章节将由不同的人编写,因此每个章节必须位于每个xml文件中 XML: 喋喋不休 XSL: 我有一个PHP来创建HTML: <?php # LOAD XML FILE $XML = new DOMDocument(); $XML->load('chapter-1.xml'); $xslt = ne

我正在编写一个XML和XSL来基于一本书创建一个HTML文档。所有文件都称为chapter-1.xmlchapter-2.xml等,每章中的标题将以章节开始,然后是章节标题。这些章节将由不同的人编写,因此每个章节必须位于每个xml文件中

XML:


喋喋不休
XSL:




我有一个PHP来创建HTML:

<?php
# LOAD XML FILE
$XML = new DOMDocument();
$XML->load('chapter-1.xml');

$xslt = new XSLTProcessor();
$XSL = new DOMDocument();
$XSL->load('chapter.xsl');
$xslt->importStylesheet( $XSL );
print $xslt->transformToXML( $XML );
?>
load('chapter-1.xml');
$xslt=新的XSLTProcessor();
$XSL=newdomdocument();
$XSL->load('chapter.XSL');
$xslt->importStylesheet($XSL);
打印$xslt->transformToXML($XML);
?>
我希望它在HTML中显示为“1.这是第一章”。在不使用标记中的另一个属性的情况下,是否仍然可以解决此问题?每个文档只有一个章节标签。有没有办法用CSS解决这个问题,或者从文件名中获取章节号


我还需要为所有带有编号的章节创建一个目录。

由于您的代码没有任何意义,恐怕这是解决类似您的问题的一般思路

控制您的输入

章节
节点提供单独的XML文件很不方便。因此,如果您自己生成这些文件,请确保在同一XML文件中生成所有
章节
元素,例如:

<chapters>
 <chapter title="This is the first chapter">
    <paragraph title="Start">Blablabla</paragraph>
 </chapter>
 <chapter title="xy">
    <paragraph title="Start">Blablabla</paragraph>
 </chapter>
</chapters>
<?xml version="1.0" encoding="utf-8" ?>
<toc>
<chapter num="1" title="First Chapter"/>
<chapter num="2" title="Second Chapter"/>
<chapter num="3" title="Inserted Chapter"/>
<chapter num="4" title="Third Chapter"/>
<chapter num="5" title="Last Chapter"/>
<chapter num="6" title="Last Minute Chapter"/>
</toc>
<?xml version="1.0" encoding="utf-8" ?>
<chapter title="Third Chapter">
    <paragraph title="Start">Lorem ipsum ... </paragraph>
    <paragraph title="End">...nota bene.</paragraph>
</chapter>
或者,您也可以为此使用
xsl:number

问答

是否有任何方法可以解决此问题而不使用 标签

是的,使用这两种方法之一直接在HTML输出中生成编号

有没有一种方法可以通过CSS解决这个问题,或者通过获取章节 文件名中的数字是多少


试图用CSS解决这个问题是个坏主意。CSS是关于设计文档的样式,而不是更改内容。我还建议在转换文档时不要依赖文件名。

假设您有一个名为“toc.xml”的文档,它与样式表位于同一目录中,如下所示:

<chapters>
 <chapter title="This is the first chapter">
    <paragraph title="Start">Blablabla</paragraph>
 </chapter>
 <chapter title="xy">
    <paragraph title="Start">Blablabla</paragraph>
 </chapter>
</chapters>
<?xml version="1.0" encoding="utf-8" ?>
<toc>
<chapter num="1" title="First Chapter"/>
<chapter num="2" title="Second Chapter"/>
<chapter num="3" title="Inserted Chapter"/>
<chapter num="4" title="Third Chapter"/>
<chapter num="5" title="Last Chapter"/>
<chapter num="6" title="Last Minute Chapter"/>
</toc>
<?xml version="1.0" encoding="utf-8" ?>
<chapter title="Third Chapter">
    <paragraph title="Start">Lorem ipsum ... </paragraph>
    <paragraph title="End">...nota bene.</paragraph>
</chapter>

假设您的“chapter-3.xml”文档如下所示:

<chapters>
 <chapter title="This is the first chapter">
    <paragraph title="Start">Blablabla</paragraph>
 </chapter>
 <chapter title="xy">
    <paragraph title="Start">Blablabla</paragraph>
 </chapter>
</chapters>
<?xml version="1.0" encoding="utf-8" ?>
<toc>
<chapter num="1" title="First Chapter"/>
<chapter num="2" title="Second Chapter"/>
<chapter num="3" title="Inserted Chapter"/>
<chapter num="4" title="Third Chapter"/>
<chapter num="5" title="Last Chapter"/>
<chapter num="6" title="Last Minute Chapter"/>
</toc>
<?xml version="1.0" encoding="utf-8" ?>
<chapter title="Third Chapter">
    <paragraph title="Start">Lorem ipsum ... </paragraph>
    <paragraph title="End">...nota bene.</paragraph>
</chapter>

乱数假文。。。
…没什么好处。
现在,应用以下XSLT样式表:

<?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" encoding="utf-8"/>

<xsl:variable name="toc" select="document('toc.xml')/toc" />

<xsl:template match="/">
<html>
<body>

<xsl:variable name="chapTitle" select="chapter/@title" />
<xsl:variable name="chapNum" select="$toc/chapter[@title=$chapTitle]/@num"/>

<h1><xsl:value-of select="concat($chapNum, '. ', $chapTitle)"/></h1>

<xsl:for-each select="chapter/paragraph">
    <h2><xsl:value-of select="@title"/></h2>
    <p><xsl:value-of select="."/></p>
</xsl:for-each>
<h4><xsl:value-of select="concat('Next chapter: ', $toc/chapter[$chapNum+1]/@title)"/></h4>

</body>
</html>
</xsl:template>
</xsl:stylesheet>

将产生以下结果:

<html>
   <body>
      <h1>4. Third Chapter</h1>
      <h2>Start</h2>
      <p>Lorem ipsum ... </p>
      <h2>End</h2>
      <p>...nota bene.</p>
      <h4>Next chapter: Last Chapter</h4>
   </body>
</html>

4.第三章
开始
同侧眼线

终点 …没什么好处

下一章:最后一章
这个问题不清楚。显示样式表的一个片段(如果不是整个的话)以及预期的输出。你自己控制输入文件的结构了吗?现在好了吗?我还没有任何CSS。更好,但仍然:你是如何解决你发布的问题的?我不相信你真的试图自己解决这个问题。另外,展示更多的样式表(如果你可以像这样使用每个样式表,那么你是如何收集所有章节元素的,就像你所说的那样,每个章节都在单独的文件中?)。正如我所说,每个文档中只有一个章节元素。我需要把他们的号码写进每个文件的标题里。这是我的挑战。这是我第一个使用XML的项目,所以我可以向大家保证,我实际上已经搜索和尝试了一段时间。我甚至不知道是否真的有办法做到这一点,而不向每个元素添加新属性进行编号。如果我的问题听起来令人困惑,我很抱歉,但这是刚开始使用XML的结果。(还添加了php代码)我认为(我不确定也不知道如何)可以使用php将带有源XML文件名的参数发送到XSLT样式表。我认为没有其他方法,至少XSLT1.0中没有(我相信PHP正在使用XSLT1.0)。这里有一个教训:文件名不是存储有意义内容的合适位置。非常感谢您的回答。不幸的是,这些章节将由不同的人放在一起,因此有必要找到一种使用不同文件名的方法。最新提到的解决方案@ USER 3016153接近我正在努力实现的。@ JANLLNDSO,如果这种方法对你不起作用,那么你应该考虑更新你的问题,包括额外的相关信息,希望下一个答案某人手工艺将符合你的所有约束。问一个广泛的问题,这是开放的竞争,你只能期待广泛和一般的答案。请记住这一点,以后的帖子。看起来不错!我将尝试一下,并切换您的答案,如果它的工作!非常感谢@janlindso求求你了。看来我离你最近了。谢谢