2010-10-05 257 views
1

编辑:实例现在包含有我的主文档故障转换XML文档URL

大家在所有标签!我只是对XSLT有个简单的问题。我有一个很大的xml文件,其中嵌套了许多<DIMENSION_Id>节点。在每个<DIMENSION_Id>节点中有两个SYN标记:<SYN>String</SYN><SYN>Integer</SYN>我想要做的是将每个DIMENSION_Id的最远的子节点连接到它的所有祖先路径以创建一个URL。

<DIMENSIONS VERSION="1.0.0"> 
    <DIMENSION NAME="Category" SRC_TYPE="INTERNAL"> 
     <DIMENSION_NODE ID="1000"/> 
     <DIMENSION_Id> 
      <SYN>Text</SYN> 
      <SYN>Number</SYN> 
      <DIMENSION_Id> 
       <SYN>More Text</SYN> 
       <SYN>Another Number</SYN> 
      </DIMENSION_Id> 
     </DIMENSION_Id> 
    </DIMENSION> 
</DIMENSIONS> 

我写了这个XSLT先获得从父节点的所有信息,然后最后创建一个完整的URL子节点。不幸的是,它只给了我最远的子节点的信息......我不知道如何附加任何其他文本。 (应改为类似:最远的父/接近父/母/ item_selected)

不幸的是它是所有给我当前节点的值....下面是我写的XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text" /> 
    <xsl:template match="/DIMENSION_NODE"> 
     <xsl:for-each select="ancestor-or-self::*"> 
      <xsl:value-of select="@SYN" /> 
      <xsl:text>/</xsl:text> 
      <xsl:value-of select="." /> 
      <xsl:value-of select="@SYN" /> 
      <xsl:text>/</xsl:text> 
      <xsl:value-of select="." /> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

在此先感谢您的帮助!

+0

你的问题配方不好。您的输入样本与样式表不符,也不符合您的期望结果。请纠正这一点。 – 2010-10-05 16:19:23

+0

请原谅我,但我不明白你需要告诉我什么信息。我想要的是让这就是在父SYN一路孩子,所以它看起来像一个URL值,即: GRANDPARENT_SYN_VALuE/PARENT_SYN_VALUE/CURRENT_NODE_SYN_VALUE 为每个XML节点。接下来我需要做的是将第二个SYN值中的整数与URL匹配......但我主要关心的是实际的URL字符串。如果你需要我进一步澄清,请让我知道.. – Daniel 2010-10-05 17:02:44

+0

通过制定不佳的问题,我的意思是:'GRANDPARENT_SYN_VALuE/PARENT_SYN_VALUE' ...但你有**两个**'SYN'元素;你的样式表匹配输入样本中不存在的'DIMENSION_NODE'根元素;你试图用''输出'SYN'属性和'DIMENSION_NODE'元素的字符串值...... – 2010-10-05 17:53:48

回答

1

编辑:输入样本更接近问题。

有了这个输入:

<DIMENSIONS VERSION="1.0.0"> 
    <DIMENSION NAME="Category" SRC_TYPE="INTERNAL"> 
     <DIMENSION_NODE ID="1000"/> 
     <DIMENSION_Id> 
      <SYN>Text</SYN> 
      <SYN>1</SYN> 
      <DIMENSION_Id> 
       <SYN>More Text</SYN> 
       <SYN>2</SYN> 
      </DIMENSION_Id> 
     </DIMENSION_Id> 
    </DIMENSION> 
</DIMENSIONS> 

两个选项。

1)应用模板和模式,以祖先:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text"/> 
    <xsl:template match="text()"/> 
    <xsl:template match="SYN[number()!=.]"> 
     <xsl:apply-templates select="ancestor::DIMENSION_Id" mode="output"/> 
     <xsl:value-of select="concat(' ',../SYN[number()=.],'&#xA;')"/> 
    </xsl:template> 
    <xsl:template match="DIMENSION_Id" mode="output"> 
     <xsl:value-of select="concat('/',SYN[number()!=.])"/> 
    </xsl:template> 
</xsl:stylesheet> 

2)使用参数:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text"/> 
    <xsl:template match="text()"/> 
    <xsl:template match="SYN[number()!=.]"> 
     <xsl:param name="pPath"/> 
     <xsl:value-of select="concat($pPath,' ',../SYN[number()=.],'&#xA;')"/> 
    </xsl:template> 
    <xsl:template match="DIMENSION_Id"> 
     <xsl:param name="pPath"/> 
     <xsl:apply-templates> 
      <xsl:with-param name="pPath" 
           select="concat($pPath,'/',SYN[number()!=.])"/> 
     </xsl:apply-templates> 
    </xsl:template> 
</xsl:stylesheet> 

两个输出:

/Text 1 
/Text/More Text 2 
+0

嗯..对于第一个我使用SYN切换文件,并使用DIMENSION_Id命名的文件...但它不起作用。我无法真正理解你的第二个例子,我很抱歉...我对XSLT很陌生。 – Daniel 2010-10-05 17:00:39

+0

@Daniel:检查我的编辑与输入更接近你的 – 2010-10-05 18:26:56

1

我猜你想要这个

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

<xsl:template match="DIMENSION_Id[not(DIMENSION_Id)]"> 
    <xsl:apply-templates select="(.|ancestor::DIMENSION_Id)/SYN" mode="gen"/> 
</xsl:template> 

<xsl:template match="SYN" mode="gen"> 
    <xsl:value-of select="concat('/',.)"/> 
</xsl:template> 
<xsl:template match="text()"/> 
</xsl:stylesheet> 

当这个变换所提供的XML文档应用(校正为良好的形成):

<DIMENSIONS VERSION="1.0.0"> 
    <DIMENSION NAME="Category" SRC_TYPE="INTERNAL"> 
     <DIMENSION_NODE ID="1000"/> 
     <DIMENSION_Id> 
      <SYN>Text</SYN> 
      <SYN>Number</SYN> 
      <DIMENSION_Id> 
       <SYN>More Text</SYN> 
       <SYN>Another Number</SYN> 
      </DIMENSION_Id> 
     </DIMENSION_Id> 
</DIMENSION> 
</DIMENSIONS> 

有用,正确的结果产生:

/Text/Number/More Text/Another Number 
+0

hmm..I'm没有得到这个时候输出任何东西......但我认为很多是因为我自己的不正确张贴的文件。我现在编辑它是正确的。对困惑感到抱歉。 – Daniel 2010-10-05 19:25:57

+0

@Daniel:使用新的XML文档,我可以得到完全相同的结果。你有没有更正它,因为你正在呈现的那个是*不是*格式良好,会导致解析器错误。 – 2010-10-05 19:36:53

0

对不起,每个人都感到困惑。我得到了一些帮助,这里是解决方案:

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 

    <xsl:template match="/"> 
     <URLs> 
      <xsl:apply-templates select="//DIMENSION_NODE"/> 
     </URLs> 
    </xsl:template> 

    <xsl:template match="DIMENSION_NODE"> 
     <xsl:call-template name="getPath"> 
      <xsl:with-param name="currentnode" select="."/> 
     </xsl:call-template> 
    </xsl:template> 

    <xsl:template name="getPath"> 
     <xsl:param name="currentnode"/> 
     <xsl:param name="currenttext" select="''"/> 
     <xsl:param name="firstrun" select="1"/> 
     <xsl:choose> 
      <xsl:when test="$currentnode[parent::DIMENSION]"> 
       <URL> 
        <xsl:value-of select="$currenttext"/> 
       </URL> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:choose> 
        <xsl:when test="$firstrun = 1"> 
         <xsl:variable name="gettext"> 
          <xsl:text>/</xsl:text> 
          <xsl:value-of select="concat($currentnode/DVAL/SYN[1],'&#x9;',$currentnode/DVAL/DVAL_ID/@ID)"/> 
         </xsl:variable> 
         <xsl:call-template name="getPath"> 
          <xsl:with-param name="currentnode" select="$currentnode/.."/> 
          <xsl:with-param name="currenttext" select="concat($gettext,$currenttext)"/> 
          <xsl:with-param name="firstrun" select="0"/> 
         </xsl:call-template> 
        </xsl:when> 
        <xsl:otherwise> 
         <xsl:variable name="gettext"> 
          <xsl:text>/</xsl:text> 
          <xsl:value-of select="$currentnode/DVAL/SYN[1]"/> 
         </xsl:variable> 
         <xsl:call-template name="getPath"> 
          <xsl:with-param name="currentnode" select="$currentnode/.."/> 
          <xsl:with-param name="currenttext" select="concat($gettext,$currenttext)"/> 
          <xsl:with-param name="firstrun" select="0"/> 
         </xsl:call-template> 
        </xsl:otherwise> 
       </xsl:choose> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 
</xsl:stylesheet> 

此外,我在我的XML文件中的错误道歉。

+0

我很少downvote niether问题或答案,但这是错误的。除了冗长和错误的设计(不是XSLT风格,即'$ firstrun'或调用命名模板传递上下文节点作为参数),它不会帮助任何人,因为它不符合您的输入样本('DVAL'和'DVAL_ID '元素)。 – 2010-10-06 18:10:41