【问题标题】:xslt 1.0 to replace xpath references with the actual data in the same xmlxslt 1.0 用同一 xml 中的实际数据替换 xpath 引用
【发布时间】:2012-11-26 03:49:03
【问题描述】:

我有一些要求,我需要替换同一个 xml 文件中的引用。 限制是只能使用 xslt 1.0。

下面是我的示例输入 xml。

    <org>
       <depts>
          <dept>
             <deptId>1009</deptId>
                <deptName>IT</deptName>
                <deptAccessCode>IT-1009</deptAccessCode>
          </dept>
          <dept>
             <deptId>2344</deptId>
             <deptName>BPO</deptName>
             <deptAccessCode>BP-2344</deptAccessCode>
          </dept>
          </depts>
          <employees>
             <employee>
             <name>abc</name>
             <dept>
              <REFERENCE>
                 <LocationXPath>/org/depts/dept[2]</LocationXPath>
              </REFERENCE>
          </dept>
          <employee>
       </employees>
</org>

现在我想用 XPath /org/depts/dept[2] 中的实际数据替换节点 REFERENCE。 所以输出 xml 应该如下所示。

    <org>
       <depts>
          <dept>
             <deptId>1009</deptId>
                <deptName>IT</deptName>
                <deptAccessCode>IT-1009</deptAccessCode>
          </dept>
          <dept>
             <deptId>2344</deptId>
             <deptName>BPO</deptName>
             <deptAccessCode>BP-2344</deptAccessCode>
          </dept>
          </depts>
          <employees>
             <employee>
             <name>abc</name>
             <dept>
             <deptId>2344</deptId>
             <deptName>BPO</deptName>
             <deptAccessCode>BP-2344</deptAccessCode>
          </dept>
          <employee>
       </employees>
    </org>

我在不同元素中有几个 REFERENCE 节点,它们引用了 xml 树中的不同 xpath,我需要用实际数据替换它们。

<someWhereInTheXmlTree>
<sometag>
   <REFERENCE>
      <LocationXPath>some/reference[1]/to/a/node[3]/in/the[4]/same/xml</LocationXPath>
   </REFERENCE>
</sometag>
<someWhereInTheXmlTree>
...
<ffff>
<bbbb>
   <REFERENCE>
      <LocationXPath>abc/xyz[1]/node[4]/element</LocationXPath>
   </REFERENCE>
</bbbb>
<ffff>

请帮助我。 提前感谢您的帮助。

到目前为止,我已经实现了一个 XSLT 来替换引用,但现在我面临着不需要的空名称空间。

这是我的 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tib="http://www.tibco.com/bw/xslt/custom-functions"
xmlns="http://www.realestate.org/residential/2010/schemas" >

<xsl:output omit-xml-declaration="no" indent="yes" method = "xml" />
<xsl:strip-space elements="*"/>
<xsl:param name="myxml"  />
<xsl:template match="node()|@*">
    <xsl:param name="isNodeToReplace"><xsl:call-template name="ReferenceCheck" /></xsl:param>
    <xsl:choose>
        <xsl:when test="$isNodeToReplace='true'">
            <xsl:call-template name="replaceWithData">
                <xsl:with-param name="ref"><xsl:value-of select="." /></xsl:with-param>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template name="ReferenceCheck">
    <xsl:choose>
        <xsl:when test="name(child::*[1])='REFERENCE' and name(child::*[1]//child::*[1])='LocationXPath'">true</xsl:when>
        <xsl:otherwise>false</xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template name="replaceWithData">
    <xsl:param name="ref" />
    <xsl:copy-of select="tib:evaluate($myxml,$ref)" />
</xsl:template>

</xsl:stylesheet>

在上面的 XSLT 中,我将整个 xml(正在处理的相同 xml)作为参数传递 $myxml

下面是我的示例输入 XML——这只是 xml 的一个 sn-p,我正在处理的实际 xml 文件太大并且包含如此复杂的树结构。但是这个示例 xml 是否足以生成我的问题。

输入文件

<?xml version="1.0" encoding="UTF-8"?>
<org xmlns="http://www.realestate.org/residential/2010/schemas">
    <depts>
        <dept>
            <deptId>1</deptId>
            <deptName>health</deptName>
            <deptAccessCode>HL007845</deptAccessCode>
        </dept>
    </depts>
    <employees>
        <employee>
            <name>TOM</name>
            <dept>
                <REFERENCE>
                    <LocationXPath>/org/depts/dept[1]</LocationXPath>
                </REFERENCE>
            </dept>
        </employee>
    </employees>
</org>

我的输出文件

<?xml version="1.0" encoding="UTF-8"?>
<org xmlns="http://www.realestate.org/residential/2010/schemas">
    <depts>
        <dept>
            <deptId>1</deptId>
            <deptName>health</deptName>
            <deptAccessCode>HL007845</deptAccessCode>
        </dept>
    </depts>
    <employees>
        <employee>
            <name>TOM</name>
            <dept xmlns="">
                <deptId>1</deptId>
                <deptName>health</deptName>
                <deptAccessCode>HL007845</deptAccessCode>
            </dept>
        </employee>
    </employees>
</org>  

预期输出的位置

<?xml version="1.0" encoding="UTF-8"?>
<org xmlns="http://www.realestate.org/residential/2010/schemas">
    <depts>
        <dept>
            <deptId>1</deptId>
            <deptName>health</deptName>
            <deptAccessCode>HL007845</deptAccessCode>
        </dept>
    </depts>
    <employees>
        <employee>
            <name>TOM</name>
            <dept>
                <deptId>1</deptId>
                <deptName>health</deptName>
                <deptAccessCode>HL007845</deptAccessCode>
            </dept>
        </employee>
    </employees>
</org>

所以我在替换的根元素中的 > 中得到了不需要的空名称空间。 希望这可以清楚地解释我的问题 提前致谢

最终我在下面的链接中找到了删除不需要的空名称空间的解决方案。 http://social.msdn.microsoft.com/forums/en-US/xmlandnetfx/thread/0de59291-ef3a-4a4c-9ca5-17923b16a504

这是新的 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tib="http://www.tibco.com/bw/xslt/custom-functions"
xmlns="http://www.realestate.org/residential/2010/schemas" >

<xsl:output omit-xml-declaration="no" indent="yes" method = "xml" />
<xsl:strip-space elements="*"/>
<xsl:param name="myxml"  />
<xsl:template match="node()|@*">
    <xsl:param name="isNodeToReplace"><xsl:call-template name="ReferenceCheck" /></xsl:param>
    <xsl:choose>
        <xsl:when test="$isNodeToReplace='true'">
            <xsl:call-template name="replaceWithData">
                <xsl:with-param name="ref"><xsl:value-of select="." /></xsl:with-param>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template name="ReferenceCheck">
    <xsl:choose>
        <xsl:when test="name(child::*[1])='REFERENCE' and name(child::*[1]//child::*[1])='LocationXPath'">true</xsl:when>
        <xsl:otherwise>false</xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template name="replaceWithData">
    <xsl:param name="ref" />
    <xsl:apply-templates select="tib:evaluate($myxml,$ref)" mode="move-to-namespace">
        <xsl:with-param name="namespace" select="'http://www.realestate.org/residential/2010/schemas'" />
    </xsl:apply-templates>
</xsl:template>
<xsl:template match="*" mode="move-to-namespace">
    <xsl:param name="namespace" />
    <xsl:element name="{local-name()}" namespace="{$namespace}">
        <xsl:copy-of select="@*" />
        <xsl:apply-templates select="node()" mode="move-to-namespace">
            <xsl:with-param name="namespace" select="$namespace"/>
        </xsl:apply-templates>
    </xsl:element>
</xsl:template>

<xsl:template match="text() | comment() | processing-instruction()" mode="move-to-namespace">
    <xsl:copy/>
</xsl:template>

</xsl:stylesheet>

如果有任何 xslt 专家进一步改进它以避免任何不必要的说明并提供适当的解释,那将非常高兴。

提前致谢

【问题讨论】:

  • 大家好,我的 XSLT 处理器支持自定义函数 tib:evaluate()。现在我可以用实际数据替换引用,但是在替换的根元素中我得到了不需要的空名称空间。
  • 我在这里完全被打动了。请专家帮助我。
  • Sardar Ahamed,很高兴您找到了解决方案。这里有几个类似的问题,我已经回答了其中一些问题——当你想在输出中去掉一些命名空间时,基本原则不是复制节点(或将其创建为文字结果元素),而是使用xsl:element(或xsl:attribute 用于属性)重新创建节点。将来,如果您有相关问题,请将其作为单独的 SO 问题提出 - 否则您的读者无法很好地看到您已更新问题并有其他问题。通过单独的 SO 问题,您还可以获得更多代表。

标签: xpath reference replace xslt-1.0


【解决方案1】:

一般情况

  • 在纯 XSLT 1.0 中不可行。

  • 在纯 XSLT 2.0 中不可能

  • 在纯 XSLT 3.0 中可能是可能的——阅读
    &lt;xsl:evaluate&gt;指令。

  • 在 XSLT 1.0 中,如果您的 XSLT 处理器实现了 EXSLT dyn:evaluate() 扩展功能(少数人这样做),您可能会很幸运。 否则,您将不得不编写一个扩展函数来选择 节点并将它们返回。

如果对 XPath 表达式的语法有限制,那么可以实现纯 XSLT 1.0 解决方案。

【讨论】:

  • 您好,感谢您的快速回复。我将检查我的 xslt 处理器对评估()函数的支持。意思是,您能否解释一下答案中的最后一部分,以了解对 xpath expressipns 语法的限制,那么有可能吗?
  • 嗨 Dimitre,我已经更新了进度。我的 xslt 处理器支持 tib:evaluate() 自定义功能。现在我面临替换元素中的空名称空间。请建议如何删除不需要的名称空间.
  • 我很少看到其他关于删除空名称空间的帖子。但在我的情况下,我需要使用 copy-of 指令,因为我正在动态生成节点( )
  • @SardarAhamed:我认为这是一个新问题的主题。同时,您可以决定接受当前问题的最佳答案(通过单击答案旁边的复选标记)。
  • 是的,我选择了最佳答案
【解决方案2】:

这在纯 XSLT 1.0 中是不可能的。您必须使用扩展功能,例如 Xalan evaluate expression

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    相关资源
    最近更新 更多