xslt:for-each无效,返回0结果

时间:2015-04-07 14:24:51

标签: xml xslt

我一直在看这个问题太久了,我想我只是缺少一些简单的东西。我的样式表不起作用,它没有返回任何值。我认为它与名称空间有关。如果我删除它,它会按预期工作。

下面是我的XML(缩写)和我的xsl

<?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" href="ImportMapping.xslt" ?>
    <ArrayOfSysDefinitionBase xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <SysDefinitionBase xmlns:d2p1="http://schemas.datacontract.org/2004/07/Scribe.Core.Mapping.AdvSys" i:type="d2p1:AdvSysDefinition">
        <d2p1:BlocksDict xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
          <d3p1:KeyValueOfguidBlocknKIEa0o7>
            <d3p1:Value xmlns:d5p1="http://schemas.datacontract.org/2004/07/Scribe.Core.Mapping.AdvSys.Shapes" i:type="d5p1:UpdateInsertBlock">
              <d5p1:FieldMappings xmlns:d6p1="http://schemas.datacontract.org/2004/07/Scribe.Core.Mapping.Sys">
                <d6p1:MapBinding>
                  <d6p1:TargetField>createdbyname</d6p1:TargetField>
                  <d6p1:TargetFormula>createdbyname</d6p1:TargetFormula>
                  <d6p1:TargetDataType i:nil="true" />
                </d6p1:MapBinding>
              </d5p1:FieldMappings>
            </d3p1:Value>
          </d3p1:KeyValueOfguidBlocknKIEa0o7>
        </d2p1:BlocksDict>
      </SysDefinitionBase>
    </ArrayOfSysDefinitionBase>

样式表:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="ArrayOfSysDefinitionBase/SysDefinitionBase">
    <html>
      <body>
      <table border="1">
          <tr bgcolor="#9acd32">
            <th>Source</th>
            <th>Target</th>
          </tr>
          <xsl:for-each select="BlocksDict/KeyValueOfguidBlocknKIEa0o7/Value/FieldMappings/MapBinding">
            <tr>
              <td>
                <xsl:value-of select="TargetField"/>
              </td>
              <td>
        <xsl:value-of select="TargetFormula"/>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

  

我认为它与名称空间有关。

是的,它有。您的XML文档在各种命名空间中具有许多元素,但您的XPath表达式目标不在命名空间中的元素。您需要将XML中的名称空间声明复制到样式表

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:d2p1="http://schemas.datacontract.org/2004/07/Scribe.Core.Mapping.AdvSys"
                xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays"
                xmlns:d5p1="http://schemas.datacontract.org/2004/07/Scribe.Core.Mapping.AdvSys.Shapes"
                xmlns:d6p1="http://schemas.datacontract.org/2004/07/Scribe.Core.Mapping.Sys">

并在XPath中使用适当的前缀。

<xsl:for-each select="d2p1:BlocksDict/d3p1:KeyValueOfguidBlocknKIEa0o7/d3p1:Value/d5p1:FieldMappings/d6p1:MapBinding">
  <tr>
    <td>
      <xsl:value-of select="d6p1:TargetField"/>
    </td>
    <td>
    <xsl:value-of select="d6p1:TargetFormula"/>
    </td>
  </tr>
</xsl:for-each>