为 XSLT 1.0 中的每个空 XML 标记设置默认值 [英] Set a Default value for each empty XML tags in XSLT 1.0

查看:36
本文介绍了为 XSLT 1.0 中的每个空 XML 标记设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 XSLT 1.0 将默认文本或数字写入空的 XML 标记,然后在 StackOverflow 中搜索时,我碰巧在此 发布

我需要的是例如我有一个像下面这样的标签:

<!--哪个是空的-->

<!--这也是空的-->

我需要的是为我的 XML 中的每个空标签放置一个默认值,例如 0.00</Number> 用于数字标签和 nil<;/Text> 对于字母数字标签,我有一个相当大的 XML,所以有没有办法让它像一个身份模板一样,它总是从我的输入中读取,然后将其转换为在空字符串上插入默认值,或者我只能在每个字段/标签上执行如下代码吗?

<xsl:copy-of select="concat(categoryName,$vOther[not(string(current()/categoryName))])"/>

提前致谢.

解决方案

这种转变:

<xsl:output omit-xml-declaration="yes" indent="yes"/><xsl:strip-space elements="*"/><xsl:template match="@*|node()"><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy></xsl:模板><xsl:template match="Number[not(node())]"><数字>0.00</数字></xsl:模板><xsl:template match="Text[not(node())]"><Text>nill</Text></xsl:模板></xsl:stylesheet>

应用于此 XML 文档时(因为没有提供):

<号码>10</号码><数量/><Text>你好</Text><文字/></t>

产生想要的、正确的结果:

<号码>10</号码><数字>0.00</数字><Text>你好</Text><Text>nill</Text></t>

注意:

为了获得系统的知识来解决这样的基本问题,我(无耻地)推荐观看这个Pluralsight培训课程:

XSLT 2.0 和 1.0 基础

I need to write a default text or number to an empty XML tag using XSLT 1.0, then upon searching here in StackOverflow I happen to look at the solution of Dimitre in this post

What I need is for example I have a tag like below:

<Number></Number> <!--Which is empty--> 

or

<Text></Text> <!--Which is also empty-->

What I need is to put a Default value for each empty tags in my XML like <Number>0.00</Number> for numeric tags and <Text>nil</Text> for Alphanumeric tags, I have quite a big XML so is there any way to make it like an identity template where it will always be read from my input then transform it to Insert the default on empty strings or I can only do the code like below on each field/tags?

<xsl:copy-of select="concat(categoryName,$vOther[not(string(current()/categoryName))])"/>

Thanks in advance.

解决方案

This transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Number[not(node())]">
    <Number>0.00</Number>
  </xsl:template>

  <xsl:template match="Text[not(node())]">
    <Text>nill</Text>
  </xsl:template>
</xsl:stylesheet>

when applied on this XML document (as none was provided):

<t>
  <Number>10</Number>
  <Number/>
  <Text>Hello</Text>
  <Text/>
</t>

produces the wanted, correct result:

<t>
   <Number>10</Number>
   <Number>0.00</Number>
   <Text>Hello</Text>
   <Text>nill</Text>
</t>

Note:

In order to get the systematic knowledge to solve basic problems like this, I (shamelessly) recommend to watch this Pluralsight training course:

XSLT 2.0 and 1.0 Foundations

这篇关于为 XSLT 1.0 中的每个空 XML 标记设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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