Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

xml - 避免在缺少“匹配”实体的情况下输出属性(Avoid output an attribute in case of the missing “matching” entity)

After XSLT execution i have a picture like (some attributes with empty values)

(XSLT执行后,我有一张类似的图片(某些属性值为空))

  <Document
            Region="Earth"
            City="Tokio"
            District=""
            Street="" >
  </Document>

My XSLT was

(我的XSLT是)

<xsl:template match="Document">
     <Document
            Region="{Region/@attr1}" 
            City="{City/@attr2}"  
            District="{District/@attr3}"  
            Street="{Street/@attr4}"  >
      <xsl:apply-templates />
    </Document>
  </xsl:template>

All Xpathes are correct.

(所有Xpathes都是正确的。)

The thing is that, as i suppose, my code is too "direct".

(事实是,正如我想的那样,我的代码太“直接”了。)

Need more flexibilty.

(需要更多的灵活性。)

Briefly, compliance of the condition: "If there is no value\attribute to catch in source XML then there shouldn't be any corresponding record in the output node".

(简要地说,符合条件:“如果没有值\属性可捕获源XML,则输出节点中不应有任何相应的记录”。)

In the sample code {District/@attr3} and {Street/@attr4} Xpath\Expression may return some values but sometimes are not (there is even no attribute in the source node then).

(在示例代码{District / @ attr3}和{Street / @ attr4}中,Xpath \ Expression可能返回一些值,但有时不返回(这样,源节点中甚至没有属性)。)

So desired output in case when {District/@attr3} and {Street/@attr4} return\"matching" nothing have to looks like:

(因此,在{District / @ attr3}和{Street / @ attr4}返回\“ matching”的情况下,期望的输出没有什么变化:)

Desired output

(所需的输出)

  <Document 
            Region="Earth"
            City="Tokio" >
  </Document>

How to improve XSLT code (1.0)?

(如何改进XSLT代码(1.0)?)

  ask by Alex translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's hard to advise without seeing the broader picture.

(看不到更广阔的前景很难建议。)

Try something like:

(尝试类似:)

<xsl:template match="Document">
    <Document
            Region="{Region/@attr1}" 
            City="{City/@attr2}" >
        <xsl:apply-templates select="District/@attr3 | Street/@attr4"/>
        <xsl:apply-templates/>
    </Document>
</xsl:template>

<xsl:template match="District/@attr3">
    <xsl:attribute name="District">
        <xsl:value-of select="."/>
    </xsl:attribute>
</xsl:template>

<xsl:template match="Street/@attr4">
    <xsl:attribute name="Street">
        <xsl:value-of select="."/>
    </xsl:attribute>
</xsl:template>

or perhaps:

(也许:)

<xsl:template match="Document">
    <Document
            Region="{Region/@attr1}" 
            City="{City/@attr2}" >
        <xsl:apply-templates select="District/@attr3 | Street/@attr4"/>
        <xsl:apply-templates/>
    </Document>
</xsl:template>

<xsl:template match="District/@attr3 | Street/@attr4">
    <xsl:attribute name="{name(..)}">
        <xsl:value-of select="."/>
    </xsl:attribute>
</xsl:template>

Untested, because no test input was provided.

(未经测试,因为未提供测试输入。)


Note that this assumes the attributes will either have a value or not be there at all - not that they are empty.

(请注意,这假设属性将具有值或根本不存在-并非它们为空。)

If they can be empty, you need to add a predicate, eg:

(如果它们可以为空,则需要添加一个谓词,例如:)

<xsl:apply-templates select="(District/@attr3 | Street/@attr4)[string()]"/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...