XSLT for-each命名空间

问题描述 投票:0回答:1

我在XSLT模板中使用for-each。

这是我的示例输入XML。

<products>
  <data>
    <label_1>some_label1</label_1>
    <label_2>some_label2</label_2>
    <values>
      <a>a</a>
      <b>b</b>
    </values>
  </data>
  <data>
    <label_1>some_label1</label_1>
    <label_2>some_label2</label_2>
    <values>
      <c>c</c>
      <d>d</d>
    </values>
  </data>
</products>

现在根据我的模板

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="http:/example.com/ns">
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="data">
        <data>
            <xsl:variable name="values" select="values" />
            <xsl:for-each select="$values">
                <xsl:apply-templates select="@*|node()" />
            </xsl:for-each>
        </data>
    </xsl:template>
</xsl:stylesheet>

我只得到 <values></values> 而这对我来说是可以的。

这就是我的输出。

<products>
  <data>
    <a>a</a>
    <b>b</b>
  </data>
  <data>
    <c>c</c>
    <d>d</d>
  </data> 
</products>

我在输出中需要的是这样的命名空间。

<products>
  <data>
    <ns:a>a</ns:a>
    <ns:b>b</ns:b>
  </data>
  <data>
    <ns:c>c</ns:c>
    <ns:d>d</ns:d>
  </data> 
</products>

所以我的理解是 "每个元素的值都是通过模板应用的" 我如何添加命名空间?

xslt namespaces
1个回答
1
投票

您可以通过使用以下方法获得与您所显示的类似的输出(尽管格式良好)。

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="data">
    <xsl:copy>
        <xsl:apply-templates select="values/*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="values/*">
    <xsl:element name="ns:{local-name()}" namespace="http:/example.com/ns">
        <xsl:value-of select="."/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

或者,如果你喜欢。

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="http:/example.com/ns">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/products">
    <products>
        <xsl:for-each select="data">
            <xsl:copy>
                <xsl:for-each select="values/*">
                    <xsl:element name="ns:{local-name()}">
                        <xsl:value-of select="."/>
                    </xsl:element>
                </xsl:for-each>
            </xsl:copy>
        </xsl:for-each>
    </products>
</xsl:template>

</xsl:stylesheet>

替换 http:/example.com/ns 用你自己的命名空间URI。


0
投票

信用

这个答案遵循了在 本回答 类似的问题。

解决办法

将命名空间信息添加到特定元素的所有子代中。通过与这组节点相匹配的模板来增加样式表。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ns="http://my.ns.uri"
>
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="data">
        <data>
            <xsl:variable name="values" select="values" />
            <xsl:for-each select="$values">
                <xsl:apply-templates select="@*|node()" />
            </xsl:for-each>
        </data>
    </xsl:template>

    <!--
        Added template.
    -->
    <xsl:template match="data//*">
        <xsl:element name="ns:{name()}" namespace="http://my.ns.uri">
            <xsl:for-each select=".">
                <xsl:apply-templates select="@*|node()" />
            </xsl:for-each>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>
© www.soinside.com 2019 - 2024. All rights reserved.