Я отредактировал проблему, чтобы прояснить ситуацию. Я хочу, чтобы извлечь все сечения узлов с их раздела узлов и заголовка узла включены, сохраняя вложенные level.Below является выходным результатом я хотел:XSLT Transform с исходным вложенным уровнем
<?xml version="1.0" encoding="UTF-8"?>
<results>
<section id="intro" difficulty="easy">
<title>Introduction</title>
<section>
<title>Web Data and the Two Cultures</title>
</section>
</section>
<section id="syntax" difficulty="medium">
<title>A Syntax For Data</title>
<section>
<title>Base Types</title>
</section>
<section>
<title>Representing Relational Databases</title>
</section>
<section>
<title>Representing Object Databases</title>
</section>
</section>
</results>
И содержание XML, как это:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="book.xsl"?>
<book>
<title>Data on the Web</title>
<author>Serge Abiteboul</author>
<author>Peter Buneman</author>
<author>Dan Suciu</author>
<section id="intro" difficulty="easy">
<title>Introduction</title>
<p>Text ... </p>
<section>
<title>Web Data and the Two Cultures</title>
<p>Text ... </p>
<figure height="400" width="400">
<title>Traditional client/server architecture</title>
<image source="csarch.gif"/>
</figure>
<p>Text ... </p>
</section>
</section>
<section id="syntax" difficulty="medium">
<title>A Syntax For Data</title>
<p>Text ... </p>
<figure height="200" width="500">
<title>Graph representations of structures</title>
<image source="graphs.gif"/>
</figure>
<p>Text ... </p>
<section>
<title>Base Types</title>
<p>Text ... </p>
</section>
<section>
<title>Representing Relational Databases</title>
<p>Text ... </p>
<figure height="250" width="400">
<title>Examples of Relations</title>
<image source="relations.gif"/>
</figure>
</section>
<section>
<title>Representing Object Databases</title>
<p>Text ... </p>
</section>
</section>
</book>
Вот мое решение, которое является неправильным:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/book">
<xsl:apply-templates select="section"/>
</xsl:template>
<xsl:template match="section">
<xsl:element name="section">
<xsl:for-each select="@*">
<xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
</xsl:for-each>
</xsl:element>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="section"/>
</xsl:template>
<xsl:template match="title">
<xsl:element name="title">
<xsl:for-each select="@*">
<xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
</xsl:for-each>
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Я думал об использовании рекурсии, но я не нашел, как это сделать в XSLT. Должны быть другие способы. Спасибо заранее!
[Вы используете «pull» вместо «push» типа программирования в XSLT] (http://stackoverflow.com/questions/19718048/how-to-build-a-recursive-navigation-in-xsl -xml/19718283 # 19718283) Попробуйте использовать более 'apply-templates' вместо' for-each' –
Ваш XSLT-код абсолютно не имеет смысла. Вы скопировали его откуда-то, чтобы показать, что вы что-то пробовали? Все, что вам нужно, это [шаблон преобразования идентичности] (http://en.wikipedia.org/wiki/Identity_transform#Using_XSLT) и еще один шаблон для переименования 'результатов' в' book'. –
Я написал это. Я работал над этим со вчерашнего дня, и я все еще пытаюсь. Мой XSLT-код выше предназначался для печати элементов в html-документе. Но теперь я знаю, что должен сделать атрибут метода xsl: output element как «xml «Значит, больше не будет таких символов, как <. Но я до сих пор не могу найти способ сохранить формат отступа и вложенные иерархии. @ michael.hor257k – Tqri