根据其他属性值替换 xml 属性 [英] Replace xml attribute based on the other attribute value

查看:26
本文介绍了根据其他属性值替换 xml 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 XSLT 的新手,所以我非常感谢您的帮助.我在 stackoverflow 上搜索了很多,尝试应用几种方法,但都失败了.

I'm new to XSLT, so I'll really appreciate any help. I've searched a lot on stackoverflow, tried to apply several approaches, but failed.

我有一个这样的 xml:

I have an xml like this:

    <?xml version="1.0" encoding="utf-8"?>
       <asset_detail>
          <asset id="1" show_type="Movies">
             <title><![CDATA[Movie]]></title>
             <media_list>
               <media id="11" title="" type="trailer">
                 <version format="m3u8" rel_path="/Content/movie.m3u8"/>
                 <version format="HLS" rel_path="/movies/movie.m3u8"/>
               </media>
               </media_list>
           </asset>
       </asset_detail>

我应该复制整个 xml 并且:

I should copy the whole xml and:

if media_list/media/version/@format = '**HLS**' I need to replace **@rel_path** with
value: **concat**(existing value of **@rel_path****,** **someVariable**
(I'm passing to xsl as a <xsl:param>)

我想我必须使用类似的东西:

I think I have to use something like:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="@*|node()">
        <xsl:param name="someVariable"/>
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="media/version">
        <xsl:attribute name="rel_path">
            <xsl:choose>
                <xsl:when test="./@format = HLS">
                    <xsl:value-of select="concat(rel_path,$someVariable)">
                </xsl:when>
            </xsl:choose>
        </xsl:attribute>
    </xsl:template>
</xsl:stylesheet>

我知道这行不通:)

推荐答案

更短的解决方案,使用 AVT(属性值模板):

A shorter solution, using AVT (Attribute Value Templates):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>
 <xsl:param name="psomeParam" select="'/xxx'"/>

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

 <xsl:template match="version[@format='HLS']">
  <version rel_path="{@rel_path}{$psomeParam}">
   <xsl:apply-templates select="@*[not(name()='rel_path')] | node()"/>
  </version>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时:

<asset_detail>
    <asset id="1" show_type="Movies">
        <title><![CDATA[Movie]]></title>
        <media_list>
            <media id="11" title="" type="trailer">
                <version format="m3u8" rel_path="/Content/movie.m3u8"/>
                <version format="HLS" rel_path="/movies/movie.m3u8"/>
            </media>
        </media_list>
    </asset>
</asset_detail>

产生了想要的、正确的结果:

<asset_detail>
   <asset id="1" show_type="Movies">
      <title>Movie</title>
      <media_list>
         <media id="11" title="" type="trailer">
            <version format="m3u8" rel_path="/Content/movie.m3u8"/>
            <version rel_path="/movies/movie.m3u8/xxx" format="HLS"/>
         </media>
      </media_list>
   </asset>
</asset_detail>

这篇关于根据其他属性值替换 xml 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆