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