【问题标题】:Empty tag xml - add value from another tag with xslt空标签 xml - 使用 xslt 从另一个标签添加值
【发布时间】:2020-07-31 23:39:12
【问题描述】:

我在How to Insert an XML tag after a particular tag using XSLT? 看到了一个例子,但这不完全是我的问题。

我有一个 xml,它的一些标签缺少值,例如:

<row>
    <title>The Outsider</title>
    <year>2018</year>
    <actors></actors> <!-- empty tag: add value "unknown" -->
    <actors>Tadanobu Asano</actors>
    <actors>Kippei Shîna</actors>
    <actors>Shioli Kutsuna</actors>
    <actors>Emile Hirsch</actors>
    <actors>Ray Nicholson</actors>
    <actors>Rory Cochrane</actors>
    <actors>Nao Ohmori</actors>
    <actors>Min Tanaka</actors>
    <actors>Masaki Miura</actors>
    <actors>Shun Sugata</actors>
    <actors>Hiroya Shimizu</actors>
    <actors>Hiro</actors>
    <actors>Young Dais</actors>
    <actors>Gô Jibiki</actors>
    <genre>Action</genre>
    <genre>Crime</genre>
    <genre>Drama</genre>
    <genre>Thriller</genre>
    <description/>      <!-- here is missing <description> -->
</row>

我想要的是将值添加到标签中。

理想的输出:

 <row>
        <title>The Outsider</title>
        <year>2018</year>
        <actors>Unknown</actors> <!-- add word "unknown" if tag is empty -->
        <actors>Tadanobu Asano</actors>
        <actors>Kippei Shîna</actors>
        <actors>Shioli Kutsuna</actors>
        <actors>Emile Hirsch</actors>
        <actors>Ray Nicholson</actors>
        <actors>Rory Cochrane</actors>
        <actors>Nao Ohmori</actors>
        <actors>Min Tanaka</actors>
        <actors>Masaki Miura</actors>
        <actors>Shun Sugata</actors>
        <actors>Hiroya Shimizu</actors>
        <actors>Hiro</actors>
        <actors>Young Dais</actors>
        <actors>Gô Jibiki</actors>
        <genre>Action</genre>
        <genre>Crime</genre>
        <genre>Drama</genre>
        <genre>Thriller</genre>
        <description>The Outsider<description/>  <!-- complete <description> with <title> value -->
    </row>

到目前为止,我尝试过这个:

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

   <xsl:template match="node()|@*" name="ident">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*" />
        </xsl:copy>
    </xsl:template>
 
      <xsl:template match="row[not(description)]">
        <xsl:copy>
                <xsl:apply-templates select="genre"/>
                    <description>
                      <xsl:value-of select="./title" />
                    </description>
                <xsl:apply-templates select="*[not(self::genre)]"/>
          </xsl:copy>
        </xsl:template>


</xsl:transform>

但我在输出 xml 中没有得到我想要的。你能帮助我吗?谢谢。

【问题讨论】:

    标签: xml xslt tags


    【解决方案1】:

    您的标题显示“缺少标签” - 但您的示例显示 empty actordescription 元素,而不是 missing 元素。如果这是您需要处理的唯一情况,您可以这样做:

    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"/>
    
    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="actors[not(text())]">
        <xsl:copy>Unknown</xsl:copy>
    </xsl:template>
    
    <xsl:template match="description[not(text())]">
        <xsl:copy>
            <xsl:value-of select="../title" />
        </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

    • 非常感谢!!!!您的回答确实帮助我解决了问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多