XSLT:创建副本的其他方法

时间:2017-07-06 06:23:08

标签: xml xslt

我得到以下XML作为输入:

[SKCloudServiceController requestAuthorization:^(SKCloudServiceAuthorizationStatus status) {

        if(status == SKCloudServiceAuthorizationStatusAuthorized) {

            SKCloudServiceController *cloudServiceController = [[SKCloudServiceController alloc] init];

            [cloudServiceController requestCapabilitiesWithCompletionHandler:^(SKCloudServiceCapability capabilities, NSError * _Nullable error) {

                if (capabilities == SKCloudServiceCapabilityMusicCatalogPlayback) {

                    NSLog(@"You can play the song");
                    [[MPMusicPlayerController systemMusicPlayer] setQueueWithStoreIDs:@[@"1123241921"]];
                    [[MPMusicPlayerController systemMusicPlayer] prepareToPlayWithCompletionHandler:^(NSError * _Nullable error) {

                        NSLog(@"Error : %@",error.description);
                    }];];
                }

                else {
                    NSLog(@"You did not have the capability for playing the Apple Music");
                }
            }];
        }

        else {
            NSLog(@"You do not authorize for Apple Music");
        }
    }];

以下用于转换的xsl脚本:

<?xml version="1.0" encoding="ISO-8859-1" ?>

<Telefonliste>
    <Eintrag PNr="p1" >
        <Name>Meier</Name>
        <TelNr Vorwahl="0271">891234</TelNr>
    </Eintrag>
    <Eintrag PNr="p2" >
        <Name>Schmitz</Name>
        <TelNr Vorwahl="0228">870887</TelNr>
    </Eintrag>
</Telefonliste>

如果我使用我的脚本,它会产生以下输出:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform version="1.0" encoding="UTF-8" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" />

<xsl:template match="/ | * | @*">
    <xsl:copy>
        <xsl:apply-templates />
    </xsl:copy>
</xsl:template>

<xsl:template match=" text() | @* ">
    <xsl:value-of select="." />
</xsl:template>

</xsl:transform>

这是我想要的,但缺少属性...为什么缺少属性,如何添加它们?我不想使用身份转换https://en.wikipedia.org/wiki/Identity_transform#Using_XSLT。第二个问题是,如何复制身份转换属性?据我所知,xsl:copy命令不会复制属性节点。

1 个答案:

答案 0 :(得分:0)

  

为什么缺少属性以及如何添加它们?

缺少属性,因为您既不复制它们也不创建新属性。这也应该回答第二部分;其中一个:

<xsl:template match="@*">
    <xsl:copy/>
</xsl:template>

或:

<xsl:template match="@*">
    <xsl:attribute name="{name()}">
        <xsl:value-of select="." />
    </xsl:attribute>
</xsl:template>

将重新创建属性。

但是,为了使其正常工作,您必须先将模板应用于属性。您当前的样式表不会这样做 - 指令:

<xsl:apply-templates />

将模板应用于子节点,而不是属性。

  

据我所知,xsl:copy命令不会复制属性节点...

这是不正确的。如上所述,问题出在其他地方。

  

我不想使用身份转换

我会质疑这种决定的智慧。你的方法是不必要的错综复杂的。另请注意,您的两个模板会发生冲突,因为两者都会匹配一个属性。

相关问题