แทนที่แอตทริบิวต์ xml ตามค่าแอตทริบิวต์อื่น

ฉันเพิ่งเริ่มใช้ XSLT ดังนั้นเราจะขอบคุณสำหรับความช่วยเหลือจริงๆ ฉันค้นหา stackoverflow มามาก พยายามใช้หลายวิธี แต่ล้มเหลว

ฉันมี xml เช่นนี้:

    <?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 ทั้งหมดและ:

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

ฉันคิดว่าฉันต้องใช้สิ่งที่ชอบ:

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

ฉันรู้ว่ามันใช้งานไม่ได้ :)


person user2101310    schedule 23.02.2013    source แหล่งที่มา


คำตอบ (2)


วิธีแก้ปัญหาที่สั้นกว่า โดยใช้ AVT (เทมเพลตค่าแอตทริบิวต์):

<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>
person Dimitre Novatchev    schedule 23.02.2013

คุณอยู่ใกล้มาก พารามิเตอร์โกลบอลที่ส่งผ่านไปยังสไตล์ชีตควรได้รับการประกาศที่ระดับภายนอก (ย้ายไปเป็นพี่น้องขององค์ประกอบ output) การจับคู่รูปแบบ HLS อาจทำได้โดยตรงในเทมเพลต [@format='HLS'] นอกจากนี้ คุณยังขาดเครื่องหมายสิ้นสุดองค์ประกอบบน value-of สุดท้ายนี้ คุณไม่สามารถเปลี่ยนแอตทริบิวต์ขององค์ประกอบที่ตรงกันได้โดยตรง ดังนั้นองค์ประกอบ copy ด้านล่าง ปล่อยองค์ประกอบที่ตรงกับแอตทริบิวต์ที่มีอยู่ + รูปแบบที่อัปเดต:

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

หวังว่านี่จะช่วยได้

person Cumbayah    schedule 23.02.2013