是否可以在 xslt 中嵌套动态节点集?

发布于 2024-11-01 04:58:02 字数 2443 浏览 1 评论 0原文

我想知道是否可以在 XSLT 中嵌套动态节点集,如果可以,如何使用 xPath 选择它们。这是一项更大任务的一部分。我只展示我所坚持的部分。

这是我的 XSLT:

<xsl:variable name="Tables">
  <xsl:for-each select="Table">
    <xsl:variable name="TableName" select="Name | @Name"/>
    <xsl:variable name="Columns">
      <xsl:for-each select="Column">
        <xsl:variable name="ColumnName" select="Name | @Name"/>
        <xsl:variable name="Type" select="Type | @Type"/>
        <Column>
          <Name>
            <xsl:value-of select="$ColumnName"/>
          </Name>
          <Type>
            <xsl:value-of select="$Type"/>
          </Type>
        </Column>
      </xsl:for-each>
    </xsl:variable>
    <Table>
      <Name>
        <xsl:value-of select="$TableName"/>
      </Name>
      <Columns>
        <xsl:value-of select="$Columns"/>
      </Columns>
    </Table>
  </xsl:for-each>
</xsl:variable>

<xsl:for-each select="msxsl:node-set($Tables)/Table">
  Table Name: <xsl:value-of select="Name"/>
  <xsl:for-each select="msxsl:node-set(Columns)/Column" xml:space="preserve">
    Column Name: <xsl:value-of select="Name"/>
    Column Type: <xsl:value-of select="Type"/>
  </xsl:for-each>
</xsl:for-each>

这是我的 XML:

  <Table Name="Product">
    <Column Name="ProductID" Type="int"/>
    <Column Name="Name" Type="string"/>
    <Column Name="Cost" Type="decimal"/>
    <Column Name="Area" Type="decimal?"/>
  </Table>
  <Table Name="Market">
    <Column Name="MarketID" Type="int"/>
    <Column Name="Name" Type="string"/>
    <Column Name="MinimumASP" Type="double"/>
    <Column Name="MaximumASP" Type="double"/>
  </Table>

这是我当前得到的输出:

  Table Name: Product
  Table Name: Market

这是我想要得到的:

  Table Name: Product
  Column Name: ProductID
  Column Type: int
  Column Name: Name
  Column Type: string
  Column Name: Cost
  Column Type: decimal
  Column Name: Area
  Column Type: decimal?
  Table Name: Market
  Column Name: MarketID
  Column Type: int
  Column Name: Name
  Column Type: string
  Column Name: MinimumASP
  Column Type: double
  Column Name: MaximumASP
  Column Type: double

I'd like to know if it is possible to nest dynamic node sets in XSLT and if so, how to select them using xPath. This is part of a bigger task. I'm only showing the part that I'm stuck on.

This is my XSLT:

<xsl:variable name="Tables">
  <xsl:for-each select="Table">
    <xsl:variable name="TableName" select="Name | @Name"/>
    <xsl:variable name="Columns">
      <xsl:for-each select="Column">
        <xsl:variable name="ColumnName" select="Name | @Name"/>
        <xsl:variable name="Type" select="Type | @Type"/>
        <Column>
          <Name>
            <xsl:value-of select="$ColumnName"/>
          </Name>
          <Type>
            <xsl:value-of select="$Type"/>
          </Type>
        </Column>
      </xsl:for-each>
    </xsl:variable>
    <Table>
      <Name>
        <xsl:value-of select="$TableName"/>
      </Name>
      <Columns>
        <xsl:value-of select="$Columns"/>
      </Columns>
    </Table>
  </xsl:for-each>
</xsl:variable>

<xsl:for-each select="msxsl:node-set($Tables)/Table">
  Table Name: <xsl:value-of select="Name"/>
  <xsl:for-each select="msxsl:node-set(Columns)/Column" xml:space="preserve">
    Column Name: <xsl:value-of select="Name"/>
    Column Type: <xsl:value-of select="Type"/>
  </xsl:for-each>
</xsl:for-each>

This is my XML:

  <Table Name="Product">
    <Column Name="ProductID" Type="int"/>
    <Column Name="Name" Type="string"/>
    <Column Name="Cost" Type="decimal"/>
    <Column Name="Area" Type="decimal?"/>
  </Table>
  <Table Name="Market">
    <Column Name="MarketID" Type="int"/>
    <Column Name="Name" Type="string"/>
    <Column Name="MinimumASP" Type="double"/>
    <Column Name="MaximumASP" Type="double"/>
  </Table>

This is the output I'm currently getting:

  Table Name: Product
  Table Name: Market

This is what I'd like to get:

  Table Name: Product
  Column Name: ProductID
  Column Type: int
  Column Name: Name
  Column Type: string
  Column Name: Cost
  Column Type: decimal
  Column Name: Area
  Column Type: decimal?
  Table Name: Market
  Column Name: MarketID
  Column Type: int
  Column Name: Name
  Column Type: string
  Column Name: MinimumASP
  Column Type: double
  Column Name: MaximumASP
  Column Type: double

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹊巢 2024-11-08 04:58:03

可以通过更简单的方式生成所需的结果,并且无需创建任何临时树

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match="@*">
  <xsl:value-of select=
   "concat(name(..), ' ', name(), ': ', .)"/>
  <xsl:text>
</xsl:text>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时(包装在单个顶部元素使其格式良好):

<t>
    <Table Name="Product">
        <Column Name="ProductID" Type="int"/>
        <Column Name="Name" Type="string"/>
        <Column Name="Cost" Type="decimal"/>
        <Column Name="Area" Type="decimal?"/>
    </Table>
    <Table Name="Market">
        <Column Name="MarketID" Type="int"/>
        <Column Name="Name" Type="string"/>
        <Column Name="MinimumASP" Type="double"/>
        <Column Name="MaximumASP" Type="double"/>
    </Table>
</t>

产生了想要的正确结果:

Table Name: Product
Column Name: ProductID
Column Type: int
Column Name: Name
Column Type: string
Column Name: Cost
Column Type: decimal
Column Name: Area
Column Type: decimal?
Table Name: Market
Column Name: MarketID
Column Type: int
Column Name: Name
Column Type: string
Column Name: MinimumASP
Column Type: double
Column Name: MaximumASP
Column Type: double

The wanted result can be produced im a much simpler way and there is no need to create any temporary trees:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match="@*">
  <xsl:value-of select=
   "concat(name(..), ' ', name(), ': ', .)"/>
  <xsl:text>
</xsl:text>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document (wrapped in a single top element to make it well-formed):

<t>
    <Table Name="Product">
        <Column Name="ProductID" Type="int"/>
        <Column Name="Name" Type="string"/>
        <Column Name="Cost" Type="decimal"/>
        <Column Name="Area" Type="decimal?"/>
    </Table>
    <Table Name="Market">
        <Column Name="MarketID" Type="int"/>
        <Column Name="Name" Type="string"/>
        <Column Name="MinimumASP" Type="double"/>
        <Column Name="MaximumASP" Type="double"/>
    </Table>
</t>

the wanted, correct result is produced:

Table Name: Product
Column Name: ProductID
Column Type: int
Column Name: Name
Column Type: string
Column Name: Cost
Column Type: decimal
Column Name: Area
Column Type: decimal?
Table Name: Market
Column Name: MarketID
Column Type: int
Column Name: Name
Column Type: string
Column Name: MinimumASP
Column Type: double
Column Name: MaximumASP
Column Type: double
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文