【问题标题】:XSLT child elementsXSLT 子元素
【发布时间】:2017-04-25 11:59:00
【问题描述】:

我有这样的 xml 文档:

<OrdersSchedulePDFView>
   <OrdersSchedulePDFViewRow>      
      <Items>
         <ItemDesc>Content1</ItemDesc>
         <ItemDesc>Content2</ItemDesc>
      </Items>
      <Locations>
         <LocName>Content3</LocName>
         <LocName>Content4</LocName>
      </Locations>
   </OrdersSchedulePDFViewRow>
</OrdersSchedulePDFView>

请给我一个示例 xsl 文件,我可以在其中通过模板获取 ItemDesc 和 LocName 元素。提前致谢 这是我的 xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fo">   
<xsl:template match="OrdersSchedulePDFView">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">        
        <xsl:for-each select="OrdersSchedulePDFViewRow">       
        <fo:page-sequence master-reference="all">           
            <fo:flow flow-name="xsl-region-body">                               
                <xsl:template match="Locations">
                    <xsl:copy>
                        <xsl:apply-templates select="LocName"/>
                    </xsl:copy>
                </xsl:template>             
                <xsl:template match="Items">                                                        
                    <xsl:copy>
                        <xsl:apply-templates select="ItemDesc"/>
                    </xsl:copy>
                </xsl:template>         
            </fo:flow>
        </fo:page-sequence>
        </xsl:for-each>
    </fo:root>
</xsl:template>
</xsl:stylesheet>

【问题讨论】:

  • 你到底想做什么?你可以试试&lt;xsl:template match="LocName"&gt;(如果我们可能看不到的元素上没有命名空间),但是模板应该做什么?
  • 我需要打印 ItemDesc 和 LocName 元素。但我无法通过 获得它们
  • 为什么不发布您的尝试以便我们修复它,而不必从头开始为您编写代码。另外,显示您期望得到的准确输出。
  • 然后最小化 - 请参阅:minimal reproducible example
  • @ВячеславКошман 你的 XSLT 不能工作,因为模板不能嵌套。我不能告诉你如何解决它,因为你还没有发布预期的输出。

标签: apache xslt apache-fop


【解决方案1】:

虽然你的答案相当模糊,但如果我没记错的话你想要输出:

<output>
    <ItemDesc>Content1</ItemDesc>
    <ItemDesc>Content2</ItemDesc>
    <LocName>Content3</LocName>
    <LocName>Content4</LocName>
</output>

首先想到的方法是使用递归模板:

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

<xsl:template match="OrdersSchedulePDFView">
    <output>
        <xsl:apply-templates/>
    </output>
</xsl:template>

<xsl:template match="ItemDesc">
    <xsl:copy-of select="."/>
</xsl:template>

<xsl:template match="LocName">
    <xsl:copy-of select="."/>
</xsl:template>

这会遍历每个节点,并在找到匹配的模板时执行copy-of

您在 cmets 中提到您还想要for-each。这看起来像这样:

<xsl:template match="/">
    <output>
        <xsl:for-each select="//ItemDesc">
            <xsl:copy-of select="."/>
        </xsl:for-each>
        <xsl:for-each select="//LocName">
            <xsl:copy-of select="."/>
        </xsl:for-each>
    </output>
</xsl:template>

【讨论】:

  • 当我尝试应用
猜你喜欢
  • 2010-12-26
  • 1970-01-01
  • 2010-10-11
  • 1970-01-01
  • 2021-08-12
  • 2020-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多