【问题标题】:How to remove lines from xml with additional if condition如何使用附加 if 条件从 xml 中删除行
【发布时间】:2021-09-23 11:29:49
【问题描述】:

我有两个 xml 文件。 首先是一个 config.xml 文件:

      <values>
   <s1>yes</s1>
   <s2>no</s2>
   <s3>yes</s3>
   <s4>no</s4>
</values>

第二个是 my_input.xml 文件,我想通过 xslt 对其进行转换:

<?xml version="1.0"?>
<aaa>  
  <bbb>  
      <code>Code</code>
      <name1>1111</name1>
      <name2>2222</name2>
      <documents xsi:nil="true"/>
      <xi:include href="s1.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>
      <xi:include href="s2.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>
      <xi:include href="s3.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>
      <xi:include href="s4.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>
  </bbb>
</aaa>

Output.xml 文件应如下所示:

<?xml version="1.0"?>
<aaa>  
  <bbb>  
      <code>Code</code>
      <name1>1111</name1>
      <name2>2222</name2>
      <documents xsi:nil="true"/>
      <xi:include href="s1.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>
      <xi:include href="s3.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>
  </bbb>
</aaa>

如果 confing.xml 文件中将出现“否”字符串,则应从 my_input.xml 文件中删除该行。

您可以在下面找到我的代码。但我只能删除单独的行,但我不确定如何添加额外的 IF 条件......你有什么想法吗?

<?xml version="1.0"?>
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  version="1.0">

  <xsl:output omit-xml-declaration="no" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>   

  <xsl:template match="*[@href = 's2.xml']" />
  <xsl:template match="*[@href = 's4.xml']" />
</xsl:stylesheet>

兄弟, 溶胶

【问题讨论】:

    标签: xml xslt xslt-1.0 xslt-2.0


    【解决方案1】:

    使用

    <xsl:template match="xi:include[document('config.xml')/values/*[. = 'no' and local-name() = substring-before(current()/@href, '.')]]"/>
    

    【讨论】:

    • 真的很感谢这个提示!但我不确定如何检查它是否有效....由于当我打开 Firefox 时,它会即时转换 my_input.xml 并且我无法看到转换后文件的 xml 源(CTRL+U 返回源xslt 转换前的文件)。我无法使用在线调试器,例如:xsltransform.net,因为在此页面上无法添加第二个 xml 文件 - config.xml(只需要一个 xml 文件)... 换句话说,有吗是否有能力通过 xslt 来检查转换后的 xml 文件的来源
    • @KilsoiSamuel,好吧,如果您问一个关于在 XSLT 中使用两个 XML 输入文档的问题,如果您稍后询问如何测试提议的解决方案,这听起来有点不对劲。您可以从命令行运行的任何 XSLT 处理器、您喜欢的编辑器或 IDE 的任何 XSLT 插件或任何 XML IDE 都允许您这样做,所以选择您的工具,不要指望我们在 StackOverflow 上提供答案。
    • 对于在线工具,只要它们支持 XSLT 2 或更高版本,如果您声明例如&lt;xsl:param name="config-doc"&gt;&lt;values&gt;...&lt;/values&gt;&lt;/xsl:param&gt; 全局内联,然后使用例如&lt;xsl:template match="xi:include[$config-doc/values/*[. = 'no' and local-name() = substring-before(current()/@href, '.')]]"/&gt;,你基本上是在处理两个输入文档,即使没有加载document 函数。因此,通过将第二个文档的数据内联到全局参数或变量中来在线测试该代码也是可能的。
    • 谢谢你,Martin 我在 xsltproc 中检查了你的代码,当然它可以工作。 Xslt 不是我的世界:)。我认为它甚至比 REGEX 更复杂(xpath 变体):)。谢谢你的建议。我们可以关闭一个帖子。
    • @KilsoiSamuel,要“关闭”您的帖子,您将接受解决您问题的答案。
    猜你喜欢
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多