简体   繁体   English

XSLT模板输出子元素

[英]XSLT template to output child elements

I'm having trouble with XSLT and need help. 我在使用XSLT时遇到问题,需要帮助。 I replaced my original post with this one. 我用这个帖子代替了原来的帖子。 I have an XML file with an empty element that I eventually want to expand using content from a second XML file. 我有一个带有空元素的XML文件,最终我希望使用第二个XML文件中的内容进行扩展。 I am using the xsltproc (XSLT 1.0) for my processing engine on Ubuntu. 我在Ubuntu上使用xsltproc (XSLT 1.0)作为处理引擎。

I noticed the Identity Template is not copying the DOCTYPE from the input, if it is supposed to. 我注意到身份模板没有从输入中复制DOCTYPE(如果应该这样做)。

I created a simplified test input file and simplified XSLT file. 我创建了一个简化的测试输入文件和一个简化的XSLT文件。 I still cannot get the "genres" XSLT Template to do anything. 我仍然无法获得“类型” XSLT模板来执行任何操作。 I changed that template to remove the named element - but it does not do that. 我更改了该模板以删除命名的元素-但它不会那样做。

New Input XML - 新输入XML-

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE tellico PUBLIC '-//Robby Stephenson/DTD Tellico V11.0//EN' 'http://periapsis.org/tellico/dtd/v11/tellico.dtd'> <tellico xmlns="http://periapsis.org/tellico/" syntaxVersion="11"> <collection title="My Videos" type="3"> <entry id="1002"> <title>Midsomer Murders -- Set 25</title> <id>1002</id> <comments>Includes bonus material</comments> <year>2013</year> <cover>file:///data/www/htdocs/videodb/cache/img/1002.jpg</cover> <running-time>90</running-time> <medium>DVD</medium> <genres></genres> <set>Yes</set> <count>3</count> <location>2</location> </entry> </collection> </tellico> 

New XSLT Transform file - 新的XSLT转换文件-

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- the identity template (copies your input verbatim) --> <xsl:template match="node() | @*"> <xsl:copy> <xsl:apply-templates select="node() | @*" /> </xsl:copy> </xsl:template> <!-- special templates only for things that need them --> <xsl:template match="genres"/> </xsl:stylesheet> 

Process command: 处理命令:

 xsltproc -o merged.xml --novalid Identity.xslt test-input-merge.xml 

I have to use the --novalid option because the periapsis.org site is down at this time. 我必须使用--novalid选项,因为periapsis.org网站此时已关闭。

And the output I get is: 我得到的输出是:

 <?xml version="1.0"?> <tellico xmlns="http://periapsis.org/tellico/" syntaxVersion="11"> <collection title="My Videos" type="3"> <entry id="1002"> <title>Midsomer Murders -- Set 25</title> <id>1002</id> <comments>Includes bonus material</comments> <year>2013</year> <cover>file:///data/www/htdocs/videodb/cache/img/1002.jpg</cover> <running-time>90</running-time> <medium>DVD</medium> <genres/> <set>Yes</set> <count>3</count> <location>2</location> </entry> </collection> </tellico> 

According to the XSLT transform, and my limited understanding - the "genres" element should have not been included in the output, but it is. 根据XSLT转换,据我有限的理解-输出中不应该包含“流派”元素,而应该包括在内。 This is just a test, for now, to try and figure out how to get the genres template to do something. 目前,这只是一种测试,目的是试图弄清楚如何使流派模板做某事。

I hope this improves on my original post and someone sees what is wrong. 我希望这会在我的原始帖子中有所改善,并且让别人看到问题所在。 Thanks to all for any help you can provide. 感谢所有您提供的帮助。

match="entry[genres]" means that the matched element is entry with genres inside. match="entry[genres]"表示匹配的元素是内部具有genres entry

So this template processes the whole entry element (not genres ). 因此,此模板处理整个entry元素(不是genres )。

And what is the result? 结果如何? I made a test using your XML and XSLT and got: 我使用您的XML和XSLT进行了测试,并得到:

<?xml version="1.0" encoding="ISO-8859-1"?>
    Genre data goes here

So this template outputs just its content, instead of the whole source, and the identity template has no chance to process anything. 因此,此模板仅输出其内容,而不是整个源,因此身份模板没有机会进行任何处理。

Probably you should write match="entry/genres" . 可能您应该编写match="entry/genres" Or maybe just match="genres" would be enough? 也许只是match="genres"就足够了?

Edit 编辑

The real cause why your template did not work was that you forgot about namespace issues, namely the namespace declaration in the root tag. 模板不起作用的真正原因是您忘记了名称空间问题,即根标记中的namespace声明。

It causes that: 它导致:

  • all your tags are in the above mentioned namespace, not in the default (empty) namespace, 您所有的标签都位于上述命名空间中,而不是默认(空)命名空间中,
  • your template tried to match genres element in the empty namespace . 您的模板尝试匹配空名称空间中的genres元素。

So you can leave attributes in your source XML, including namespace , but make the following changes in your XSLT: 因此,您可以在源XML中保留属性,包括namespace ,但在XSLT中进行以下更改:

  • The stylesheet tag must contain declaration of this namespace with some prefix, eg xmlns:tel="http://periapsis.org/tellico/" . stylesheet标记必须包含带有某些前缀的该名称空间的声明,例如xmlns:tel="http://periapsis.org/tellico/"
  • The match attribute in your template must refer to just this namespace, ie include the above prefix: <xsl:template match="tel:genres"/> . 模板中的match属性必须仅引用此名称空间,即包括上述前缀: <xsl:template match="tel:genres"/>

As far as the DOCTYPE row is concerned: DOCTYPE行而言:

Using xsltransform.net I tried to include this row in the source XML but got the following error: 使用xsltransform.net,我尝试将此行包含在源XML中,但出现以下错误:

Error on line 1 column 2 of http://periapsis.org/:
  SXXP0003: Error reported by XML parser: The markup declarations
  contained or pointed to by the document type declaration must
  be well-formed.

Probably the reason is inaccessibility or some errors in the referred .dtd file. 可能的原因是所引用的.dtd文件无法访问或某些错误。 So I agree with you, that this row should be removed. 因此,我同意你的看法,即应删除这一行。

I don't know why match="entry[genres]" isn't matching the entry element, but it's wrong anyway. 我不知道为什么match="entry[genres]"entry元素不匹配,但这还是错误的。 What you need is something like: 您需要的是这样的:

<xsl:template match="genres">
  <xsl:copy-of select="document($with)"/>
</xsl:template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM