Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.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
Html 从书本样式的XML进行XSLT转换_Html_Xml_Xslt_Transformation - Fatal编程技术网

Html 从书本样式的XML进行XSLT转换

Html 从书本样式的XML进行XSLT转换,html,xml,xslt,transformation,Html,Xml,Xslt,Transformation,我正在尝试从XML进行XSLT转换,我想转换书籍内容,我有这样的XML文件,我需要用XSLT进行转换。我知道为每个部分创建模板是很热门的,但我只有XSLT的基本经验,所以我不知道如何应用它 <book> <section id="1"> <name>Heading First chapter</name> <p><i/> some text </p> <

我正在尝试从XML进行XSLT转换,我想转换书籍内容,我有这样的XML文件,我需要用XSLT进行转换。我知道为每个部分创建模板是很热门的,但我只有XSLT的基本经验,所以我不知道如何应用它

<book>
    <section id="1">
        <name>Heading First chapter</name>
        <p><i/> some text </p>
        <p> other text </p>
        <subsection id="1.1">
            <name>Heading second chapter</name>
                <subsubsection id="1.1.1">
                    <name>Heading third chapter</name>
                    <p>some text</p>
                </subsubsection>
        </subsection>
    </section>
</book>

标题第一章
一些文本

其他文本

标题第二章 标题第三章 一些文本

我想要的是如下所示的HTML:

<div>
    <h1>1 Heading First chapter</h1>
    <p><i>some text</i>
    <p>other text</p>
    <div>
        <h2>1.1 Heading second chapter</h2>
        <div>
            <h3>1.1.1 Heading third chapter</h3>
            <p>some text</p>        
        </div>
    </div>
</div>

第一章标题1
一些文本
其他文本

1.1标题第二章 1.1.1标题第三章 一些文字

我的尝试:

<xsl:template match="/">
         <body>
        <xsl:for-each select="book">
          <xsl:apply-templates select="."></xsl:apply-templates>
        </xsl:for-each>    
      </body>
    </html>
  </xsl:template>

<xsl:template match="p">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>

<xsl:template match="p[i]">
    <p>
      <em>
        <xsl:value-of select="."/>
      </em>
    </p>
  </xsl:template>

<xsl:template match="section/name">
    <p>
      <h2>            
          <xsl:value-of select="."/>
        </a>
      </h2>
    </p>
  </xsl:template>



您知道如何进行转换吗?

当我应用下一个XSLT时:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.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="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="book">
        <body>
            <xsl:apply-templates select="node()" />
        </body>
    </xsl:template>

    <xsl:template match="*[contains(local-name(), 'section')]">
        <div>
            <xsl:apply-templates select="node()" />
        </div>
    </xsl:template>

    <xsl:template match="name">
        <xsl:element name="h{count(ancestor::*[contains(local-name(), 'section')])}">
            <xsl:value-of select="concat(parent::*/@id, ' ', .)" />
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

到提供的输入XML

<?xml version="1.0" encoding="UTF-8"?>
<book>
    <section id="1">
        <name>Heading First chapter</name>
        <p>
            <i/> some text </p>
        <p> other text </p>
        <subsection id="1.1">
            <name>Heading second chapter</name>
            <subsubsection id="1.1.1">
                <name>Heading third chapter</name>
                <p>some text</p>
            </subsubsection>
        </subsection>
    </section>
</book>

标题第一章

一些文本

其他文本

标题第二章 标题第三章 一些文本

它产生:

<?xml version="1.0" encoding="UTF-8"?>
<body>
    <div>
        <h1>1 Heading First chapter</h1>
        <p>
            <i/> some text </p>
        <p> other text </p>
        <div>
            <h2>1.1 Heading second chapter</h2>
            <div>
                <h3>1.1.1 Heading third chapter</h3>
                <p>some text</p>
            </div>
        </div>
    </div>
</body>

第一章标题1

一些文本

其他文本

1.1标题第二章 1.1.1标题第三章 一些文本


您尝试了哪些不起作用的方法?