【问题标题】:XSLT. If variable is true copy node and update attribute valueXSLT。如果变量为真,则复制节点并更新属性值
【发布时间】:2014-05-13 13:36:10
【问题描述】:

我有类似这样的xml:

<a>
   <b attr1="1" attr2="2" attr3="3"></b>
</a>

假设如果 attr1 存在或不为空,我需要将 attr2 的值更改为 22,并将生成的 xml 存储到变量中。

现在我有这样的东西:

<xsl:variable name="bla">
    <xsl:choose>
        <xsl:when test="b/@attr1 or b/@attr1 != ''">
            true
        </xsl:when>
        <xsl:otherwise>
            false
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>

<xsl:variable name="blabla">
    <xsl:choose>
        <xsl:when test="$bla">
            <xsl:call-template name="ggg">
                <xsl:with-param name="ccc" select="b"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="b"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:when>
    </xsl:choose>
</xsl:variable>

<xsl:template name="ggg">
    <xsl:param name="ccc"/>
    <xsl:copy-of select="@*" />
    <xsl:attribute name="attr2">22</xsl:attribute>
    <xsl:copy-of select="node()" />
</xsl:template>

但它不起作用,我认为我走错了方向。

请帮忙。

---- 更新 ---- 期望的输出:

<a>
    <b attr1="1" attr2="22" attr3="3"></b>
</a>

它应该存储到变量中,因为我需要再次传递它。

【问题讨论】:

  • 你能指定想要的输出是什么吗?将值存储在变量中可能不是您情况的最佳解决方案。
  • 我已经更新了问题。
  • 你以后什么时候使用这个变量,有什么用?例如,您可以通过应用带有参数的模板来传递变量,但您仍然应该更准确地了解用例(存储变量可能只是获得所需内容的一种方式)。
  • 这个 xml 可以是大 xml 的一部分,在某些情况下我需要使用这个进行更改或另一个,取决于变量值。

标签: xml xslt


【解决方案1】:

您可以使用它(XSLT 1.0):

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="b/@attr2">
        <xsl:attribute name="attr2">
            <xsl:choose>
                <xsl:when test="../@attr1">22</xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="."/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:attribute>
    </xsl:template>
</xsl:stylesheet>

或 XSLT 2.0(更简洁):

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="b/@attr2">
        <xsl:attribute name="attr2" 
                      select="if (exists(../@attr1)) then 22 else ."/>
    </xsl:template>
</xsl:stylesheet>

您虽然可以使用全局变量来存储它以供以后使用。在这种情况下,您只需添加以下内容:

<xsl:variable name="blabla" select="/a/b/@attr1"/>

作为xsl:stylesheet 的子节点,因为/a/b/@attr1 节点是唯一的。在您的示例中,问题在于选择 b/@attr1 不会选择任何内容,因为默认上下文是全局变量中的根 (/)。

【讨论】:

  • 正如我所说,我需要将生成的 xml 存储到变量中。
  • 不,它不会工作。我需要存储一个生成的 xml 以供以后使用,而不是属性值。
  • 您可以使用 XSLT 2.0 做到这一点。在上述情况下,select 的结果将是一个属性节点。对于 XSLT 1.0,您必须使用特定的扩展名(例如 exslt)。
  • b/@attr1 没有选择任何内容(我回答的最后一段)。更喜欢使用/a/b/@attr1。请注意,它将选择与 XPath 表达式匹配的所有 @attr1。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-17
  • 1970-01-01
  • 2015-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多