仅当在 XML 中找到值时才创建标签

发布于 2024-10-20 17:24:56 字数 2126 浏览 1 评论 0原文

您好,仅当在 XML 中找到值时才需要创建标签

我正在使用 XSL 将 XML 转换为 XML 我的问题是仅当在输入 XML 中找到值时才创建标签。 我参考了很多东西并创建了 XSL,但没有工作,你能帮助我吗? 谢谢!

输入 XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href= "D:\Vignesh\Task\RFC\FIS107_24Feb1\MyChanges\XSL SHEET\ThirdIP-HostIP.xsl"?>
<rsm:Waybill     xmlns:ccts="urn:un:unece:uncefact:documentation:standard:CoreComponentsTechnicalSpecificati    on:2" xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:8"
xmlns:rsm="iata:waybill:1" xmlns:ram="iata:datamodel:3">
<rsm:MessageHeaderDocument>
<ram:ID>01463898855</ram:ID> 
<rsm:MessageHeaderDocument>
</rsm:Waybill>

我的 XSL:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"     xmlns:ccts="urn:un:unece:uncefact:documentation:standard:CoreComponentsTechnicalSpecification:2"             xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:8"
         xmlns:rsm="iata:waybill:1"           xmlns:ram="iata:datamodel:3">
<xsl:output method="xml" indent="yes" ></xsl:output>

<xsl:template match="/">

    <xsl:if test="rsm:Waybill/rsm:MessageHeaderDocument/ram:ID"> 
        <xsl:text><![CDATA[<ROUTING>]]></xsl:text>
        <xsl:text><![CDATA[<ORIGIN>]]></xsl:text>
        <xsl:text><![CDATA[ <STATION> ]]></xsl:text>
        <xsl:value-of select="/rsm:Waybill/rsm:MasterConsignment/ram:OriginLocation/ram:ID"/>
        <xsl:text><![CDATA[</STATION> ]]></xsl:text>
        <xsl:text><![CDATA[</ORIGIN>]]></xsl:text>
    </xsl:if>

</xsl:template>
</xsl:stylesheet>

我的输出将类似于 如果值在 ram:ID 中,则:

<ROUTING>
    <ORIGIN>
    <STATION>
        01463898855
    <STATION>
    </ORIGIN>
</ROUTING>

如果 NOT THEN:不应创建任何标签

Hi need to create tags only if values are found in the XML

I am converting XML to XML using XSL
my problem is to create tags only if values are found in the input XML .
I referred many things and created XSL but not working could you please help me .
Thanks!

Input XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href= "D:\Vignesh\Task\RFC\FIS107_24Feb1\MyChanges\XSL SHEET\ThirdIP-HostIP.xsl"?>
<rsm:Waybill     xmlns:ccts="urn:un:unece:uncefact:documentation:standard:CoreComponentsTechnicalSpecificati    on:2" xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:8"
xmlns:rsm="iata:waybill:1" xmlns:ram="iata:datamodel:3">
<rsm:MessageHeaderDocument>
<ram:ID>01463898855</ram:ID> 
<rsm:MessageHeaderDocument>
</rsm:Waybill>

My XSL:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"     xmlns:ccts="urn:un:unece:uncefact:documentation:standard:CoreComponentsTechnicalSpecification:2"             xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:8"
         xmlns:rsm="iata:waybill:1"           xmlns:ram="iata:datamodel:3">
<xsl:output method="xml" indent="yes" ></xsl:output>

<xsl:template match="/">

    <xsl:if test="rsm:Waybill/rsm:MessageHeaderDocument/ram:ID"> 
        <xsl:text><![CDATA[<ROUTING>]]></xsl:text>
        <xsl:text><![CDATA[<ORIGIN>]]></xsl:text>
        <xsl:text><![CDATA[ <STATION> ]]></xsl:text>
        <xsl:value-of select="/rsm:Waybill/rsm:MasterConsignment/ram:OriginLocation/ram:ID"/>
        <xsl:text><![CDATA[</STATION> ]]></xsl:text>
        <xsl:text><![CDATA[</ORIGIN>]]></xsl:text>
    </xsl:if>

</xsl:template>
</xsl:stylesheet>

My output will be like
if value is in ram:ID then:

<ROUTING>
    <ORIGIN>
    <STATION>
        01463898855
    <STATION>
    </ORIGIN>
</ROUTING>

iF NOT THEN : no tags should be created

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

岁月打碎记忆 2024-10-27 17:24:56

但如果你解决了这个问题,你的输出将不会是你想要的。 您需要

<ROUTING>
    <ORIGIN>
    <STATION>
        01463898855
    </STATION>
    </ORIGIN>
</ROUTING>

更深入地了解 XSLT 的含义。或者,如果您只是想修复它而不获得更深入的理解,请将其重写为

<xsl:if test="rsm:Waybill/rsm:MessageHeaderDocument/ram:ID != ''"> 
  <ROUTING>
    <ORIGIN>
      <STATION>
        <xsl:value-of select="/rsm:Waybill/rsm:MasterConsignment/ram:OriginLocation/ram:ID"/>
      </STATION>
    </ORIGIN>
  </ROUTING>
</xsl:if>

But if you fix that problem, your output won't be what you want. It will be

<ROUTING>
    <ORIGIN>
    <STATION>
        01463898855
    </STATION>
    </ORIGIN>
</ROUTING>

You need a deeper understanding of what XSLT is all about. Or if you just want to fix it without gaining a deeper understanding, rewrite it as

<xsl:if test="rsm:Waybill/rsm:MessageHeaderDocument/ram:ID != ''"> 
  <ROUTING>
    <ORIGIN>
      <STATION>
        <xsl:value-of select="/rsm:Waybill/rsm:MasterConsignment/ram:OriginLocation/ram:ID"/>
      </STATION>
    </ORIGIN>
  </ROUTING>
</xsl:if>
冷清清 2024-10-27 17:24:56

如果您将行

更改为该

<xsl:if test="rsm:Waybill/rsm:MessageHeaderDocument/ram:ID and rsm:Waybill/rsm:MessageHeaderDocument/ram:ID != ''">

行,还应该测试 ram:ID 是否有值

If you change your the line

<xsl:if test="rsm:Waybill/rsm:MessageHeaderDocument/ram:ID">

into

<xsl:if test="rsm:Waybill/rsm:MessageHeaderDocument/ram:ID and rsm:Waybill/rsm:MessageHeaderDocument/ram:ID != ''">

that should also test if there is a value for ram:ID

夕色琉璃 2024-10-27 17:24:56
<xsl:if test="string(rsm:Waybill/rsm:MessageHeaderDocument/ram:ID)">
     do your strange stuff..
</xsl:if>

如果 ram:ID 的字符串值不是零长度字符串,则为 true

<xsl:if test="string(rsm:Waybill/rsm:MessageHeaderDocument/ram:ID)">
     do your strange stuff..
</xsl:if>

It would be true if a string-value of ram:ID isn't a zero-length string.

メ斷腸人バ 2024-10-27 17:24:56

存在许多问题

  1. 您没有创建二维标记——只是一个一维字符串。

  2. 如果可能的话,在 XSLT 中最好避免使用条件指令。

解决这两个问题的一种可能的解决方案:

<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="rsm:MessageHeaderDocument[ram:ID]">
    <ROUTING>
        <ORIGIN>
            <STATION>
                <xsl:value-of select=
                "/*/rsm:MasterConsignment/ram:OriginLocation/ram:ID"/>
            </STATION>
        </ORIGIN>
    </ROUTING>
 </xsl:template>
</xsl:stylesheet>

解释

  1. 元素(文字结果元素)实际上是作为元素创建的,而不是作为字符串创建的。

  2. 使用单独的 进行模式匹配可以避免在模板正文中使用条件 xslt 指令。

There are a number of problems:

  1. You are not creating 2-dimensional markup -- just a 1-dimensional string.

  2. In XSLT conditional instructions are best avoided, if possible.

One possible solution to both problems:

<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="rsm:MessageHeaderDocument[ram:ID]">
    <ROUTING>
        <ORIGIN>
            <STATION>
                <xsl:value-of select=
                "/*/rsm:MasterConsignment/ram:OriginLocation/ram:ID"/>
            </STATION>
        </ORIGIN>
    </ROUTING>
 </xsl:template>
</xsl:stylesheet>

Explanation:

  1. The elements (literal result elements) are created really as elements, not as string.

  2. Using a separate <xsl:template> with pattern matching avoids the need of conditional xslt instructions inside the body of a template.

梦归所梦 2024-10-27 17:24:56

我想将其添加为评论,但因为我没有足够的声誉,所以我无法将评论添加到投票的答案中。

请注意,投票的答案有重复的测试条件。
编辑:这在评论和另一个答案中都提到过。

<xsl:if test="rsm:Waybill/rsm:MessageHeaderDocument/ram:ID and rsm:Waybill/rsm:MessageHeaderDocument/ram:ID != ''">

解决办法是,

<xsl:if test="rsm:Waybill/rsm:MessageHeaderDocument/ram:ID != ''">

我已经测试过了。

将检查 both 是否存在 ,如下并且该元素具有值。

检查 是否存在

另外还有

<xsl:if test="string-length(someelement)=0">

此检查是否该值为空,因此如果 somelement 不存在,以及它存在但具有空值,则返回 true。

I wanted to add this as a comment, but because i do not have enough reputation, i cannot add comment to the voted answer.

Please note that the voted answer has a duplicate test condition.
Edit: this was mentioned as a comment and also in another answer.

<xsl:if test="rsm:Waybill/rsm:MessageHeaderDocument/ram:ID and rsm:Waybill/rsm:MessageHeaderDocument/ram:ID != ''">

The solution is ,

<xsl:if test="rsm:Waybill/rsm:MessageHeaderDocument/ram:ID != ''">

I have tested this.

<xsl:if test="someelement!=''"> will check both if the <someelement> is present , as well as the element has a value.

<xsl:if test="someelement"> will check only if the <someelement> is present

Also there is

<xsl:if test="string-length(someelement)=0">

This checks if the value is empty, hence it returns true if somelement is not present, as well as when it is present, but have an empty value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文