如何使用 msxsl:node-set 获取可在模板参数中使用的节点集?

How do I use the msxsl:node-set to get a node set that I can use in a template parameter?

TL;DR;为什么我不能在 XPATH 中使用元素名称来反对 msxsl:node-set?总是returns什么都没有,就好像node-set是空的,调试的时候发现不是空的。

详细信息:我需要在 XSLT 1.0 文档中使用节点集,因为我的源 XML 缺少一个重要节点。我不想重写整个 XSLT,而是想注入一个节点集,以便我的 XSLT 处理可以继续正常进行。我想在节点集上使用 XPATH,但我无法使用实际的元素名称,而只有 * 有效,但我不确定为什么,也不知道如何访问实际的元素名称XPATH。

这是我的 XML(仅作为示例,此处的 XML 文档最不重要,请参见 XSLT):

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="generic.xslt" ?>
<ParentNode xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" i:noNamespaceSchemaLocation="generic.xsd">
  <SomeChildNode>text</SomeChildNode>
</ParentNode>

这是我的 XSLT:

<?xml version="1.0" encoding="utf-16"?>
<xsl:stylesheet version="1.0" xmlns="http://schemas.datacontract.org/2004/07/MeM.BizEntities.Integration.DataFeedV2" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:a="http://schemas.datacontract.org/2004/07/MeM.BizEntities" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <xsl:output method="xml" indent="yes" encoding="utf-16" omit-xml-declaration="no" />

    <!-- Global Variables, used in multiple places -->
    <xsl:variable name="empty"/>

    <!-- Match Templates -->
    <xsl:template match="ParentNode">
        <ArrayOfSalesOrder>
            <xsl:for-each select="SomeChildNode">
                <xsl:call-template name="SomeChildNodeTemplate">
                    <xsl:with-param name="order" select="."/>
                </xsl:call-template>
            </xsl:for-each>
        </ArrayOfSalesOrder>
    </xsl:template>

    <xsl:template name="SomeChildNodeTemplate">
        <xsl:variable name="someRTF">
            <Items>
                <Item>
                    <Code>code</Code>
                    <Price>75</Price>
                    <Quantity>1</Quantity>
                </Item>
                <Item>
                    <Code>code2</Code>
                    <Price>100</Price>
                    <Quantity>3</Quantity>
                </Item>
            </Items>
        </xsl:variable>
        <xsl:call-template name="ItemsTemplate">
            <xsl:with-param name="items" select="msxsl:node-set($someRTF)"/>
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="ItemsTemplate">
        <xsl:param name="items"/>
        <ItemsTransformed>
            <xsl:for-each select="$items/Item">
                <NewItem>
                    <NewCode>
                        <xsl:value-of select="Code"/>
                    </NewCode>
                </NewItem>
            </xsl:for-each>
        </ItemsTransformed>
        <ItemsTransformedThatWorksButNotHowIWant>
            <xsl:for-each select="$items/*/*">
                <NewItem>
                    <NewCode>
                        <xsl:value-of select="*[1]"/>
                    </NewCode>
                    <NewPrice>
                        <xsl:value-of select="*[2]"/>
                    </NewPrice>
                    <NewQuantity>
                        <xsl:value-of select="*[3]"/>
                    </NewQuantity>
                </NewItem>
            </xsl:for-each>
        </ItemsTransformedThatWorksButNotHowIWant>
    </xsl:template>
</xsl:stylesheet>

我希望能够使用 XPATH 查询节点集,这样我就可以使用它们的正确元素名称。情况似乎并非如此,我正在努力理解原因。我知道可能存在命名空间问题,但尝试 *:Item 等对我不起作用。我可以使用 *[local-name()='Item'] 但这似乎是一个可怕的解决方法,更不用说我将不得不重写任何下游模板,而这正是我试图通过在第一名.

结果:

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfSalesOrder xmlns="http://schemas.datacontract.org/2004/07/MeM.BizEntities.Integration.DataFeedV2" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:a="http://schemas.datacontract.org/2004/07/MeM.BizEntities" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <ItemsTransformed />
  <ItemsTransformedThatWorksButNotHowIWant>
    <NewItem>
      <NewCode>code</NewCode>
      <NewPrice>75</NewPrice>
      <NewQuantity>1</NewQuantity>
    </NewItem>
    <NewItem>
      <NewCode>code2</NewCode>
      <NewPrice>100</NewPrice>
      <NewQuantity>3</NewQuantity>
    </NewItem>
  </ItemsTransformedThatWorksButNotHowIWant>
</ArrayOfSalesOrder>

如您所见,我可以让它与 * 一起工作,但这在更复杂的结构上不是很有用。我究竟做错了什么?这与命名空间有关吗?

我希望在 <ItemsTransformed /> 节点下看到一些东西,但它只是空的,到目前为止,除了 * 之外我无法得到任何东西。

下面的 SO 问题是我正在使用的,我以为我在那里有答案,但我无法让 XPATH 工作。

参考: XSLT 1.0 - Create node set and pass as a parameter

这里的问题是您的样式表有一个 默认 命名空间:

xmlns="http://schemas.datacontract.org/2004/07/MeM.BizEntities.Integration.DataFeedV2"

因此,当您这样做时:

<xsl:variable name="someRTF">
    <Items>
        <Item>
            <Code>code</Code>
            <Price>75</Price>
            <Quantity>1</Quantity>
        </Item>
        <Item>
            <Code>code2</Code>
            <Price>100</Price>
            <Quantity>3</Quantity>
        </Item>
    </Items>
</xsl:variable>

您正在使用默认命名空间中的元素填充您的变量,因此该变量实际包含:

<Items xmlns="http://schemas.datacontract.org/2004/07/MeM.BizEntities.Integration.DataFeedV2">
      <Item>
        <Code>code</Code>
        <Price>75</Price>
        <Quantity>1</Quantity>
      </Item>
      <Item>
        <Code>code2</Code>
        <Price>100</Price>
        <Quantity>3</Quantity>
      </Item>
</Items>

自然地,当您稍后尝试 select 时:

<xsl:for-each select="xyz:node-set($someRTF)/Items/Item">

你 select 什么都没有,因为 ItemsItem 都在默认命名空间中,你没有用它们的完全限定名称来调用它们。

--- 编辑:---

通过确保变量的根元素及其所有后代元素不在命名空间中,可以轻松解决该问题。

这是一个简化的示例(将 运行 与任何输入):

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://schemas.datacontract.org/2004/07/MeM.BizEntities.Integration.DataFeedV2"
xmlns:exsl="http://exslt.org/common"
exclude-result-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:variable name="someRTF">
    <Items xmlns="">
        <Item>
            <Code>code</Code>
            <Price>75</Price>
            <Quantity>1</Quantity>
        </Item>
        <Item>
            <Code>code2</Code>
            <Price>100</Price>
            <Quantity>3</Quantity>
        </Item>
    </Items>
</xsl:variable>

<xsl:template match="/">
    <ArrayOfSalesOrder>
        <ItemsTransformed>
            <xsl:for-each select="exsl:node-set($someRTF)/Items/Item">
                <NewItem>
                    <NewCode>
                        <xsl:value-of select="Code"/>
                    </NewCode>
                </NewItem>
            </xsl:for-each>
        </ItemsTransformed>
    </ArrayOfSalesOrder>
</xsl:template>

</xsl:stylesheet>

结果:

<?xml version="1.0" encoding="UTF-8"?>
<ArrayOfSalesOrder xmlns="http://schemas.datacontract.org/2004/07/MeM.BizEntities.Integration.DataFeedV2">
   <ItemsTransformed>
      <NewItem>
         <NewCode>code</NewCode>
      </NewItem>
      <NewItem>
         <NewCode>code2</NewCode>
      </NewItem>
   </ItemsTransformed>
</ArrayOfSalesOrder>