簡體   English   中英

如何復制XSLT中的節點值

[英]How to copy node value in XSLT

這是我的 xml 文件,我需要在其中檢查 SOME_SW_INSTALLED 的值,如果它是 IS_TRUE,那么 SOME_HW_INSTALLED 的值應該是 IS_TRUE(反之亦然)。 我是新來的,不了解 xslt。

<MEDIA_FAMILY>
      <SOME_ID ReadOnly="true">1</SOME_ID>
      <SOME_NAME ReadOnly="true">somecode</SOME_NAME>
      <SOME_DESCRIPTION ReadOnly="true">something</SOME_DESCRIPTION>
      <SOME_SW_INSTALLED ReadOnly="true">IS_TRUE</SOME_SW_INSTALLED>
      <SOME_HW_INSTALLED ReadOnly="false">IS_FALSE</SOME_HW_INSTALLED>
</MEDIA_FAMILY>

這是我的 xslt 但有人可以建議我更簡單的方法

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space elements="*"/>

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

<xsl:template match="/MEDIA_FAMILY/SOME_HW_INSTALLED">
   <xsl:choose>
       <xsl:when test= "../SOME_SW_INSTALLED = 'IS_TRUE'">
            <xsl:copy>
            <xsl:apply-templates select="@*"/>
           <xsl:text>IS_TRUE</xsl:text>
        </xsl:copy>
          
       </xsl:when>
     <xsl:otherwise>
            <xsl:copy>
            <xsl:apply-templates select="@*"/>
           <xsl:text>IS_FALSE</xsl:text>
        </xsl:copy>
     </xsl:otherwise>
   </xsl:choose>
</xsl:template>
</xsl:transform>

很難從一個例子中預見所有可能的情況及其預期結果。 這對你有用嗎?

XSLT 1.0

<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:strip-space elements="*"/>

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

<xsl:template match="SOME_HW_INSTALLED">
     <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:value-of select="../SOME_SW_INSTALLED"/>
    </xsl:copy>
 </xsl:template>

</xsl:stylesheet>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM