仅当在 XML 中找到值时才创建标签
您好,仅当在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
但如果你解决了这个问题,你的输出将不会是你想要的。 您需要
更深入地了解 XSLT 的含义。或者,如果您只是想修复它而不获得更深入的理解,请将其重写为
But if you fix that problem, your output won't be what you want. It will be
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
如果您将行
更改为该
行,还应该测试 ram:ID 是否有值
If you change your the line
<xsl:if test="rsm:Waybill/rsm:MessageHeaderDocument/ram:ID">
into
that should also test if there is a value for ram:ID
如果
ram:ID
的字符串值不是零长度字符串,则为true
。It would be
true
if a string-value ofram:ID
isn't a zero-length string.存在许多问题:
您没有创建二维标记——只是一个一维字符串。
如果可能的话,在 XSLT 中最好避免使用条件指令。
解决这两个问题的一种可能的解决方案:
解释:
元素(文字结果元素)实际上是作为元素创建的,而不是作为字符串创建的。
使用单独的
进行模式匹配可以避免在模板正文中使用条件 xslt 指令。There are a number of problems:
You are not creating 2-dimensional markup -- just a 1-dimensional string.
In XSLT conditional instructions are best avoided, if possible.
One possible solution to both problems:
Explanation:
The elements (literal result elements) are created really as elements, not as string.
Using a separate
<xsl:template>
with pattern matching avoids the need of conditional xslt instructions inside the body of a template.我想将其添加为评论,但因为我没有足够的声誉,所以我无法将评论添加到投票的答案中。
请注意,投票的答案有重复的测试条件。
编辑:这在评论和另一个答案中都提到过。
解决办法是,
我已经测试过了。
将检查 both 是否存在
,如下并且该元素具有值。
将仅检查
是否存在另外还有
此检查是否该值为空,因此如果
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.
The solution is ,
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 presentAlso there is
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.