【问题标题】:XML -> XSLT -> Word for-each not loopingXML -> XSLT -> Word for-each 不循环
【发布时间】:2012-11-30 20:47:44
【问题描述】:

我已将我的 SQL 客户端从 MySQL Query Browser 更新到 MySQL Workbench。 随着那次升级,我的 XML 格式发生了变化,我的 XSLT 不再工作了。它只会显示第一项。

这里是 XML 和 XSLT 的简化版本,它的 Nessus 输出发送到 MySQL 数据库。

XML

<?xml version="1.0" encoding="UTF-8"?>
<DATA>

    <ROW>
        <Niveau>Haute</Niveau>
        <Nom>Some vulnerability</Nom>
        <Solution>Some Solution</Solution>
        <cve>CVE-2006-1234, CVE-2006-5615</cve>
        <bid>11263, 11291</bid>
        <Number>5</Number>
    </ROW>

    <ROW>
        <Niveau>Haute</Niveau>
        <Nom>Some Other Vulnerability</Nom>
        <Solution>Apply the security patch.</Solution>
        <cve>CVE-2006-1742, CVE-2006-1743</cve>
        <bid>20584, 20585</bid>
        <Number>23</Number>
    </ROW>

    <ROW>
        <Niveau>Moyenne</Niveau>
        <Nom>Yet another vulnerability</Nom>
        <Solution>Do this and that</Solution>
        <cve></cve>
        <bid></bid>                
        <Number>2</Number>
    </ROW>


</DATA>

XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/">

    <xsl:processing-instruction name="mso-application">
      <xsl:text>progid="Word.Document"</xsl:text>
    </xsl:processing-instruction>
    <w:wordDocument
      xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">

      <w:body>
        <xsl:for-each select="DATA/ROW">
          <w:p>
            <w:r>
              <w:t><xsl:value-of select="Nom"/></w:t>
            </w:r>
          </w:p>
        </xsl:for-each>
      </w:body>
    </w:wordDocument>

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

然后我在将两者合并到 Word 文档中的脚本中对其进行解析。它用于创建与我的条目一样多的表。但现在我只得到第一行。 我很确定它与&lt;xsl:template match="/"&gt;&lt;xsl:for-each select="DATA/ROW"&gt; 有关,但我似乎找不到正确的组合。

如果有人也可以告诉我如何在Nom 之前添加一个序列号,就像1- Nom 这样,我会是一个非常高兴的露营者。

电流输出

<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
  <w:body>
    <w:p>
      <w:r>
        <w:t>Some vulnerability</w:t>
      </w:r>
    </w:p>
  </w:body>
</w:wordDocument>

期望的输出

<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
  <w:body>
    <w:p>
      <w:r>
        <w:t>1- Some vulnerability</w:t>
      </w:r>
    </w:p>
    <w:p>
      <w:r>
        <w:t>2- Some Other Vulnerability</w:t>
      </w:r>
    </w:p>
    <w:p>
      <w:r>
        <w:t>3- Yet another vulnerability</w:t>
      </w:r>
    </w:p>
  </w:body>
</w:wordDocument>

谢谢。

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    这应该做你想做的:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" indent="yes"/>
      <xsl:template match="/DATA">
    
        <xsl:processing-instruction name="mso-application">
          <xsl:text>progid="Word.Document"</xsl:text>
        </xsl:processing-instruction>
        <w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
          <w:body>
            <xsl:for-each select="ROW">
              <w:p>
                <w:r>
                  <w:t>
                    <xsl:value-of select="concat(count(preceding-sibling::ROW)+1,'- ',Nom)"/>
                  </w:t>
                </w:r>
              </w:p>
            </xsl:for-each>
          </w:body>
        </w:wordDocument>
      </xsl:template>
    </xsl:stylesheet>
    

    “更多 XSLT”的解决方案是:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
        xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
      <xsl:output method="xml" indent="yes"/>
      <xsl:template match="/DATA">
    
        <xsl:processing-instruction name="mso-application">
          <xsl:text>progid="Word.Document"</xsl:text>
        </xsl:processing-instruction>
        <w:wordDocument>
          <w:body>
            <xsl:apply-templates select="ROW"/>
          </w:body>
        </w:wordDocument>
      </xsl:template>
    
      <xsl:template match="ROW">
        <w:p>
          <w:r>
            <w:t>
              <xsl:value-of select="concat(count(preceding-sibling::ROW)+1,'- ',Nom)"/>
            </w:t>
          </w:r>
        </w:p>
      </xsl:template>
    </xsl:stylesheet>
    

    两者的输出是:

    <?xml version="1.0" encoding="utf-8"?><?mso-application progid="Word.Document"?>
    <w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
      <w:body>
        <w:p>
          <w:r>
            <w:t>1- Some vulnerability</w:t>
          </w:r>
        </w:p>
        <w:p>
          <w:r>
            <w:t>2- Some Other Vulnerability</w:t>
          </w:r>
        </w:p>
        <w:p>
          <w:r>
            <w:t>3- Yet another vulnerability</w:t>
          </w:r>
        </w:p>
      </w:body>
    </w:wordDocument>
    

    【讨论】:

    • 感谢您的快速回复。使用这 2 个 XSLT 给我的结果与我的完全相同,并且在 Nom 前面加上 1- 。似乎“循环”由于某种原因仍然无法正常工作..
    • 这很奇怪。对于我来说,使用 Saxon、Xalan、xsltproc 或 Firefox 的内置 XSLT 处理器时,它们都会产生基本相同的输出。你在用什么?
    • 好的,有人更正了我的输入 XML。似乎&lt;Number of Affected&gt; 中的空间造成了问题。我更改了&lt;NoAffected&gt; 的名称,现在它正在处理。我正在使用以下使用 irb 和 nokogiri 的 linux 脚本。 irb -rrubygems
    • 看起来真的很复杂。 Nokogiri在后台使用libxslt,你可以直接使用,比如xsltproc NessusTable.xslt MyXML.xml。它也适用于 Windows:zlatkovic.com/libxml.en.html。还有Saxon和Xalan,都是跨平台的,还有微软自己的XSLT处理器:microsoft.com/en-us/download/details.aspx?id=21714(显然不是跨平台的)。
    【解决方案2】:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
         <xsl:output method="xml" indent="yes"/>
      <xsl:template match="/DATA">
    
        <xsl:processing-instruction name="mso-application">
          <xsl:text>progid="Word.Document"</xsl:text>
        </xsl:processing-instruction>
        <w:wordDocument
          xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
    
          <w:body>
            <xsl:for-each select="//ROW">
              <xsl:variable name="pos" select="position()"/>
              <w:p>
                <w:r>
                  <w:t>
                  <xsl:value-of select="concat($pos,'-')"/>
                  <xsl:value-of select="Nom"/>
                  </w:t>
                </w:r>
              </w:p>
            </xsl:for-each>
          </w:body>
        </w:wordDocument>
    
      </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多