繁体   English   中英

XSLT 2.0至1.0

[英]XSLT 2.0 to 1.0

这里有很多将XSLT 1.0转换为2.0的帮助,但是我需要另辟!径!

好的,我在XSLT 2.0中有一个XSL文件:我需要将此文件转换为XSLT 1.0。 现在,我知道转换正挂在“结果文档”上,但是我不确定如何解决此问题。 而且,我不确定我的其他信息是否会转换为1.0:我的value of select =语句是否仍然有效? 救命! (谢谢)!

<?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>

考虑到result-document是XSLT 2.0中的新增功能,并且没有处理器特定扩展的XSLT 1.0根本不允许创建多个结果文档,因此通常无法将使用result-document样式表转换为XSLT 1.0。

但是,在您的代码段中, 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>

当然,您将需要确保使用正确的参数运行XSLT处理器,以将其结果写入output.xml

至于value-of ,请检查是否有任何使用的表达式选择了输入XML文档中的多个节点。 但是,您只需要检查样式表是否曾经具有version="2.0"并已通过XSLT 2.0处理器以这种方式运行。 您发布的代码段具有version="1.0" ,这样,即使X​​SLT 2.0处理器也可以在向后兼容模式下运行它,其中value-of语义值(第一个选定节点的输出字符串值)。

因此,仅当对version =“ 2.0”进行输出并且您想保留对version =“ 1.0”的值时,才需要对value进行调整。 您需要使用for-each然后例如通过替换

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

  <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>

使用xslt-1.0编写的代码唯一的问题是<xsl:result-document href="output.xml" method="xml"> xslt-1.0不支持此功能

一种可能的解决方案是删除此文件(xsl:result-document),然后将“ stdout”输出重定向到文件。 这取决于您使用的工具。

另外,某些xlst处理器支持某些扩展。 例如,使用xsltproc可以将xsl:result-document替换为xsl:document。

暂无
暂无

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

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