简体   繁体   English

XSLT 2.0至1.0

[英]XSLT 2.0 to 1.0

Lots of help here to convert XSLT 1.0 to 2.0, but I need to go the other way! 这里有很多将XSLT 1.0转换为2.0的帮助,但是我需要另辟!径!

OK, I have an XSL file working in XSLT 2.0: I need to convert this file to XSLT 1.0. 好的,我在XSLT 2.0中有一个XSL文件:我需要将此文件转换为XSLT 1.0。 Right now, I know the transformation is getting hung up on "result-document" but I'm not sure how to fix this. 现在,我知道转换正挂在“结果文档”上,但是我不确定如何解决此问题。 And, I'm not sure if my other info will translate to 1.0: will my value-of select= statements still work? 而且,我不确定我的其他信息是否会转换为1.0:我的value of select =语句是否仍然有效? help! 救命! (thanks)! (谢谢)!

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.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="/">
    <xsl:result-document href="output.xml" method="xml">
        <playlist>
            <xsl:for-each select="//dict[preceding-sibling::*[1]='Tracks']/dict">
                <Track>
                    <Artist>
                        <xsl:value-of select="*[preceding-sibling::*[1][local-name()='key'] = 'Artist']"/>
                    </Artist>
                    <Album>
                        <xsl:value-of select="*[preceding-sibling::*[1][local-name()='key'] = 'Album']"/>
                    </Album>
                    <Songname>
                        <xsl:value-of select="*[preceding-sibling::*[1][local-name()='key'] = 'Name']"/>
                    </Songname>
                    <TrackID>
                        <xsl:value-of select="*[preceding-sibling::*[1][local-name()='key'] = 'Track ID']"/>
                    </TrackID>
                    <TagID>
                        <xsl:value-of select="*[preceding-sibling::*[1][local-name()='key'] = 'Tag ID']"/>
                    </TagID>
                </Track>
            </xsl:for-each>

            <xsl:for-each select="//dict[preceding-sibling::*[1]='Upload Information']">
                <Description>
                    <xsl:value-of select="*[preceding-sibling::*[1][local-name()='key'] = 'Playlist Description']"/>
                </Description>
                <Title>
                    <xsl:value-of select="*[preceding-sibling::*[1][local-name()='key'] = 'Playlist Title']"/>
                </Title>
                <Username>
                    <xsl:value-of select="*[preceding-sibling::*[1][local-name()='key'] = 'Username']"/>
                </Username>
                <Token>
                    <xsl:value-of select="child::*[preceding-sibling::*[1] = 'Token']"/>
                </Token>
            </xsl:for-each>
        </playlist>
    </xsl:result-document>
</xsl:template>

Well considering that result-document is new in XSLT 2.0 and that XSLT 1.0 without processor specific extension does not allow creating of multiple result documents at all it is in general not possible to translate stylesheets making use of result-document to XSLT 1.0. 考虑到result-document是XSLT 2.0中的新增功能,并且没有处理器特定扩展的XSLT 1.0根本不允许创建多个结果文档,因此通常无法将使用result-document样式表转换为XSLT 1.0。

However in your snippet the result-document instruction is inside the template matching the single / document node so you could simply remove it and use 但是,在您的代码段中, result-document指令位于与单个/ document节点匹配的模板内,因此您可以简单地将其删除并使用

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.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="/">
        <playlist>
            <xsl:for-each select="//dict[preceding-sibling::*[1]='Tracks']/dict">
                <Track>
                    <Artist>
                        <xsl:value-of select="*[preceding-sibling::*[1][local-name()='key'] = 'Artist']"/>
                    </Artist>
                    <Album>
                        <xsl:value-of select="*[preceding-sibling::*[1][local-name()='key'] = 'Album']"/>
                    </Album>
                    <Songname>
                        <xsl:value-of select="*[preceding-sibling::*[1][local-name()='key'] = 'Name']"/>
                    </Songname>
                    <TrackID>
                        <xsl:value-of select="*[preceding-sibling::*[1][local-name()='key'] = 'Track ID']"/>
                    </TrackID>
                    <TagID>
                        <xsl:value-of select="*[preceding-sibling::*[1][local-name()='key'] = 'Tag ID']"/>
                    </TagID>
                </Track>
            </xsl:for-each>

            <xsl:for-each select="//dict[preceding-sibling::*[1]='Upload Information']">
                <Description>
                    <xsl:value-of select="*[preceding-sibling::*[1][local-name()='key'] = 'Playlist Description']"/>
                </Description>
                <Title>
                    <xsl:value-of select="*[preceding-sibling::*[1][local-name()='key'] = 'Playlist Title']"/>
                </Title>
                <Username>
                    <xsl:value-of select="*[preceding-sibling::*[1][local-name()='key'] = 'Username']"/>
                </Username>
                <Token>
                    <xsl:value-of select="child::*[preceding-sibling::*[1] = 'Token']"/>
                </Token>
            </xsl:for-each>
        </playlist>

</xsl:template>

Of course you would then need to ensure you run your XSLT processor with the right arguments to write its result to output.xml . 当然,您将需要确保使用正确的参数运行XSLT处理器,以将其结果写入output.xml

As for value-of , check whether any of the used expressions selects multiple nodes in your input XML documents. 至于value-of ,请检查是否有任何使用的表达式选择了输入XML文档中的多个节点。 But you only need to check if the stylesheet had once version="2.0" and was run that way with an XSLT 2.0 processor. 但是,您只需要检查样式表是否曾经具有version="2.0"并已通过XSLT 2.0处理器以这种方式运行。 Your posted snippet has version="1.0" , that way even an XSLT 2.0 processor would run it in backwards compatible mode where value-of semantics (output string value of first selected node) is used. 您发布的代码段具有version="1.0" ,这样,即使X​​SLT 2.0处理器也可以在向后兼容模式下运行它,其中value-of语义值(第一个选定节点的输出字符串值)。

So the value-of only need adaption if with version="2.0" they output multiple values and you want to preserve that with version="1.0". 因此,仅当对version =“ 2.0”进行输出并且您想保留对version =“ 1.0”的值时,才需要对value进行调整。 You need to use for-each then eg by replacing 您需要使用for-each然后例如通过替换

                    <xsl:value-of select="child::*[preceding-sibling::*[1] = 'Token']"/>

with

  <xsl:for-each select="child::*[preceding-sibling::*[1] = 'Token']">
    <xsl:if test="position() > 1"><xsl:text> </xsl:text></xsl:if>
    <xsl:value-of select="."/>
  </xsl:for-each>

The only problem with your code with xslt-1.0 would be the <xsl:result-document href="output.xml" method="xml"> . 使用xslt-1.0编写的代码唯一的问题是<xsl:result-document href="output.xml" method="xml"> This is not supported for xslt-1.0 xslt-1.0不支持此功能

One possible solution could be to remove this (xsl:result-document) and redirect the "stdout" output to a file. 一种可能的解决方案是删除此文件(xsl:result-document),然后将“ stdout”输出重定向到文件。 This depends on the tools you are using. 这取决于您使用的工具。

Also some xlst processor support some extensions. 另外,某些xlst处理器支持某些扩展。 For example wiht xsltproc you can replace xsl:result-document with xsl:document. 例如,使用xsltproc可以将xsl:result-document替换为xsl:document。

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

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