将XML节点与XSL结合使用

时间:2015-06-29 23:02:30

标签: xml xslt

我有一个API,我正在使用SOAP调用,它正在输出一个XML文件。我遇到麻烦的地方是将文件放入表格以便于阅读。最大的问题是asset.device和相应的asset.os没有包装。

输出结果如下:

<?xml version="1.0" encoding="UTF-8"?>
<DeviceAssetInfoExport>
  <asset.device>
    <asset.device.id>123</asset.device.id>
    ...
  </asset.device>
  <asset.os>
    <asset.os.reportedos>abc</asset.os.reportedos>
    ...
  <asset.os>
  <asset.device>
    <asset.device.id>321</asset.device.id>
    ...
  </asset.device>
  <asset.os>
    <asset.os.reportedos>cba</asset.os.reportedos>
    ...
  <asset.os>
</DeviceAssetInfoExport>

所需的输出是一个html表,如:

<html>
<body>
  <h2>Servers</h2>
    <table>
      <tr>
        <th>Name</th>
        <th>Address</th>
        <th>OS</th>
      </tr>
      <tr>
        <td>123</td>
        <td>home.co</td>
        <td>abc</td>
      </tr>
  </table>
</body>
</html>

我目前的尝试如下:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" >
<xsl:template match="DeviceAssetInfoExport">
    <h2>Servers</h2>
        <table border="1">
            <tr bgcolor="gray">
                <th>Name</th>
                <th>Address</th>
                <th>OS</th>
            </tr>
            <xsl:apply-templates select="asset.device | asset.os"/>
        </table>
</xsl:template>

<xsl:template match="asset.device | asset.os">
    <tr>
        <td><xsl:value-of select="asset.device.longname"/></td>
        <td><xsl:value-of select="asset.device.uri"/></td>
        <td><xsl:value-of select="asset.os.reportedos"/> - <xsl:value-of select="asset.os.osarchitecture"/></td>
    </tr>
</xsl:template>


</xsl:stylesheet>

不幸的是,我从中收到的输出将asset.os.reportedos信息放在表中自己的行上。它处于正确的第三个位置,它只是没有设备信息的正确行。

如果我能做些什么来使我想要的结果更清楚,请告诉我。

谢谢!

1 个答案:

答案 0 :(得分:2)

看看这是否可以让你入门:

输入XML

- (IBAction)importButtonPressed:(UIButton *)sender {
[MBProgressHUD showHUDAddedTo:self.view animated:YES];

[[PHImageManager defaultManager]requestAVAssetForVideo:self.selectedAsset options:nil resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {

    NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:asset];

    if ([compatiblePresets containsObject:AVAssetExportPreset960x540]) {

        exportSession = [[AVAssetExportSession alloc]
                                               initWithAsset:asset presetName:AVAssetExportPreset960x540];

        NSString *videoPath;

        //Totally valid path to drop the video in
        videoPath = [[FileManager sharedFileManager] pathToSaveNewVideo];

        NSURL *outputhURL = [NSURL fileURLWithPath:videoPath];
        exportSession.outputURL = outputhURL;
        exportSession.outputFileType = AVFileTypeQuickTimeMovie;

        CMTime adur = self.mPlayerItem.duration;
        CMTime start = CMTimeMake(adur.value * self.lineCutter.startAt, adur.timescale);
        CMTime duration = CMTimeMake(adur.value * (self.lineCutter.endsAt - self.lineCutter.startAt), adur.timescale);
        CMTimeRange range = CMTimeRangeMake(start, duration);
        exportSession.timeRange = range;


        outputhURLBuff = outputhURL;
        [self performSelectorOnMainThread:@selector(exportWithSession:) withObject:exportSession waitUntilDone:NO];
    }
}];

}



-(void)exportWithSession:(AVAssetExportSession*)aexport{
[aexport exportAsynchronouslyWithCompletionHandler:^{

    if (exportSession.status == AVAssetExportSessionStatusCompleted){
        [self performSelectorOnMainThread:@selector(didExportedToURL:) withObject:outputhURLBuff waitUntilDone:NO];
    }else{
        [self performSelectorOnMainThread:@selector(didFailedToExportToURL:) withObject:outputhURLBuff waitUntilDone:NO];
    }

}];

}

<强> XSLT

<DeviceAssetInfoExport>
  <asset.device>
    <asset.device.id>123</asset.device.id>
  </asset.device>
  <asset.os>
    <asset.os.reportedos>abc</asset.os.reportedos>
  </asset.os>
  <asset.device>
    <asset.device.id>456</asset.device.id>
  </asset.device>
  <asset.os>
    <asset.os.reportedos>def</asset.os.reportedos>
  </asset.os>
</DeviceAssetInfoExport>

<强>结果

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/DeviceAssetInfoExport">
    <html>
        <body>
            <h2>Servers</h2>
            <table border="1">
                <tr>
                    <th>ID</th>
                    <th>OS</th>
                </tr>
                <xsl:apply-templates select="asset.device"/>
            </table>
        </body>
    </html>     
</xsl:template>

<xsl:template match="asset.device">
    <tr>
        <td><xsl:value-of select="asset.device.id"/></td>
        <td><xsl:value-of select="following-sibling::asset.os[1]/asset.os.reportedos"/></td>
    </tr>
</xsl:template>

</xsl:stylesheet>