从另一个 xml 为空节点设置默认值 [英] Set default values from another xml for empty nodes

查看:35
本文介绍了从另一个 xml 为空节点设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于:

带数据的文件:

<doc>
     <a>some text(<p>text here</p>) or blank</a>
     <b>same</b>
</doc>

具有默认值的文件:

<doc>
     <a><p>Dummy text</p></a>
     <b><p>Dummy text</p></b>
</doc>

我得到了这个 xslt 来从第二个文件中获取默认值:

I got this xslt to get the defaults from the second file:

$file = 带有默认值的文件路径

$file = file path with default values

<a>
    <xsl:choose>
        <xsl:when test="//a!= ''">
            <xsl:copy-of select="//a/p" />
        </xsl:when>
        <xsl:otherwise>
              <xsl:copy-of select="document($file)//a/p" />
        </xsl:otherwise>
    </xsl:choose>
</a>

有了它,它运行良好,唯一的问题是我想通过 xml 节点进行迭代,并制作一个自动化过程来提高可扩展性和节省每次从每个节点输入相同条件的时间.但是我找不到将 document() 函数xpath 的变量一起使用的方法,document($file)$nodexpath 没有不行.

With this it works well and all, the only problem is that I would like to make an iteration through the xml nodes and make an automated process to improve scalability and save time entering every time the same condition fro each node. But I can't find a way to use the document() function with variables for the xpath, document($file)$nodexpath doesn't work.

我可能遗漏了一些东西,最近刚开始使用 xslt,如果有人能给我一些建议,我将不胜感激.

I might be missing something, just started recently with xslt, if any of you could give me some advice I would appreciate It greatly.

提前致谢.

实际数据更像这样的xml,可以有更深的层次.

The actual data resembles more an xml like this, it can have more deepth levels.

   <doc>
       <object>
          <group1> 
              <a>(<p>text here</p> or blank)</a>
              <b>(<p>text here</p> or blank)</b>
              <c>
                 <c1>(<p>text here</p> or blank)</c1>
                 <c2>(<p>text here</p> or blank)</c2>
              </c>
          </group1>
          <group2>
              <d>(<p>text here</p> or blank)</d>
          </group2>
       </object>
    </doc>

推荐答案

我终于解决了,我在这里发布我的解决方案,感谢 Daniel 的回答,对我帮助很大:

I've finally resolved it, I'm posting my solution here, thanks Daniel for your answer, it helped me a lot:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/*">
        <doc xml:lang="es">
            <object>
                <xsl:apply-templates select="*"/>
            </object>
        </doc>
    </xsl:template> 

    <xsl:template match="*">
        <xsl:copy>
            <xsl:choose>
              <xsl:when test="current() != ''">
                <xsl:choose>
                    <xsl:when test="self::p">
                        <xsl:copy-of select="node()"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:apply-templates select="*"/>
                    </xsl:otherwise>
                </xsl:choose>
              </xsl:when>
              <xsl:otherwise>
                <xsl:variable name="file" select="document('template.xml')"/>
                <xsl:copy-of select ="$file//*[name()=name(current())]/p"/>
              </xsl:otherwise>
            </xsl:choose>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

这篇关于从另一个 xml 为空节点设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆